简体   繁体   English

Java Nio的简单客户端-服务器示例

[英]Simple client-server example with java nio

Please could you show me a small example in localhost of a client and a server that communicate using java nio? 请您给我看一个使用java nio进行通信的客户端和服务器的本地主机的小例子吗? I'd need to understand how it works java nio. 我需要了解它如何工作。 For example 1) client requires connection, client connects client sends "Hello i'm a client". 例如1)客户端需要连接,客户端连接客户端发送“你好,我是客户端”。 2) Server accepts, server receives message from client, Server responds "hello i'm a server" 3) Client sends "hello server" 4) Server sends "hello client". 2)服务器接受,服务器从客户端接收消息,服务器响应“你好,我是服务器” 3)客户端发送“你好服务器” 4)服务器发送“你好客户端”。

I do not need the server to handle multiple clients at a time, what I need to understand is how a connection with multiple messages between client and server happens. 我不需要服务器一次处理多个客户端,我需要了解的是客户端与服务器之间如何与多个消息建立连接。

Not really sure why you need to use nio if you are not going to handle multiple connections. 如果您不打算处理多个连接,则不确定是否需要使用nio。 I don't see a point in using them. 我看不出使用它们的意义。

Alright here goes: Let me know if this works. 好的,这里走了:让我知道这是否有效。

Server code: 服务器代码:

public class Server {

ServerSocket socket;
Socket listener;

public Server(int port) throws IOException {
    socket = new ServerSocket(port);
}


public void connect() throws IOException{
    listener = socket.accept();
}

public String read() throws IOException{

    byte[] temp = new byte[1024];

    int bytesRead = 0;

    try(InputStream input = listener.getInputStream()){
        bytesRead = input.read(temp);
    }

    return new String(temp,0,bytesRead,"ASCII");
}

public void write(String data) throws IOException{
    byte[] temp = new byte[1024];

    try(OutputStream out = listener.getOutputStream()){
        out.write(data.getBytes());
        out.flush();
    }
}

public void close(){
    socket.close();
}

}

Client code: 客户代码:

public class Client{

Socket client;

InetSocketAddress addr;

public Client(String ip, int port) throws IOException{

    client = new Socket();
    addr = new InetSocketAddress(ip,port);

}


public void connect() throws IOException{
    client.connect(addr);
}

public String read() throws IOException{

    byte[] temp = new byte[1024];

    int bytesRead = 0;

    try(InputStream input = client.getInputStream()){
        bytesRead = input.read(temp);
    }

    return new String(temp,0,bytesRead,"ASCII");
}

public void write(String data) throws IOException{
    byte[] temp = new byte[1024];

    try(OutputStream out = client.getOutputStream()){
        out.write(data.getBytes());
        out.flush();
    }
}

public void close(){
    client.close();
}

}

Now all you have to do is call connect() on the server, then connect() on the client and write and send messages that you want. 现在,您要做的就是在服务器上调用connect(),然后在客户端上调用connect()并编写和发送所需的消息。

Don't forget to call close after you are all done. 完成所有操作后,请不要忘记关闭。

Also be aware that you are going to need some mechanism to tell the server and client how long each messages are going to be. 还请注意,您将需要某种机制来告诉服务器和客户端每条消息将持续多长时间。 Or you can specify an ending character that tells the client/server that the message is over. 或者,您可以指定一个结束字符,告诉客户端/服务器消息已结束。

One send in the server does not necessarily equal one read in the client and vice versa. 服务器中的一次发送不一定等于客户端中的一次发送,反之亦然。 You are going to have to figure out what to do. 您将必须弄清楚该怎么做。

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

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