winsock2客户端无法向服务器

时间:2016-05-17 07:33:19

标签: c++ sockets winsock2

我正在尝试让我的客户端连接到服务器(包括C ++和winsock2)并进行身份验证但总是失败。服务器似乎没问题,因为我可以telnet到服务器并手动输入密码(即888)并从服务器获取响应。 telnet会话如下所示:

telnet works fine with server

我通过互联网搜索send和recv的用法,但他们只是不在这里工作。我也试图追加" \ r"或" \ n"密码后也无法正常工作。 这是服务器代码:

strcpy_s(sendBuff, "200 Welcome.\r\n");
send(client, sendBuff, strlen(sendBuff), 0);
while (true)
{
    memset(rcvBuffer, 0,1024);
    input="";

    int n;
    while ((n = recv(client, rcvBuffer, sizeof(rcvBuffer),0))>0)   <<- server keeps waiting here
    {
        if(strchr(rcvBuffer, '\n') != 0)
            break;
        if (n>0)
            input.append(rcvBuffer, n);
    }
    if (n == SOCKET_ERROR) {
        cout << "Detect client disconnection" << endl;
        break;
    }
    cout <<"Got message from client["<< input << "] sizeof(input) is" <<input.size()<< endl;

    if (ParseCmd(input.c_str(), cmd, params)){
        if (cmd == "QUIT"||cmd=="quit")
            break;
        if (cmd == "AUTH"||cmd=="auth"){
            for (struct_user u : user) {
                if (params == u.passwd){
                    this_user=u;
                    auth = true;
                    strcpy_s(sendBuff, "200 You are logged in.\r\n");
                    send(client, sendBuff, strlen(sendBuff), 0);
                }
            }
            if (auth != true) {
                strcpy_s(sendBuff, "401 Bad password.\r\n");
                send(client, sendBuff, strlen(sendBuff), 0);
                cout << "Bad Password" << endl;
            }
        }
        if (cmd == "gethsi"||cmd=="GETHSI"){
... some codes here
        }           // end of gethsi

    }
    else
    {
        strcpy_s(sendBuff, "400 Invalid command.\r\n");
        send(client, sendBuff, strlen(sendBuff), 0);
        cout << "Invalid command" << endl;
    }

以下是客户端代码:

memset(rcvbuff, 0, 512);
rcvResult = recv(ConnectSocket, rcvbuff, 512, 0);               //Get welcome message
rcvbuff[rcvResult] = 0;
cout << "rcvbuf [welcome] " << rcvbuff << endl;

if (rcvbuff[0] == '2'&&rcvbuff[1] == '0'&&rcvbuff[2] == '0') {
    memset(sendbuff, 0, 512);
    strcpy_s(sendbuff, "auth 888");
    sendResult = send(ConnectSocket, sendbuff, strlen(sendbuff), 0);
    if (sendResult == SOCKET_ERROR) {
        std::printf("send failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    std::printf("Bytes Sent: %ld\n", sendResult);

    memset(rcvbuff, 0, 512);
    rcvResult = recv(ConnectSocket, rcvbuff, 512, 0);    <<-- keep waiting here
    rcvbuff[rcvResult] = 0;
    cout << "rcvbuf [authenticate] " << rcvbuff << endl;

客户已收到&#34; 200欢迎&#34;消息并发送&#34; auth 888&#34;消息并继续等待&#34; rcvResult = recv(ConnectSocket,rcvbuff,512,0);&#34;因为它没有从服务器收到任何东西。服务器正在等待输入的recv命令中等待。

我还在考虑追加&#34; \ r \ n&#34;在&#34; auth 888&#34;之后但很奇怪的事情发生了。见下图。上半部分是客户端,下半部分是服务器:

After appending "\r\n" to send string

基本上,服务器只能从客户端接收空字符串,因为大小为0.

我花了一整天时间,无法弄清楚为什么,希望有人可以在这里提供帮助。非常感谢你提前。

P.S。我没有足够的声誉来发布图片。希望有人可以帮我发布。谢谢!

纳尔逊

0 个答案:

没有答案