Problem with a blocking network task

Posted by user326967 on Stack Overflow See other posts from Stack Overflow or by user326967
Published on 2010-04-27T15:05:08Z Indexed on 2010/04/28 5:13 UTC
Read the original article Hit count: 335

Hello everyone.

I'm new in Java so please forgive any obscene errors that I may make :)

I'm developing a program in Java that among other things it should also handle clients that will connect to a server. The server has 3 threads running, and I have created them in the following way :

DaemonForUI du;
DaemonForPort da;
DaemonForCheck dc;

da = new DaemonForPort(3);
dc = new DaemonForCheck(5);
du = new DaemonForUI(7);

Thread t_port = new Thread(da);
Thread t_check = new Thread(dc);
Thread t_ui = new Thread(du);

t_port.setName("v1.9--PORTd");
t_check.setName("v1.9-CHECKd");
t_ui.setName("v1.9----UId");

t_port.start();
t_check.start();
t_ui.start();

Each thread handles a different aspect of the complete program. The thread t_ui is responsible to accept asynchronous incoming connections from clients, process the sent data and send other data back to the client. When I remove all the commands from the previous piece of code that has to with the t_ui thread, everything runs ok which in my case means that the other threads are printing their debug messages.

If I set the t_ui thread to run too, then the whole program blocks at the "accept" of the t_ui thread.

After reading at online manuals I saw that the accepted connections should be non-blocking, therefore use something like that :

public ServerSocketChannel ssc = null;

ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(port));
ssc.configureBlocking(false);

SocketChannel sc = ssc.accept();

if (sc == null) {
    ;
}
else {
    System.out.println("The server and client are connected!");
    System.out.println("Incoming connection from: " + sc.socket().getRemoteSocketAddress());
    in = new DataInputStream(new BufferedInputStream(sc.socket().getInputStream()));
    out = new DataOutputStream(new BufferedOutputStream(sc.socket().getOutputStream()));
    //other magic things take place after that point...

The thread for t_ui is created as follows :

class DaemonForUI implements Runnable{
    private int cnt;
    private int rr;
    public ListenerForUI serverListener;

    public DaemonForUI(int rr){
        cnt = 0;
        this.rr = rr;
        serverListener = new ListenerForUI();
    }

    public static String getCurrentTime() {
        final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        return (sdf.format(cal.getTime()));
    }


    public void run() {
        while(true) {
            System.out.println(Thread.currentThread().getName() + "\t (" + cnt + ")\t (every " + rr + " sec) @ " + getCurrentTime());
            try{
                Thread.sleep(rr * 1000);
                cnt++;
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

Obviously, I'm doing something wrong at the creation of the socket or at the use of the thread. Do you know what is causing the problem?

Every help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about network-programming