0

Here is the main method of Client program which write to OutputStream for server and then wait for the server to send back a response.

public static void main(String[] args) throws IOException
{
    Scanner input = new Scanner(System.in);
    InetAddress server_addr = InetAddress.getByName(AUCTIONSERVER_IP_ADDRESS);
    Socket client = new Socket(server_addr, BUYER_PORT);
    user = "";
    OutputStream out = client.getOutputStream();
    InputStream in = client.getInputStream();
    while (true)
    {
        String a = input.nextLine(); //read command from user
        out.write(a.getBytes());     //send the command to server
        byte[] data = new byte[10000];
        in.read(data, 0, 10000);     //receive the output
    }
}

The server program which can accept multiple buyer at the same time and start each Thread below

The run() method for each Thread server create

public void run()
{
    try
    {
            OutputStream out = this.socket.getOutputStream();
            InputStream in = this.socket.getInputStream();

            while (true)
            {
                byte[] data = new byte[100];
                in.read(data, 0, 100); // do something with the data
                out.write(result.getBytes()); // return the output to Buyer client
            }           
    } catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The client program will put something to OutputStream and server thread will read it (each client will be handled by 1 server thread) then send something back to client. Each write from client will match with one read from server and vice versa. However, when there is a special message sending from server to client (out of the cycle mentioned earlier), there is no way for client to receive it without messing up the cycle. Also, it will be stalled by input.nextLine() in client program so the client will not receive the notification unless he sends any command. Could anyone please suggest an efficient way to implement real-time notification for this problem?

I am thinking about making the server send an OutputStream to every thread, the one who actually have the notification will receive the message; the others will receive something like "-1". All the client program will check for inputStream at the beginning and handle it. However, this method seems inefficient for real server.

Toan Le
  • 412
  • 1
  • 6
  • 17
  • Totally unclear, not to mention incoherent, but this code doesn't work anyway. You're ignoring the count returned by the `read()` method. – user207421 Apr 26 '15 at 09:32
  • @EJP I just re-worded the problem a little. I hope it's a little clearer. – Toan Le Apr 26 '15 at 10:51
  • I think the [http://stackoverflow.com/questions/10621783/is-there-epoll-equivalent-in-java] contains an answer to your question. – Zaboj Campula Apr 26 '15 at 17:50
  • I was too naive to the client program have to wait for respond after sending each command to server. I could just make a thread on client program which always wait for incoming message from servers. That totally works! – Toan Le Apr 26 '15 at 21:12

0 Answers0