Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java socket - Read & Write

Tags:

java

sockets

The problem: Client doesn't receive any message.

Here is the full code for both client and server:

CLIENT

public class Client extends Socket{


public Client(String hostName, int port) throws UnknownHostException, IOException {
    super(hostName,port);

    BufferedReader in = new BufferedReader(new InputStreamReader(getInputStream()));

    while(true) {
        String line = in.readLine();
        System.out.println("Text received: " + line);
    }

}

SERVER

public final class Server extends ServerSocket{

public Server(int port) throws IOException {
    super(port);

    System.out.println("Server waiting for client 1");
    Socket client1 = accept();
    PrintWriter writer = new PrintWriter(client1.getOutputStream(), true);
    writer.write("Hello user 1");

    System.out.println("Server waiting for client 2");
    Socket client2 = accept();
    PrintWriter writer2 = new PrintWriter(client2.getOutputStream(), true);
    writer2.write("Hello user 2");

    System.out.println("Clients connected");

}
  • I start the server to listen to port 4444
  • I start the clients with hostname of "localhost" and port 4444
like image 366
ShlomiRex Avatar asked Jul 29 '16 22:07

ShlomiRex


People also ask

What is socket read in java?

1. Overview. To send and receive data over a network, we often use sockets. Sockets are nothing but a combination of an IP address and a port number, which can uniquely identify a program running on any given machine. In this tutorial, we'll show how we can read data which is sent to us over a socket.

What is a socket read?

Behavior for sockets: The read() call reads data on a socket with descriptor fs and stores it in a buffer. The read() all applies only to connected sockets. This call returns up to N bytes of data. If there are fewer bytes available than requested, the call returns the number currently available.

How do you use socket in java?

To communicate over a socket connection, streams are used to both input and output the data. The socket connection is closed explicitly once the message to the server is sent. In the program, the Client keeps reading input from a user and sends it to the server until “Over” is typed.

What is difference between socket () and ServerSocket () class?

Here, two classes are being used: Socket and ServerSocket. The Socket class is used to communicate client and server. Through this class, we can read and write message. The ServerSocket class is used at server-side.


1 Answers

You have to include a newline character at the end of the message, and also flush the PrintWriter if the connection is not immediately altered, forcing an automatic flush:

writer.write("Hello user 1\n");
writer.flush();

EDIT:

It is possible to enable automatic flushing on a PrintWriter using the constructor new PrintWriter(someOutputStream, true)

However, as explained in the documentation:

if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output

This means that even with automatic flushing, the PrintWriter would still have to be manually flushed after write is called, and a newline character (\n) would still have to be included at the end of the string.

EDIT 2:

Here is a small example of a fully functional client/server application:

Client:

public static void main(String[] args){
    try{
        Socket socket = new Socket(HOST_ADDRESS, PORT);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(bufferedReader.readLine());
        bufferedReader.close();
        socket.close();
    }catch(IOException e){}
}

Server:

public static void main(String[] args){
    try{
        ServerSocket serverSocket = new ServerSocket(PORT);
        Socket socket = serverSocket.accept();
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
        printWriter.write("Hello user!\n");
        printWriter.flush();
        printWriter.close();
        socket.close();
        serverSocket.close();
    }catch(IOException e){}
}
like image 113
Alexander Guyer Avatar answered Sep 22 '22 11:09

Alexander Guyer