2017-09-09 63 views
0

发送的所有数据我真的不知道这是否是某种延迟的网络,我的电脑,或其他什么,我很新的套接字编程,并已努力使这项工作了一段时间,实现一个简单的框架,以缓解我的课将来的项目我的工作,我在我有问题使用这些功能:recv()没有得到客户端使用send()

void TCPSocket::send(const std::string& message, int flags) { 

    if (isListening || !isConnected) { 
     throw ConnectionException("Can't send message from a socket that is not connected"); 
    } 

    int msgLength = message.length(); 
    int bytesLeft = msgLength; 
    int bytesSent; 
    const char* cMessage = message.c_str(); 

    while (bytesSent < msgLength) { 

     const char* cMessageLeft = cMessage + bytesSent; 

     int result = ::send(socketFd, cMessageLeft, bytesLeft, flags); 
     if (result == -1) { 
      throw ConnectionException("Could not send message. Error: " + std::string(strerror(errno))); 
     } 

     bytesLeft -= result; 
     bytesSent += result; 

    } 

} 

std::string TCPSocket::recv(unsigned int maxlen, int flags) { 

    if (isListening || !isConnected) { 
     throw ConnectionException("Can't receive message in a socket that is not connected"); 
    } 

    char buffer[maxlen+1]; 
    int result = ::recv(socketFd, buffer, maxlen, flags); 

    if (result == -1) { 
     throw ConnectionException("Could not receive message. Error: " + std::string(strerror(errno))); 
    } 
    if (result == 0) { 
     isConnected = false; 
     throw ClosedConnection("Client closed connection."); 
    } 
    buffer[result] = '\0'; 
    std::string message(buffer); 

    return message; 

} 

它的伟大工程只有一个消息,我收到和使用不同的可执行文件发送没有任何问题,但我已经尝试发送超过1条消息,我的问题开始,有时我得到服务器接收1消息,有时它没有得到,当我只添加一个很少printf()它收到他们所有人,请有人可以探讨对我来说为什么会发生这种情况?

客户端代码:

int main() { 
    TCPSocket cl1(0); 

    try { 
     cl1.connect("localhost", 1170); 
     for (int i = 0; i < 5; i++) { 
      //printf("Esperando 5s... "); 
      std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 
      //printf("Pronto!\n\n"); 

      cl1.send("Thank you!\n"); 
      //printf("Msg enviada\n\n"); 

     } 
     cl1.close(); 
    } 
    catch(std::exception &e) { 
     std::cout << e.what() << std::endl; 
    } 
} 

Server代码:

int main() { 
    TCPSocket sv1(0); 

    try { 
     sv1.bind(1170); 
     sv1.listen(); 
     TCPSocket client = sv1.accept(); 
     printf("Cliente conectado\n"); 
     try { 
      for (;;) { 
       //client.send("Welcome !\n"); 
       std::cout << client.recv(256) << std::endl; 
      } 
     } 
     catch (const ClosedConnection & x) { 
      printf("Connection closed\n"); 
     } 

    } 
    catch(std::exception &e) { 
     std::cout << e.what() << std::endl; 
    } 
} 

如果我取消客户端的代码printfs输出,所有数据被接收服务器上。

澄清,我以5秒的时间间隔发送消息,仍然recv()只读取第一个,它会阻止,直到client完成它的执行,从不读取应该在缓冲区中的其余消息。出于某种原因,在客户端代码上使用printfs会使应用程序正常运行。

+0

由设计。 TCP是流协议,不是消息协议。换句话说,您需要循环调用'recv',直到您收到您期望的所有数据。 – selbie

+0

@selbie我明白,但是为什么我只收到一次“谢谢”,当我清楚地发送了5次并且它们应该在缓冲区中?如果我发送5次真的很快,那么recv()会将它们全部返回到同一个字符串中,但这不是发生的事情。 –

+0

请发表[mcve] – xaxxon

回答

1

你似乎没有初始化bytesSent,所以它实际发送数据的次数似乎是不确定的。

int bytesSent = 0; 
+0

显然这真的是这个问题,我现在感觉很蠢,谢谢。 –

+1

感觉愚蠢对你有好处。或者至少对你来说,比感觉聪明和愚蠢更好。 – user4581301