IPv6 simple TCP Server demo

TCP Server client implemention in C

* main.c

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h> /* read(), write(), close() */
#include <errno.h>
#include <pthread.h>

#define CLTBUFSZ 256

static int sockfd;

static off_t get_line(FILE *f, char *b) {
    off_t off = 0;

    fgets(b, 255, f);
    while (off < 255 && b[off]!=0x0d && b[off]!=0x0a) {
        off++;
    }
    b[off] = 0x00;
    return off;
}

// Function designed for chat between client and server.
int client_handler(int clt_fd, char *clt_ip)
{
    char buff[CLTBUFSZ];
    FILE *is = fdopen(clt_fd, "r");
    off_t off;
    char body[256];

    if (!is) {return -1;}
    bzero(buff, CLTBUFSZ);

    while (!feof(is)) {
        off = get_line(is, buff);
        if (off < 3) { break; }
        printf("%s\n", buff); /* Print client request message */
    }

    strcpy(buff, "HTTP/1.1 200 OK\r\n");
    send(clt_fd, buff, strlen(buff), 0);
    strcpy(buff, "Date: Sun, 12 May 2024 05:41:07 GMT\r\n");
    send(clt_fd, buff, strlen(buff), 0);
    sprintf(body, "<h1>TCP Server IPv6 demo.</h1><p>Your IP address: %s</p>\n", clt_ip);
    sprintf(buff, "Content-Length: %lu\r\n", strlen(body));
    send(clt_fd, buff, strlen(buff), 0);
    strcpy(buff, "Content-Type: text/html; charset=utf-8\r\n\r\n");
    send(clt_fd, buff, strlen(buff), 0);
    send(clt_fd, body, strlen(body), 0);

    return 0;
}

#define IPv6_CHUNK  \
    *s++ = cvt[ t[i]>>4 ]; \
    *s++ = cvt[ t[i] & 0x0f ]; \
    i++; \
    *s++ = cvt[ t[i]>>4 ]; \
    *s++ = cvt[ t[i] & 0x0f ]; \
    i++

char *inet6_to_s(struct in6_addr addr6) {
    char *s_addr = malloc(40);
    char *s = s_addr;
    uint8_t *t = (uint8_t *)addr6.__in6_u.__u6_addr8;
    int i = 0;
    char cvt[] = "0123456789abcdef";

    IPv6_CHUNK;
    while (i < 16) {
        *s++ = ':';
        IPv6_CHUNK;
    }
    *s = 0x00;
    return s_addr;
}

unsigned short atos(const char s[]) {
    int i;
    unsigned short us = 0;
    for (i = 0; i < 6 && s[i]; i++) {
        if (s[i] < 0x30 || s[i] > 0x3a) {
            break;
        }
        us *= 10;
        us += s[i] - 0x30;
    }
    return us;
}

void *work_rt(void *params) {
    int ret;
    char *s_addr = NULL;
    socklen_t len;
    int connfd;
    struct sockaddr_in6 cli;

    connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
    if (connfd < 0) {
        printf("server accept failed...\n");
        return NULL;
    }

    struct timeval timeout;
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    if (setsockopt (connfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) < 0) {
        printf("setsockopt failed\n");
        return NULL;
    }
    s_addr = inet6_to_s(cli.sin6_addr);
    if (0 == strncmp(s_addr, "0000:0000:0000:0000:0000:0000:0000:0000", 39)) {
        return NULL;
    }
    printf("server accept the client#%d [%s]:%d\n", connfd, s_addr, ntohs(cli.sin6_port));
    ret = client_handler(connfd, s_addr);
    if (ret != 0) {
        printf("client_handler error %d:%s\n", errno, strerror(errno));
    }
    free(s_addr);
    close(connfd);
    return NULL;
}


/*
 * http://[2408:8940:0:974a:368c:700c:????:????]:8080/
 * curl -i "http://\[2408:8940:0:974a:368c:700c:????:????\]:8080"
*/
int main(int argc, char *argv[])
{
    struct sockaddr_in6 servaddr;
    char s_port[6];
    pthread_t t1;

    if (argc < 2) {
        printf("Usage: %s port\n", argv[0]);
        return 0;
    }
    strncpy(s_port, argv[1], 6);

    sockfd = socket(AF_INET6, SOCK_STREAM, 0);
    if (sockfd == -1) {
        printf("socket creation failed %d:%s\n", errno, strerror(errno));
        return errno;
    }
    printf("server sockfd=%d\n", sockfd);
    /* atexit(clean_handler); */
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin6_family = AF_INET6;
    servaddr.sin6_addr = in6addr_any;
    servaddr.sin6_port = htons(atos(s_port));

    /*
> netstat -ano | findstr 8080
TCP    192.168.0.104:15515    157.148.54.168:8080    ESTABLISHED     24680
TCP    [2408:8940:0:974a:b0db:b3d9:????:????]:17234  [2408:80f1:21:4080::28]:8080  ESTABLISHED     11092
> taskkill /F /PID 11092
> taskkill /F /PID 24680
     */
    if ((bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
        printf("socket bind failed:%d:%s\n", errno, strerror(errno));
        return errno;
    }
    printf("Socket bound ipaddr.\n");
    // Now server is ready to listen and verification
    if ((listen(sockfd, 16)) != 0) {
        printf("Listen failed...\n");
        return errno;
    }
    printf("Server listening [::]:%d\n", ntohs(servaddr.sin6_port));

    /* Accept the data packet from client and verification */
    for (;;) {
        pthread_create(&t1, NULL, work_rt, NULL);
        pthread_join(t1, 0);
    }
}

* CMakeLists.txt

bash 复制代码
cmake_minimum_required(VERSION 3.10)
project(tcpsvr C)

set(CMAKE_C_STANDARD 90)

add_executable(tcpsvr main.c)

target_link_libraries(tcpsvr pthread)

* Makefile

bash 复制代码
CC=gcc
CFLAGS=-g -static -Wl,-s,--stack=67108864
TARGETS=tcpsvr
all: ${TARGETS}

${TARGETS}: main.o
	${CC} main.o -lpthread -o $@
main.o: main.c
	${CC} ${CFLAGS} -c main.c -o $@

clean:
	rm -f ${TARGETS} main.o core.*

test:
	./tcpsvr 8080

make

make test

相关推荐
ACP广源盛1392462567324 分钟前
IX8024与科学大模型的碰撞@ACP#筑牢科研 AI 算力高速枢纽分享
运维·服务器·网络·数据库·人工智能·嵌入式硬件·电脑
Empty-Filled1 小时前
AI生成测试用例功能怎么测:一个完整实战案例
网络·人工智能·测试用例
码云数智-大飞2 小时前
本地部署大模型:隐私安全与多元优势一站式解读
运维·网络·人工智能
jinanwuhuaguo2 小时前
(第二十九篇)OpenClaw 实时与具身的跃迁——从异步孤岛到数字世界的“原住民”
前端·网络·人工智能·重构·openclaw
汤愈韬3 小时前
三种常用 NAT 的经典案例
网络协议·网络安全·security
等风来不如迎风去3 小时前
【win11】最佳性能:fix 没有壁纸,一直黑屏
网络·人工智能
Harvy_没救了3 小时前
【网络部署】 Win11 + VMware CentOS8 + Nginx 文件共享服务 Wiki
运维·网络·nginx
汤愈韬3 小时前
NAT Server 与目的Nat
网络·网络协议·网络安全·security
2401_873479404 小时前
断网时如何实时判断IP归属?嵌入本地离线库,保障风控不中断
运维·服务器·网络
7ACE5 小时前
Wireshark TS | TLP 超时时间
网络·网络协议·tcp/ip·wireshark·tcpdump