Okay, so I'm tying to get use to programming with sockets, and I have run into a bit of a snag.
As of now, my program basically works like this:
Server: Listen for a connection
Client: Request a connection
Server: Accept connection
Client: Send message
Client: Wait for response
Server: Processes message
Server: Send response
I want my client to also listen for message from the server. And these message can happen at any time. When you do something like:
The read blocks until until something can be read. This is a desired effect. However, I would like to prevent the listening thread from waking so that I can send a message and get the response without that while loop picking it up.
Is there any way to guarantee that?
I could open up two connections, one for sending and one for receiving. Is that frowned upon? It seems like a wast of resources.
As of now, my program basically works like this:
Server: Listen for a connection
Client: Request a connection
Server: Accept connection
Client: Send message
Client: Wait for response
Server: Processes message
Server: Send response
I want my client to also listen for message from the server. And these message can happen at any time. When you do something like:
Code:
...
public final void listen(Socket s)
{
InputStream in = s.getInputStream();
int aByte;
while((aByte=in.read())!=-1)
{
//Do stuff
}
}
...
Is there any way to guarantee that?
I could open up two connections, one for sending and one for receiving. Is that frowned upon? It seems like a wast of resources.
Comment