I have written a simple java code to send a message from server to client and the client should display it respectively. The codes are.
Client.java
impor
You're reading lines but you aren't writing lines. Add a line terminator to the message, or use newline()
.
In Server code rather than using BufferedWriter, use PrintWriter.
while (true) {
Socket mysocket = server_socket.accept();
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader reader = new BufferedReader(new InputStreamReader(mysocket.getInputStream()));
out.println("Hello World!");
System.out.println("data sent");
}
This is working on my system. Try this.
Referring all your answers and searching a bit on google i have finally got the code running by making the following changes in Server Class.
while (true) {
Socket mysocket = server_socket.accept();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(mysocket.getOutputStream()));
BufferedReader reader = new BufferedReader(new InputStreamReader(mysocket.getInputStream()));
writer.write("Hello omkar \n");
writer.flush();
System.out.println("data sent");
}