Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

Non-blocking methods for transferring of Object

Thread view: 
Sameer - 07 May 2005 08:38 GMT
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);
}
}
Filip Larsen - 07 May 2005 10:58 GMT
> I have designed one chatting software using non-blocking features of
> java.
[quoted text clipped - 7 lines]
> 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,
Signature

Filip Larsen



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.