使用c中的套接字重新连接 [英] re-connection using socket in c

查看:32
本文介绍了使用c中的套接字重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c 中使用套接字并在发送/接收成功.然而,问题是当我的服务器崩溃时,客户端必须重新连接服务器(服务器在一段时间后运行).所以我做了什么:

im using socket in c and got success on send/recv. however, the problem is when my server crashes, the client has to reconnect the server (server runs after some time). so here what i did:

  1. 创建套接字
  2. 连接
  3. 接收/发送
    • 如果接收大小 == 0转到第 2 步.

这个算法对我不起作用.有什么想法吗?

this algorithm is not working for me. is there any ideas?

代码:

int initSocket(char *servIP, unsigned short serverPort)
{
    portNum = serverPort;
    ipAddr = servIP;
                            /* Socket descriptor */
    struct sockaddr_in echoServAddr; /* Echo server address */

    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
        printf("socket() failed");
        bConnected = -1;
        return -1;
    }

    /* Construct the server address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
    echoServAddr.sin_family      = AF_INET;             /* Internet address family */
    echoServAddr.sin_addr.s_addr = inet_addr(ipAddr);   /* Server IP address */
    echoServAddr.sin_port        = htons(portNum);      /* Server port */

    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 20000;

    if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
        error("setsockopt failed\n");

    /*if (setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
         error("setsockopt failed\n");
    */
    /* Establish the connection to the echo server */
    if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
    {
        printf("connect() failed\n");
        bConnected = -1;
        //close(sock);
        return -1;
    }

    bConnected = 0;
    return 0;
}




--------------- if server crashes ------------
if(recv_size == 0)
            {
            // server crashed
                while(initSocket(ipAddr, portNum) < 0)
                {
                    printf("IP : %s\v", ipAddr);
                    printf("Port : %d\v", portNum);                 
                }

            } 
-----------------------------

推荐答案

  1. 创建套接字
  2. 连接
  3. recv/send if recv size == 0 转到第 2 步.

您无法重新连接 TCP 套接字,您必须创建一个新套接字.您还需要处理接收或发送错误的情况.

You cannot re-connect a TCP socket, you have to create a new one. You'd also want to handle the case where recv or send errors.

所以这必须是:

  1. 创建套接字
  2. 连接
  3. 接收/发送如果 recv 返回 <= 0 或 send 返回 -1(并且不是超时):关闭套接字,转到步骤 1

不过,您的代码似乎已经完成了所有这些步骤,而不仅仅是重复第 2 步和第 3 步,因此有点不清楚您观察到的实际问题是什么.

Though, it seems your code already does all these steps, and not just repeating step 2 and 3, so it's a bit unclear what's the actual problem you are observing.

此外,您的 initSocket() 代码在 connect() 失败时不会关闭 () 套接字,因此一旦服务器出现故障,您将很容易泄漏套接字并在不到一秒的时间内耗尽文件描述符,如果你不打算使用它,你必须 close() 刚刚创建的套接字.

In addition, your initSocket() code does not close() the socket when connect() fails, so you'll easily leak sockets and run out of file descriptors in less than a second once the server fails, you have to close() the socket you just created if you're not going to use it.

这篇关于使用c中的套接字重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆