简体   繁体   English

从套接字nonblockin winsock2接收数据?

[英]receiving data from sockets nonblockin winsock2?

I am fairly new to cpp. 我对cpp很陌生。 I am wanting to be able to receive data via sock_stream. 我希望能够通过sock_stream接收数据。 I want to pass the received data from SocketInfo->DataBuf.buf to an char recvArray[1024] buffer initiallized at the start of the program. 我想将接收到的数据从SocketInfo-> DataBuf.buf传递到在程序开始时初始化的char recvArray [1024]缓冲区。 I need to be able to check/parse and assemble numerical values out of my recvArray[]. 我需要能够从recvArray []中检查/解析和组合数值。 How do I make this copy work? 如何制作此副本? A couple of things I have tried get a compiler error "invalid conversion from char* to char" 我尝试过的几件事得到了一个编译器错误“从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
}
//recieve data
SocketInfo->RecvBytes = sizeof(SocketInfo->DataBuf);
check = WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &(SocketInfo->RecvBytes), &Flags, NULL, NULL);

Try to declare a new variable of type WSABUF lets us say, 尝试声明一个类型为WSABUF的新变量,让我们说,

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

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

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