сервер не принимает более одного клиента в nio

Я пытаюсь создать приложение для чата. У меня есть код, который отправляет данные от клиента на сервер. когда один или несколько клиентов входят в систему (когда клиентская программа запускается один или несколько раз). сервер не будет принимать остальные подключения, кроме первого подключения. пожалуйста, помогите мне решить это вот мой код:

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

Код клиента:

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();
}
}
5
задан alaster 19 June 2012 в 14:51
поделиться