与多进程实现对比来看。
示例来源于网络视频
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>#include "wrap.h"#define MAXLINE 8192
#define SERV_PORT 8000struct s_info { //定义一个结构体, 将地址结构跟cfd捆绑struct sockaddr_in cliaddr;int connfd;
};void *do_work(void *arg)
{int n,i;struct s_info *ts = (struct s_info*)arg;char buf[MAXLINE];char str[INET_ADDRSTRLEN]; //#define INET_ADDRSTRLEN 16 可用"[+d"查看while (1) {n = Read(ts->connfd, buf, MAXLINE); //读客户端if (n == 0) {printf("the client %d closed...\n", ts->connfd);break; //跳出循环,关闭cfd}printf("received from %s at PORT %d\n",inet_ntop(AF_INET, &(*ts).cliaddr.sin_addr, str, sizeof(str)),ntohs((*ts).cliaddr.sin_port)); //打印客户端信息(IP/PORT)for (i = 0; i < n; i++) buf[i] = toupper(buf[i]); //小写-->大写Write(STDOUT_FILENO, buf, n); //写出至屏幕Write(ts->connfd, buf, n); //回写给客户端}Close(ts->connfd);return (void *)0;
}int main(void)
{struct sockaddr_in servaddr, cliaddr;socklen_t cliaddr_len;int listenfd, connfd;pthread_t tid;struct s_info ts[256]; //根据最大线程数创建结构体数组.int i = 0;listenfd = Socket(AF_INET, SOCK_STREAM, 0); //创建一个socket, 得到lfdbzero(&servaddr, sizeof(servaddr)); //地址结构清零servaddr.sin_family = AF_INET;servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //指定本地任意IPservaddr.sin_port = htons(SERV_PORT); //指定端口号 8000Bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); //绑定Listen(listenfd, 128); //设置同一时刻链接服务器上限数printf("Accepting client connect ...\n");while (1) {cliaddr_len = sizeof(cliaddr);connfd = Accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len); //阻塞监听客户端链接请求ts[i].cliaddr = cliaddr;ts[i].connfd = connfd;/* 达到线程最大数时,pthread_create出错处理, 增加服务器稳定性 */pthread_create(&tid, NULL, do_work, (void*)&ts[i]);pthread_detach(tid); //子线程分离,防止僵线程产生.i++;}return 0;
}