linux 两次 connect()

就是有时间研究这个。
一个已经 connect() 成功的 fd 再次 connect 会怎么样呢?撸代码试一下就好了。

#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>


int main()
{
    const char *addr = "127.0.0.1";
    unsigned short port = 6379;
    struct sockaddr_in sa;
    int s = -1;

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        return -1;
    }

    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);

    if (inet_aton(addr, &sa.sin_addr) == 0)
    {
        return -1;
     }
    //第一次链接
    if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1)
    {
        perror("error:");
    }else
    {
        printf("ok\n");
    }

    //在一次链接
    if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1)
    {
        printf("errno=%d\n", errno );
        perror("error:");
    }else
    {
        printf("ok\n");
    }
    close( s );
    return 0;
}

运行结果:
yuhaiyang-Aspire-4750:~/qt/build-doubleConnect-Desktop-Debug$ ./doubleConnect
ok
errno=106
error:: Transport endpoint is already connected

查头文件 errno = 106

#define EISCONN  106 /* Transport endpoint is already connected */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值