Non-blocking methods for transferring of Object

S

Sameer

Hello,
I have designed one chatting software using non-blocking features of
java.
In this project I transferred data using non-blocking methods of Buffer
related classes.
Now I decided to change the design of the projct and I like to send the
data as objects over the network using readObject and writeObject
methods of socket class.
For this purpose, I have to create instances ObjectInputStream and
ObjectOutputStream.
As per our previous discussion on this group the constructors of these
classes are BLOCKING methods.
Then how to carry out this? Is there any alternative or I have to
continue with blocking approach i.e. a multithreaded chatting server
where each thread handles one connection?
The code given under is not working...
Please suggest some remedy.


import java.io.*'
import java.net.*;
import java.nio.channels.*;;
import java.util.*;

public class PollingChatServer {
private int port;

private Vector sockets = new Vector();

public PollingChatServer(int port) throws IOException {
this.port = port;
listen();
}

private void listen() {
try {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ssc.socket().bind(new InetSocketAddress(port));
System.out.println("Listening on port " + port);
while (true) {
System.out.println("Looking for connections...");
SocketChannel sc = ssc.accept();
if (sc != null) {
System.out.println("Connection from " + sc);
sockets.addElement(sc);
}
for (Enumeration e = sockets.elements(); e.hasMoreElements();) {
SocketChannel socch = null;
try {
socch = (SocketChannel) (e.nextElement());
Socket soc = socch.socket();
System.out.println("In for loop before creation of ois...");
//a blocking constructior object input stream
ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());
Message messageObject = (Message) ois.readObject();
String str = messageObject.message;
if (str.length() != 0) {
System.out.println("Sending " + str);
Message.BroadcastMessage(str, sockets);
}
} catch (IOException ie) {
System.out.println("Connection lost: " + socch);
sockets.remove(socch);
socch.close();
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe);
sockets.remove(socch);
socch.close();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
}
} catch (IOException ie) {
ie.printStackTrace();
}
}

public static void main(String args[]) throws Exception {
int port = 12769;
new PollingChatServer(port);
}
}
 
F

Filip Larsen

Sameer wrote
I have designed one chatting software using non-blocking features of
java.
In this project I transferred data using non-blocking methods of Buffer
related classes.
Now I decided to change the design of the projct and I like to send the
data as objects over the network using readObject and writeObject
methods of socket class.
For this purpose, I have to create instances ObjectInputStream and
ObjectOutputStream.
As per our previous discussion on this group the constructors of these
classes are BLOCKING methods.

One approach can be to use an intermediate buffer like
ByteArrayOutputStream and ByteArrayInputStream to collect and provide
data respectively for the Object streams. The content of the buffer can
then be transfered using non-blocking IO.


Best regards,
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top