sockets - Send or receive an notification through TCP in java -


here main method of client program write outputstream server , wait server send 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 = input.nextline(); //read command user         out.write(a.getbytes());     //send command server         byte[] data = new byte[10000];         in.read(data, 0, 10000);     //receive output     } } 

the server program can accept multiple buyer @ same time , start each thread below

the run() method 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); // data                 out.write(result.getbytes()); // return output buyer client             }                } catch (ioexception e)     {         // todo auto-generated catch block         e.printstacktrace();     } } 

the client program put outputstream , server thread read (each client handled 1 server thread) send client. each write client match 1 read server , vice versa. however, when there special message sending server client (out of cycle mentioned earlier), there no way client receive without messing cycle. also, stalled input.nextline() in client program client not receive notification unless sends command. please suggest efficient way implement real-time notification problem?

i thinking making server send outputstream every thread, 1 have notification receive message; others receive "-1". client program check inputstream @ beginning , handle it. however, method seems inefficient real server.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -