Problem with blocking network task

I am new to Java, so please forgive any obscene mistakes I may make :)

I am developing a Java program that, among other things, also needs to handle clients that will connect to the server. There are 3 threads running on the server and I created them like this:

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 processes a different aspect of the entire program. The t_ui thread is responsible for accepting asynchronous incoming connections from clients, processing the sent data, and passing other data to the client. When I remove all the commands from the previous code snippet related to the t_ui thread, everything works fine, which in my case means that other threads are printing their debug messages.

If I also run the t_ui thread, then the whole program blocks "accepting" the t_ui thread.

After reading online tutorials, I saw that accepted connections should be non-blocking, so use something like this:

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 stream 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 am doing something wrong when creating a socket or using a stream. Do you know what is causing the problem?

Any help would be greatly appreciated.

+2


a source to share


2 answers


Don't use non-blocking I / O until you know you need it. Just start a new stream for each received socket as well as receiving streams.



+1


a source


Problem solved :) I looked at your suggestions and looked closely at the code. This was a design error as I had a function that created a while (true) loop inside the DaemonForUI constructor (or rather, inside the ListenerForUI ()). This caused the entire program to loop through the while statement, thereby stopping every other action.

Stupid mistake I must admit ... :(



Thanks for the help to everyone who answered my question.

I will go over the mentioned idea of ​​creating a new thread for each incoming connection. The responsibility that has to be done for every incoming connection is not that heavy, so I thought a single thread could do the job.

0


a source







All Articles