Non-blocking method for reading writing objects to sockets

Joined
May 7, 2010
Messages
1
Reaction score
0
Non-Blocking Object Serialization via a socket

Client side:
Socket server = Socket(serverAddress, serverPort).connect();
OutputStream outStream = server.getOutputStream();
OutputObjectStream objectOutStream = new OutputObjectStream(outStream);

//write to server
outStream.write(1);
outObjectStream.write(MySerializableObject);


ServerSide:

Socket client = serverSocket.accept();
InputStream inStream = client.getInputStream();
InputObjectStream inObjectStream = new InputObjectStream(inStream);

//main driving while loop of the server
while (running_forever) {
//do all kinds of work
if (inStream.available() >0) { //NON-BLOCKING
//read in the number the client sent (pick it off the stream)
inStream.read();
//read in the object the client sent
Object = inObjectStream.readObject();
}

//do other kinds of work

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top