【问题标题】:receiving data from sockets nonblockin winsock2?从套接字非阻塞winsock2接收数据?
【发布时间】:2012-12-08 17:55:49
【问题描述】:

我对 cpp 还很陌生。 我希望能够通过 sock_stream 接收数据。我想将接收到的数据从 SocketInfo->DataBuf.buf 传递到在程序开始时初始化的 char recvArray[1024] 缓冲区。我需要能够从我的 recvArray[] 中检查/解析和组合数值。我如何使这个副本工作?我尝试过的几件事得到了编译器错误“从 char* 到 char 的无效转换”

//check sockets for for read or write notification
for(int i=0; Total>0 && i<TotalSockets; i++){
    LPSOCKET_INFORMATION SocketInfo = SocketList[i];

    //check if data incomming on each socket
    if( FD_ISSET(SocketInfo->Socket, &Reader)){
        Total--;
        SocketInfo->DataBuf.buf = SocketInfo->Buffer;
        SocketInfo->DataBuf.len = BUFFERSIZE;
        Flags = 0;
        //receive data
        SocketInfo->RecvBytes = sizeof(SocketInfo->DataBuf);
        check = WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &(SocketInfo->RecvBytes), &Flags, NULL, NULL);
        //error checking
        if( check == SOCKET_ERROR){
            if( WSAGetLastError() != WSAEWOULDBLOCK){
                printf("base station client receive failed with error %d \n", WSAGetLastError());
                FreeSocketInformation(i); //shuts down client socket if no data
            }
            continue;
        }
        else{
            //receive bytes into array

            //print received bytes on screen
            printf("%s \n", SocketInfo->DataBuf.buf);
            //otherwise deal with data
            //need to copy to recvArray

            //if no bytes received then connection is closed.
            if( SocketInfo->RecvBytes == 0){
                FreeSocketInformation(i); //shuts down client socket if no data
                continue;
            }
        }
    } //end of read data from socket
}

【问题讨论】:

    标签: c++ arrays winsock


    【解决方案1】:
    //recieve data
    SocketInfo->RecvBytes = sizeof(SocketInfo->DataBuf);
    check = WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &(SocketInfo->RecvBytes), &Flags, NULL, NULL);
    

    尝试声明一个 WSABUF 类型的新变量让我们说,

    WSABUF tempDataBuf;
    check = WSARecv(SocketInfo->Socket, &(tempDataBuf), 1, &(SocketInfo->RecvBytes), &Flags, NULL, NULL);
     SocketInfo->DataBuf = tempDataBuf;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 2013-10-15
      • 2019-07-10
      • 1970-01-01
      • 2014-11-01
      相关资源
      最近更新 更多