DEVC++中使用winsock2进行TCP编程的客户端无法接收到服务器返回数据的问题

在客户端输入两个数字,发送到服务器端,服务器接收后进行在做和运算,将结果返回给客户端,但现在无法接收到返回数据。客服端连接是没有问题的。
代码如下:

服务端
SOCKET  AcceptSocket;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);

AcceptSocket = accept( s,(SOCKADDR*) &remoteAddr,&nAddrlen);
if (AcceptSocket == INVALID_SOCKET)
{
    cout<<"Accept socket fali!"<<endl;
    ::closesocket(s);
    ::WSACleanup( );
    return 1;
}

//6.收发数据
//退出标记
BOOL  QuitFlag = FALSE;

while(!QuitFlag)
{
    //接收数据
    int Buffer[1024]={0};
    //char Buf[255];
    nRetuen = ::recv(AcceptSocket,(char*)&Buffer,1024,QuitFlag);

    //判断情况
    switch (nRetuen)
    {
    case 0:
        {
            //::closesocket(AcceptSocket);
            //::closesocket(s);
            //::WSACleanup( );
            cout << "client's connection is closed!" << endl;
        }
        break;
    case SOCKET_ERROR :
        {
            //::closesocket(AcceptSocket);
            //::closesocket(s);
            //::WSACleanup( );
            cout << "server's connection is closed!" << endl;
            break;
        }

    default:
        {
            if (Buffer[0] == 0  &&  Buffer[1] == 0)
            {
                QuitFlag = TRUE;
            }
            else
            {
                int result = Buffer[0] + Buffer[1];
                cout <<Buffer[0]  <<" + "<<Buffer[1] <<" = " <<result <<endl;
                //发送数据
                send(s,(char*)&result,1024,0);
                cout<<"send success!"<<endl;

            }

        }
    }

}



客户端
    int data[1024] = {0};
while (1)
{
    cin >> data[0] >> data[1];
    fflush( stdin );

    nRetuen =  ::send(sockClient,(char*)&data,1024,0);
    int error = 0;
    if (nRetuen == SOCKET_ERROR)
    {
        error = WSAGetLastError();
        cout<<"send fail!"<<endl;
    }

    if (data[0] == 0  && data[1] == 0)
    {
        cout << " Client will  quit!" <<endl;
        break;
    }
    //send(sockClient,(char*)&data[2],sizeof(data[2]),0);
    //5.接收数据
    int sum=0;
    int recvbuffer[1024]={0};
    memset(recvbuffer,0,1024);
    nRetuen = ::recv(sockClient,(char*)&recvbuffer,1024,0);

    if(nRetuen > 0)
    {
        sum = (int*)&recvbuffer;
        cout << data[0] << " + " << data[1] << " = " << sum;;
    }
    else if (nRetuen == 0)
    {
        error = WSAGetLastError();
        cout<<"receive fail!"<<endl;
    }
    else
    {
    cout << "SOCKET_ERROR"<<endl;
    }
}

客户端

c++

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!