Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket server that waits for message from client to read

I have the following socket server in java,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MiddleSocket {
    private ServerSocket middleman;
    private int port = 1027;
    private Socket client;


    protected void createSocketServer()
    {
        try
        {
            middleman = new ServerSocket(port);
            client = middleman.accept();
            middleman.close();
            PrintWriter out = new PrintWriter(client.getOutputStream(),true);
            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            while(true)
            {
                System.out.println("echo: " + in.readLine());
                out.println("test");
            }
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }

}

It works correctly in how it reads from the client and everything. However, I want the socket server to only try and read when there is a message from the client since in the loop it keeps reading even when no messages from the client are received this outputs a bunch of

echo: null

Is there some mechanism short of a wait() to have the while loop to know to just sit and wait and then fire when a message is sent from the client to read that message and once that message is read and a reply is sent, to then just sit idle until the next message from the client is received?

like image 441
jgr208 Avatar asked Oct 20 '14 16:10

jgr208


People also ask

How do you wait on a socket?

To wait for activity on any sockets managed by a TCP/IP application, use a SOCKET TYPE=SELECT command. To wait for activity on the sockets, any or all of read, write, and exception lists can be passed to the SOCKET TYPE=SELECT command.

Which method of ServerSocket will wait for a client to initiate communication?

To create the server application, we need to create the instance of ServerSocket class. Here, we are using 6666 port number for the communication between the client and server. You may also choose any other port number. The accept() method waits for the client.

Can server socket send message to client?

When a server wants to communicate with a client, there is a need for a socket. A socket is a point of connection between the server and the client. TCP/IP server program that sends message to the client.

How do you send data from client to server in socket programming?

Create a socket with the socket() system call. Initialize the socket address structure as per the server and connect the socket to the address of the server using the connect() system call. Receive and send the data using the recv() and send(). Close the connection by calling the close() function.


1 Answers

You can simply do something like as follows:

String line;
while ((line = in.readLine()) != null) {
   \\Do stuff
}

This should have the expected behaviour.

Edit:

here's a full example of what I was talking about in the comments using your code:

package javaapplication12;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketExample {
    private ServerSocket middleman;
    private int port = 8080;
    private Socket client;


    protected void createSocketServer()
    {
        try
        {
            while (true){
                middleman = new ServerSocket(port);
                client = middleman.accept();
                middleman.close();
                PrintWriter out = new PrintWriter(client.getOutputStream(),true);
                BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
                String line;
                while((line = in.readLine()) != null)
                {
                    System.out.println("echo: " + line);
                    out.println("test");
                }
            }
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }

    public static void main(String[] args){
        SocketExample test = new SocketExample();
        test.createSocketServer();
    }
}
like image 72
WillBD Avatar answered Oct 07 '22 05:10

WillBD