Search code examples
javadebuggingnio

JavaNIO multithreaded server not working


Hello Experts
can somebody please indentify the problem with this server why this is unable to connect more then one client

import java.io.*;
import java.net.*;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.*;

public class MultithreadedServer extends Thread {

    private ServerSocketChannel ssChannel;

    private Thread tRunSer = new Thread(this, "ServerSelectThread");

    public static void main(String argv[]) throws Exception {
        new MultithreadedServer();
    }

    public MultithreadedServer() throws Exception {
        this.start();
    }

    public void run() {
        while (true) {
            try {

                ssChannel = ServerSocketChannel.open();
                ssChannel.configureBlocking(false);
                int port = 2345;
                ssChannel.socket().bind(new InetSocketAddress(port));
            } catch (Exception e) {
            }
        }
    }
}

class Connect extends Thread {
    private ServerSocketChannel ssChannel;
    private SimManager SM;
    private BallState BS = new BallState(10, 5);

    public Connect(ServerSocketChannel ssChannel) {
        this.ssChannel = ssChannel;

        SM = new SimManager(BS);
        SM.start();
    }

    public void run() {
        try {
            SocketChannel sChannel = ssChannel.accept();
            while (true) {
                ObjectOutputStream oos = new ObjectOutputStream(sChannel
                        .socket().getOutputStream());
                oos.writeObject(BS);
                System.out.println("Sending String is: '" + BS.X + "'" + BS.Y);
                oos.flush();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

my intention is to send the objects on network.

please help

new code:

import java.io.*;
import java.net.*;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.*;

public class MultithreadedServer extends Thread {

    private ServerSocketChannel ssChannel;
    private SimManager SM;
    private BallState BS = new BallState(10, 5);
    private Thread tRunSer = new Thread(this, "ServerSelectThread");

    public static void main(String argv[]) throws Exception {
        new MultithreadedServer();
    }

    public MultithreadedServer() throws Exception {
        this.start();
    }

    public void run() {
        // create the server socket once
        try {
            ssChannel = ServerSocketChannel.open();
            ssChannel.configureBlocking(false);
            ssChannel.socket().bind(new InetSocketAddress(2345));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        while (true) {
            // accept new connections on the socket

            SocketChannel accept;
            try {
                accept = ssChannel.accept();
                ObjectOutputStream oos;
                oos = new ObjectOutputStream(accept.socket().getOutputStream());

                oos.writeObject(BS);
                System.out.println("Sending String is: '" + BS.X + "'" + BS.Y);
                oos.flush();

            }

            catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

Solution

  • You are creating a new server socket for each loop iteration (using the same port over and over). You must create the server socket only once, and then accept new incoming connections.

    Something like:

     public void run() {
        // create the server socket once
        ssChannel = ServerSocketChannel.open();
        ssChannel.configureBlocking(false);
        ssChannel.socket().bind(new InetSocketAddress(2345));
    
        while (true) {
            // accept new connections on the socket
            try {
                SocketChannel accept = ssChannel.accept();
                System.out.println("new client: " + accept.getRemoteAddress());
            } catch (Exception e) {
                System.out.println("exception: " + e.getMessage());
            }
        }
    }