SocketChannel 客户端-服务器

SocketChannel Client-Server

客户端连接到服务器,然后进入下面的循环。 它从控制台获取一行输入,并将其发送到服务器。 如果输入行是单个“x”字符,则客户端退出。

服务器

首先,它将自己的套接字地址打印到屏幕上。当它接收到连接时,它会将对端的套接字地址打印到屏幕上(以便您可以检查谁连接到服务器),然后进入以下循环。它从客户端接收一行消息,将接收到的消息打印到屏幕上。

非阻塞模式

客户端应使用缓冲区和非阻塞套接字通道。

阻塞模式

服务器应使用缓冲区和阻塞套接字通道。

问题

我遇到的问题是服务器端的缓冲区没有刷新,而是用新输入打印旧输入。示例:

Message received: hello

Message received: helloworld

Message received: helloworldthis

Message received: helloworldthisis

Message received: helloworldthisis Edwin

And it should be printing: hello

world

this

is

Edwin.

这是服务器源代码:

public class EchoServer_1 
{
    //  private static CharBuffer buffer = CharBuffer.allocate(1024);
    private static ByteBuffer buffer = ByteBuffer.allocate(1024);
    private static StringBuffer reqString = new StringBuffer();
    public static void main(String[] args) throws IOException
    {

        ServerSocketChannel serverSocketChannel = null;
        int serverPort = 10007;

        try{
            serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.socket().bind(new InetSocketAddress (serverPort));
            //Blocking mode
            serverSocketChannel.configureBlocking(true);

            System.out.println("Port number: " + serverPort);
            System.out.println("Waiting for connection ..");

            SocketChannel sc = serverSocketChannel.accept();

            System.out.println ("Incoming connection from: " 
                    + sc.socket().getRemoteSocketAddress( ));

            // Read message from client
            while (true)
            {   
                buffer.clear();
                sc.read(buffer);
                buffer.flip();
                while(buffer.hasRemaining()) {
                    char c = (char) buffer.get();
                    if (c == '\r' || c == '\n') break;
                    reqString.append(c);             
                }

                log("Message received: " + reqString);

            }
        }catch (IOException ex)
        {
            if (serverSocketChannel != null)
                serverSocketChannel.close();
            System.err.println("Client has disconneted");
        }
    }
    private static void log(String str) {
        System.out.println(str);
    }
}

这是客户端源代码:

public class EchoClient_1 
{

    private static Scanner stdIn = new Scanner(System.in);
    //  private static CharBuffer buffer = CharBuffer.allocate(1024);
    private static ByteBuffer buffer = ByteBuffer.allocate(256);

    public static void main(String[] args) throws IOException
    {
        String hostname = new String ("127.0.0.1");
        int port = 10007;

        try{
            InetSocketAddress address = new InetSocketAddress(hostname, port);
            SocketChannel sC = SocketChannel.open(address);
            sC.configureBlocking(false);

            log("Local address: " + hostname + " connecting to Server on port " + port
                    + "...");
            String userInput;

            System.out.println("Enter lines of characters: " );

            while ((userInput = stdIn.nextLine()) != null)
            {
                //Checks if user wants to exit
                if (userInput.equalsIgnoreCase("x")) 
                {

                    buffer.put (userInput.getBytes());
                    buffer.flip();
                    sC.write(buffer);
                    buffer.clear();
                    System.out.println("Closing ..");
                    sC.close();
                    break;
                }

                buffer.put (userInput.getBytes());
                buffer.flip();  
                sC.write(buffer);
                buffer.clear();                             
            }

            sC.close();

        }catch (IOException ex)
        {
        }
    }

    private static void log(String str) {
        System.out.println(str);
    }
}

您永远不会清除 reqString。您应该在登录后执行此操作。

在 "buffer.clear();" 之前插入一个新行

reqString.setLength(0);

喜欢:

     while (true)
                {   
                    reqString.setLength(0);

                    buffer.clear();
                    sc.read(buffer);
                    buffer.flip();
                    while(buffer.hasRemaining()) {
                        char c = (char) buffer.get();
                        if (c == '\r' || c == '\n') break;
                        reqString.append(c);             
                    }

                    log("Message received: " + reqString);

                }