开发者

JavaNIO multithreaded server not working

开发者 https://www.devze.com 2023-02-25 19:14 出处:网络
Hello Experts can somebody please indentify the problem with this server why this is unable to connect more then one client

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 开发者_JAVA技巧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();
            }

        }
    }
}


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());
        }
    }
}


If you put something in your catch block you will probably find it yourself. (e.printStackTracer() might help for the time being).


Here is the reason for your NPE:

If this channel is in non-blocking mode then this method will immediately return null if there are no pending connections.

This is from ServerSocketChannel.accept().

Your accept call returns null, and you then try to call a method on this null object.

0

精彩评论

暂无评论...
验证码 换一张
取 消