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

相关推荐
前端李易安39 分钟前
什么是HTTP,什么是HTTPS?HTTP和HTTPS都有哪些区别?
网络协议·http·https
胎粉仔40 分钟前
网络初阶——应用层:HTTPS 协议
网络协议·http·https
Koi慢热1 小时前
信息收集合集
网络·安全·web安全·网络安全
轩轩曲觞阁1 小时前
Linux网络——网络初识
linux·网络
摘星星ʕ•̫͡•ʔ2 小时前
计算机网络 第二章:物理层
网络·计算机网络
linnux领域2 小时前
使用ensp配置单臂路由、静态路由,实现PC互相通信。
网络
hgdlip2 小时前
本机ip地址和网络ip地址一样吗
网络·网络协议·tcp/ip·网络ip地址·本机ip地址
EasyCVR2 小时前
ISUP协议视频平台EasyCVR视频设备轨迹回放平台智慧农业视频远程监控管理方案
服务器·网络·数据库·音视频
长安11086 小时前
前后端、网关、协议方面补充
网络
hzyyyyyyyu9 小时前
隧道技术-tcp封装icmp出网
网络·网络协议·tcp/ip