I tried to check socket connections in Java, but could not. Here is my code (two simple applications, server and client):
public class TestServer { public static void main(String args[]) throws IOException { ServerSocket serverSocket = new ServerSocket(1111); System.out.println("Server socket created"); Socket socket = serverSocket.accept(); System.out.println("Socket accepted"); PrintWriter output = new PrintWriter(socket.getOutputStream(), true); System.out.println("Output created"); output.write("Output string"); socket.close(); serverSocket.close(); } } public class TestClient { public static void main(String args[]) throws IOException { Socket socket = new Socket("127.0.0.1", 1111); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); System.out.println("Input: " + input.readLine()); socket.close(); } }
Exit (after starting the server and after it the client):
Server socket created Socket accepted Output created Input: null
I do not know what the problem is and why the client did not receive the string sent to him. I would appreciate any help.
source share