BAEL-4895 brokenpipe error (#14346)

Co-authored-by: Sachin kumar <sachin.n.kumar@oracle.com>
This commit is contained in:
sachin 2023-07-31 22:45:49 +05:30 committed by GitHub
parent a0649ba357
commit bb967e0836
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.socketexception.brokenpipe;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 1234);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("HELLO".getBytes());
System.out.println("Writing to server..");
// Simulating a delay after writing to the socket
Thread.sleep(3000);
// Writing again to the closed socket
outputStream.write("HI".getBytes());
System.out.println("Writing to server again..");
System.out.println("Closing client.");
outputStream.close();
socket.close();
}
catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.socketexception.brokenpipe;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(1234);
System.out.println("Server listening on port 1234...");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress());
//Add some delay for reading from client
Thread.sleep(2000);
InputStream in = clientSocket.getInputStream();
System.out.println("Reading from client:" + in.read());
in.close();
clientSocket.close();
serverSocket.close();
}
catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}