JavaNIO多线程服务器无法正常工作

时间:2011-04-16 13:47:55

标签: java debugging nio

你好专家
有人可以用这个服务器来识别问题,为什么这个服务器无法连接多个客户端

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

}

我的意图是在网络上发送对象。

请帮助

新代码:

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

        }
    }
}

3 个答案:

答案 0 :(得分:1)

您正在为每个循环迭代创建一个新的服务器套接字(反复使用相同的端口)。您必须只创建一次服务器套接字,然后接受新的传入连接。

类似的东西:

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

答案 1 :(得分:0)

如果你在catch区块里放了一些东西,你可能会自己找到它。 (e.printStackTrace()可能暂时有用)。

答案 2 :(得分:0)

以下是您的NPE的原因:

  

如果此通道处于非阻塞模式,则此方法将立即返回null if   没有待处理的连接。

这是ServerSocketChannel.accept()

您的接听电话会返回null,然后您尝试在此null个对象上调用方法。