简体   繁体   English

服务器不接受nio中的一个客户端

[英]server not accepting morethan one client in nio

Am trying to build a chat application.i have a code that sends the data from client to server. 我正在尝试构建一个聊天应用程序。我有一个代码将数据从客户端发送到服务器。 when one or more client login(when the client program runs one or more time).server will not accepting the rest of connection other than first connected. 当一个或多个客户端登录时(当客户端程序运行一次或多次时).server将不接受除首次连接之外的其余连接。 please help me to resolve this here is my code: 请帮我解决这个问题,这是我的代码:

public class Server
{

//Creating non blocking socket

public void non_Socket() throws Exception {

    ServerSocketChannel ssChannel = ServerSocketChannel.open();
    int port = 80;
    int i=0;
    ssChannel.socket().bind(new InetSocketAddress(port));
    ssChannel.configureBlocking(false);
    while(true)
    {
        SocketChannel sc = ssChannel.accept();`

        if (sc == null) 
        {
            System.out.println("Socket channel is null");
            Thread.sleep(5000);
        }
        else 
        {
            System.out.println("Socket channel is not null");
            System.out.println("Received an incoming connection from " +
                    sc.socket().getRemoteSocketAddress()); 
            new PrintRequest(sc,i).start(); 
            i++;
        }
    }
}

public static void main(String [] abc) throws Exception
{
    new Server().non_Socket();
}
}

class PrintRequest extends Thread {

public  PrintRequest(SocketChannel sc,int i) throws Exception
{
    WritableByteChannel wbc = Channels.newChannel(System.out); 
    ByteBuffer b = ByteBuffer.allocateDirect(1024); // read 1024 bytes 
    int numBytesRead = sc.read(b);

    while (numBytesRead != -1) 
    {
        b.flip();

        while (b.hasRemaining())
        { 
            wbc.write(b);
            System.out.println();
            //System.out.println("Stream  "+i);
            // System.out.println("  KKK   "+b.toString());
        }
        //b.clear();
    }    
}
}

Client code: 客户代码:

public class Client extends Thread {

public void non_Client_Socket() throws Exception
{
    SocketChannel sChannel = SocketChannel.open();
    sChannel.configureBlocking(false);
    sChannel.connect(new InetSocketAddress("localhost", 80));
    while (!sChannel.finishConnect())
    {
        System.out.println("Channel is not connected yet");
    }

    System.out.println("Channel is ready to use");

    /* ----------  going to send data to server ------------*/   
    System.out.println("please enter the text");
    BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
    while(true)
    {
        System.out.println("Enter the text");
        String HELLO_REQUEST =stdin.readLine().toString();
        if(HELLO_REQUEST.equalsIgnoreCase("end"))
        {
            break;
        }

        System.out.println("Sending a request to HelloServer");    
        ByteBuffer buffer = ByteBuffer.wrap(HELLO_REQUEST.getBytes());    
        sChannel.write(buffer); 
     }
}
     /* ----------  the data is written to sChannel server
                      will read from this channel  ------------   */

public static void main(String [] args) throws Exception
{
    new Client().non_Client_Socket();
}
}

There are numerous issues here. 这里有很多问题。

  1. You are putting your ServerSocketChannel into non-blocking mode and then calling accept() without using a Selector. 您将ServerSocketChannel置于非阻塞模式,然后在不使用Selector的情况下调用accept() That means 99.9999% of the time accept() will return null, so you are burning CPU cycles. 这意味着99.9999%的时间accept()将返回null,因此您正在燃烧CPU周期。 This is pointless. 这毫无意义。 Either accept in blocking mode or use a Selector. 在阻止模式下接受或使用选择器。

  2. You are putting your client SocketChannel into non-blocking mode, calling connect(), and the calling finishConnect() without using a Selector. 您将客户端SocketChannel置于非阻塞模式,调用connect(),和调用finishConnect()而不使用Selector。 That means 99% of the time finishConnect() will return false, so you are burning CPU cycles. 这意味着99%的时间finishConnect()将返回false,因此您正在燃烧CPU周期。 This is pointless. 这毫无意义。 Either connect in blocking mode or use a Selector. 要么以阻塞模式连接,要么使用选择器。

  3. You are ignoring the result of SocketChannel.write() . 您忽略了SocketChannel.write()的结果。 You can't do that. 你不能这样做。 It returns information you need to know about. 它返回您需要了解的信息。

In short, your code doesn't make much sense. 简而言之,您的代码没有多大意义。

I don't have time to look into your code in detail, but some initial observations: 我没有时间仔细研究你的代码,但是有一些初步的观察:

  1. When using NIO, I suggest you use Selector (as I suggested in your previous question ) instead of one thread per client. 使用NIO时,我建议您使用Selector (我在上一个问题中的建议)而不是每个客户端一个线程。

  2. Remember to bind each client in order to allow the server socket to accept new connections. 请记住bind每个客户端以允许服务器套接字accept新连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM