简体   繁体   English

TCP。 客户端即使服务器不接受也可以连接

[英]TCP. client connects even if server doesn't accept him

I have TCP server-client application. 我有TCP服务器客户端应用程序。 It works but sometime something happens. 它可以工作,但有时会发生一些事情。 Client connects to server but server says he doesn't accepted him. 客户端连接到服务器,但服务器说他不接受他。

Server side code: 服务器端代码:

while(!stopped){
            try {
                AcceptClient();
            } catch(SocketTimeoutException ex){
                continue;
            } catch (IOException ex) {
                System.err.println("AppServer: Client cannot be accepted.\n"+ex.getMessage()+"\n");
                break;
            }
...

private void AcceptClient() throws IOException {
    clientSocket = serverSocket.accept();
    clientSocket.setSoTimeout(200);
    out = new ObjectOutputStream(clientSocket.getOutputStream());
    in = new ObjectInputStream(clientSocket.getInputStream());
    System.out.println("Accepted connection from "+clientSocket.getInetAddress());
}

Client side code: 客户端代码:

    try {
        socket = new Socket(IPAddress, serverPort);
        socket.setSoTimeout(5000);
        out = new ObjectOutputStream(socket.getOutputStream());
        in = new ObjectInputStream(socket.getInputStream());
    } catch (IOException e1) {
        sendSystemMessage("DISCONNECTED");
        sendSystemMessage(e1.getMessage());
        return;
    }
    sendSystemMessage("CONNECTED");

If client connects the message: 如果客户端连接消息:

Accepted connection from ... appears. 出现从...接受的连接。 But sometimes it doesn't appear even if client sends message "CONNECTED" 但是有时即使客户发送消息“ CONNECTED”也不会出现

Server is still runing the loop trying to get client and it is catching socketTimeoutException. 服务器仍在尝试获取客户端的循环中运行,并且正在捕获socketTimeoutException。 Client is connected, sends message and waits for response. 客户端已连接,发送消息并等待响应。

I suspect a missing 'flush' inside your client's 'sendSystemMessage()'. 我怀疑客户端的“ sendSystemMessage()”中缺少“刷新”。 Unfortunately the constructor of ObjectInputStream attempts to read a header from the underlying stream (which is not very intuitive IMHO). 不幸的是,ObjectInputStream的构造方法试图从基础流中读取标头(这不是很直观的恕我直言)。 So if the client fails to flush the data - the server may remain stuck on the line "in = new ObjectInputStream(socket.getInputStream())"... 因此,如果客户端无法刷新数据-服务器可能会停留在“ in = new ObjectInputStream(socket.getInputStream())”行中...

As a side note it's usually better for a server to launch a thread per incoming client, but that's just a side remark (plus it obviously depends on requirements). 附带说明一下,通常最好由服务器为每个传入的客户端启动线程,但这只是附带说明(此外,它显然取决于要求)。

I found the problem. 我发现了问题。 The communication on my net is too slow so it timeouts in getting inputstream. 我的网络上的通信速度太慢,因此在获取输入流时超时。 The solution has two parts. 解决方案包括两个部分。 Flushing outputstream before getting inputstream. 在获取输入流之前冲洗输出流。 And set socket timout after streams are initialized. 并在流初始化后设置套接字timout。

serverside: 服务器端:

clientSocket = serverSocket.accept();
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.flush()
in = new ObjectInputStream(clientSocket.getInputStream());
clientSocket.setSoTimeout(200);

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

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