套接字编程中的错误10054

时间:2015-07-06 09:46:31

标签: c sockets

我正在用C编写套接字编程代码,我在同一台计算机上运行客户端和服务器。这是服务器的代码

  int main()
{
    WSADATA wsa;
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        cout << "WSAStartup failed!" << endl;
        return -1;
    }

    SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == INVALID_SOCKET)
    {
        cout << "socket failed with error:" << WSAGetLastError() << endl;

        WSACleanup();
        return -1;
    }

    struct sockaddr_in service;
    service.sin_addr.s_addr = inet_addr("127.0.0.1");
    service.sin_family = AF_INET;
    service.sin_port = htons(5059);

    if (bind(sock, (SOCKADDR *)&service, sizeof(service)) == SOCKET_ERROR)
    {
        cout << "bind failed with error:" << WSAGetLastError() << endl;

        closesocket(sock);
        WSACleanup();
        return -1;
    }

    if (listen(sock, 5) == SOCKET_ERROR)
    {
        cout << "listen failed with error:" << WSAGetLastError() << endl;

        closesocket(sock);
        WSACleanup();
        return -1;
    }
    while (true)
    {
        cout << "Waiting for a PKCS11 client ..." << endl;

        SOCKET accsock = accept(sock, NULL, NULL);
        if (accsock == INVALID_SOCKET)
        {
            cout << "accept failed with error:" << WSAGetLastError() << endl;

            closesocket(sock);
            WSACleanup();
            return -1;
        }

        cout << "PKCS11 client connected ..." << endl;

        while (true)
        {
            int receive_size = 0;
            char test[20];
            receive_size = recv(accsock, test, 20, 0);
            if (receive_size == SOCKET_ERROR)
            {
                cout << endl << "ERROR : "<<WSAGetLastError() << " PKCS11 client disconnected." << endl;
                closesocket(accsock);
                break;
            }

            test[receive_size] = '\0';
            command = temp;
            printf("recieved value is : %s\n",test);
          }
            }

这是客户端的代码

int main(){
    /*CK_SESSION_HANDLE hSession = NULL;
    CK_MECHANISM_PTR pMechanism = NULL;
    CK_OBJECT_HANDLE hKey = NULL;
    C_EncryptInit(hSession, pMechanism, hKey);*/

    char temp[10];
    printf("initialising ... \n");
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        printf("Error = :%d", WSAGetLastError());
        WSACleanup();
        return -1;
    }
    printf("initialised\n");
    //create a socket
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d", WSAGetLastError());
        WSACleanup();
        return -1;
    }

    printf("Socket created.\n");


    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons(5059);

    //Connect to remote server
    if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected");


    _itoa(CK_ENCRYPT, temp, 16);
    command = temp;
    int counter = 0;
    char* test = "H";
    while (counter != strlen(test)) {
    counter += send(s, test, strlen(test), 0);
    cout << counter << endl;
}   


    //while (1);
    return 0;
}

问题在于,当我尝试从客户端发送多个字节到服务器时,我得到10054错误,除非我取消注释while(1)行,在这种情况下我在服务器中正确地接受按摩。任何人都有想法这个代码有什么问题?

1 个答案:

答案 0 :(得分:3)

您的客户端正在发送数据,该数据将数据放入IP堆栈以进行发送。然后立即终止,这意味着IP堆栈将取消发送和非正常关闭套接字。

在终止客户端之前,您需要shutdownclosesocket套接字。