in Education by
I've tried to test socket connection in Java, but failed. 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(); } } Output is (after running a server and, after it, client): Server socket created Socket accepted Output created Input: null I've no idea what's the problem and why client didn't receive string sent to it. I'd appreciate any help. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Usually when I use classes like PrintWriter or OutputStream I need to flush its contents to send the data through a socket or write it to a file.

Related questions

0 votes
    I've tried to test socket connection in Java, but failed. Here is my code (two simple applications, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I'm trying to create Server-client connection using sockets. Server is just an Echo-server. I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which value of Socket.readyState atribute of WebSocket indicates that the connection has been closed or could not be opened? A - 0 B - 1 C - 2 D - 3...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    Which value of Socket.readyState atribute of WebSocket indicates that the connection is going through the closing handshake? A - 0 B - 1 C - 2 D - 3...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    Which value of Socket.readyState atribute of WebSocket indicates that the connection is established and communication is possible? A - 0 B - 1 C - 2 D - 3...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    Which value of Socket.readyState atribute of WebSocket indicates that the connection has not yet been established? A - 0 B - 1 C - 2 D - 3...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    I am getting the following error trying to read from a socket. I'm doing a readInt() on that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 30, 2022 in Education by JackTerrance
0 votes
    I am getting the following error trying to read from a socket. I'm doing a readInt() on that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    I use nonblocking socket to receive new connection. But the code repeatedly fails to accept(). int sockfd = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    Step 1: server I've created a simple server with Node & Socket.io which declares a namespace under ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I get data from socket io this JSON every 10 sec.. data { "nr": "1" } data { "nr": " ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
...