TCP+IPv6 客户端 & 服务端 程序简例

本文提供了一个基于IPv6的TCP服务端及客户端程序示例代码,包括创建套接字、绑定地址、监听连接请求和服务端响应等关键步骤。

TCP+IPv6 客户端 & 服务端 程序简例

本文给出基于IPv6的TCP客户端以及服务端程序样例。

可以参考《TCP+IPv4 客户端 & 服务端 程序简例》进行对比。

服务端:

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

#define BACKLOG 16

int listen_fd = -1;

void sigINT(int signo);

int main()
{
    if (signal(SIGINT, sigINT) == SIG_ERR)
    {
        printf("set signal handler(SIGINT) error!!!\n");
        exit(1);
    }

    // socket
    if ( (listen_fd = socket(AF_INET6, SOCK_STREAM, 0)) < 0 )
    {
        printf("create socket error=%d(%s)!!!\n", errno, strerror(errno));
        exit(1);
    }

    // bind
    struct sockaddr_in6 server_addr;
    server_addr.sin6_family = AF_INET6; // IPv4
    server_addr.sin6_port = htons(12500); // Port
    server_addr.sin6_addr = in6addr_any; // IP
    if (bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        printf("socket bind error=%d(%s)!!!\n", errno, strerror(errno));
        exit(1);
    }

    // listen
    if (listen(listen_fd, BACKLOG) < 0)
    {
        printf("socket listen error=%d(%s)!!!\n", errno, strerror(errno));
        exit(1);
    }

    printf("server init ok, start to accept new connect...\n");

    while (1)
    {
        // accept
        int client_fd = accept(listen_fd, NULL, NULL);
        if (client_fd < 0)
        {
            printf("socket accept error=%d(%s)!!!\n", errno, strerror(errno));
            exit(1);
        }
        printf("accept one new connect(%d)!!!\n", client_fd);

        static const char *msg = "Hello, Client!\n";
        if (write(client_fd, msg, strlen(msg)) != strlen(msg))
        {
            printf("send msg to client error!!!\n");
        }
        close(client_fd);
    }
}

void sigINT(int signo)
{
    printf("catch SIGINT, quit...\n");
    close(listen_fd);
    exit(0);
}

客户端:

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

int main()
{
    int client_fd = socket(AF_INET6, SOCK_STREAM, 0);
    if (client_fd < 0)
    {
        printf("create socket error=%d(%s)!!!\n", errno, strerror(errno));
        exit(1);
    }

    struct sockaddr_in6 server_addr;
    server_addr.sin6_family = AF_INET6;
    server_addr.sin6_port = htons(12500);
    if (inet_pton(AF_INET6, "::1", &server_addr.sin6_addr) <= 0)
    {
        printf("inet_pton error!!!\n");
        exit(1);
    }

    if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        printf("socket connect error=%d(%s)!!!\n", errno, strerror(errno));
        exit(1);
    }
    printf("connect to server ok!\n");

    char msg[1024];
    int rbytes = -1;
    while ( (rbytes = read(client_fd, msg, sizeof(msg)-1)) > 0)
    {
        msg[rbytes] = 0; // null terminate
        printf("%s\n", msg);
    }
    if (rbytes < 0)
    {
        printf("read error=%d(%s)!!!\n", errno, strerror(errno));
    }
    exit(0);
}

服务端:编译 && 运行

[jiang@localhost 0406]$ gcc -o server server.c
[jiang@localhost 0406]$ ./server 
server init ok, start to accept new connect...
accept one new connect(4)!!!
accept one new connect(4)!!!
accept one new connect(4)!!!
^Ccatch SIGINT, quit...

客户端:编译 && 运行

[jiang@localhost 0406]$ gcc -o client client.c
[jiang@localhost 0406]$ ./client 
connect to server ok!
Hello, Client!

[jiang@localhost 0406]$ ./client 
connect to server ok!
Hello, Client!

[jiang@localhost 0406]$ ./client 
connect to server ok!
Hello, Client!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值