简体   繁体   English

C ++ TCP Winsock Server多次接收相同的数据包

[英]C++ TCP Winsock Server receiving the same packet many times

I have written a very simple C++ server which I am connecting to from a Java application. 我编写了一个非常简单的C ++服务器,我从Java应用程序连接到该服务器。 The C++ server uses winsock2. C ++服务器使用winsock2。 I am sending UTF8 encoded numbers to the server from my client and on receipt of these numbers I would like the server to perform an action. 我从我的客户端向服务器发送UTF8编码的号码,并且在收到这些号码后,我希望服务器执行操作。 However my server seems to be receiving a series of numbers as one. 但是我的服务器似乎收到了一系列数字。 At the moment I have the server listening every 1 millisecond for a new message. 目前,我让服务器每1毫秒监听一条新消息。

This is my C++ server code which receives the message: 这是我的C ++服务器代码,它接收消息:

  bool receive()
     {
          char buffer[1024];
          int inDataLength=recv(Socket,buffer,sizeof(buffer),0);
          if(buffer[0] != '\0')
          {
                     std::cout<<"Client: ";
                     std::cout << buffer;
                     sendKey(string(buffer));  
          }
          else if (inDataLength == 0) //Properly closed connection
          {
             std::cout<<"Connection lost..\r\n";
             return false;
          }    
          return true;
     }

This is called within a loop like so: 这在一个循环内调用,如下所示:

 while ( receive() )
{
      Sleep(1);
}

This is my java client code to send a message where out is OutputStream = socket.getOutputStream(): 这是我发送消息的java客户端代码,其中out是OutputStream = socket.getOutputStream():

public void send(String msg)
{
        try {
            out.write( msg.getBytes("UTF8") );
            out.flush();
            Thread.sleep(100);
        } catch (SocketException e) {
            Global.error("Connection error..");
            //e.printStackTrace();
        } catch (UnsupportedEncodingException e) {

        } catch (IOException e) {
            Global.error("Never connected..\r\n");
        } catch (Exception e)
        { Global.error("Sending failed..\r\n"); }
    }

What I am getting is the server receiving for example the number 1, then 2, then 12, then 121 etc.. in no specific pattern except once the server is receiving 2 numbers at once it will never start receiving only one again. 我得到的是服务器接收例如数字1,然后是2,然后是12,然后是121等...没有特定的模式,除非一旦服务器一次接收2个数字,它将永远不会再开始只接收一个。 This is the only place in my java code where anything is sent to the server and I flush the buffer after each message so I think the issue is on my server but I'm at a loss as to the problem. 这是我的java代码中唯一发送到服务器的地方,我在每条消息后刷新缓冲区,所以我认为问题出在我的服务器上,但我对这个问题感到茫然。

Any help would be much appreciated. 任何帮助将非常感激。

Thanks. 谢谢。

You are forgetting the most important check: 你忘记了最重要的检查:

int inDataLength=recv(Socket,buffer,sizeof(buffer),0);
if (inDataLength == -1 ) {
    std::cerr << "receive error: " << GetLastError() << std::endl;
    return false;
}
...

This actually might be the reason your loop took so much CPU time. 这实际上可能是你的循环花了这么多CPU时间的原因。

Given the sleeps it seems as the recv is not blocking.. Take a look at Winsock recv() does not block . 鉴于睡眠似乎recv没有阻塞。看看Winsock recv()没有阻止 You need to check for errors in the return value. 您需要检查返回值中的错误。

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

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