两次连接失败后,套接字功能失败

时间:2014-04-25 06:40:12

标签: c sockets websocket

while(1)
{
 if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
    {
        cout<<"Failed to obtain socket descriptor";
        continue;
    }

    /* Try to connect the remote */
    if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1)
    {
        cout << "Failed to Connect with Server\n";
        closesocket(sockfd);
        WSACleanup();
        continue;
    }
 recv(sockfd, revbuf, LENGTH, 0);
 closesocket(sockfd);
 WSACleanup();
 sleep(1000);
}

在上面的代码中,连接函数失败两次后套接字函数失败。由于我在连接失败后关闭套接字,我的套接字功能应该一直正常工作吗?任何人都可以解释为什么套接字功能失败了吗?

0 个答案:

没有答案