一客户端程序采用多线程,每个线程都是单独的完成connect, send, recv,且是阻塞模式,现问题是客户端设置recv接收2K,而服务器返回的数据(大小不确定)如果小于2K,那会什么什么情况?客户端难道就一直等待吗?

解决方案 »

  1.   

    客户端设置recv接收2K////////////////////////////////////////2k表示用户缓冲区的大小,并不是说要等代有2k数据,recv才返回只要有数据到达,recv就会返回
      

  2.   

    用SOCKET API写程序的,
    估计没有人会在程序里直接使用API函数,
    都是经过打包的,
    直接调用RECV( ),等于自己找死。
    最起码也要用PEEK探测一下。
      

  3.   

    如果用TCP应该用考虑这样的问题吧
      

  4.   

    For connection-oriented sockets (type SOCK_STREAM for example), calling recv will return as much information as is currently available—up to the size of the buffer specified. If the socket has been configured for in-line reception of OOB data (socket option SO_OOBINLINE) and OOB data is yet unread, only OOB data will be returned. The application can use the ioctlsocket or WSAIoctl SIOCATMARK command to determine whether any more OOB data remains to be read.
      

  5.   

    int bytes;
    char buf[512];
    string response = "";do {
        bytes = recv(sock, buf, sizeof(buf)-1, 0);
        if (bytes == SOCKET_ERROR) {
            *errcode = WSAGetLastError();
            break;
        }
        response.append(buf, bytes);
    } while (bytes != 0);我用上面这个循环去recv,为何每次都会非法操作?
      

  6.   

    另外MSG_PEEK和MSG_OOB都分别表示什么意思呢?
      

  7.   

    ---------------------------
    Microsoft Visual C++ Runtime Library
    ---------------------------
    Runtime Error!Program: C:\Dev-Cpp\Examples\SofeeSpreader\Release\SofeeSpreader.exeThis application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
    用那个循环去读取就会出现以上错误!
      

  8.   

    recv第一次调用时接受到数据后不会等待,直接返回收到的长度。如果连续调用,会发生等待。
    我的做法是每次发送数据时先发送长度,接收时根据长度判断是否接收完毕