linux socket怎么实现使用线程独立收发消息
时间:2023-05-17 04:18
代码实现 1、服务器端/*tcpserver.c 2011.9.1 by yyg*/#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#include <pthread.h>#define MYPORT 3490 //定义端口#define BACKLOG 10#define MAXDATASIZE 1024int sockfd,new_fd;pthread_t accthread,recthread;void recmessage(void){ //接收客户端信息函数 while(1){ int numbytes; char buf[MAXDATASIZE]; if((numbytes = recv(new_fd,buf,MAXDATASIZE,0))==-1){ perror("recv"); exit(1); } buf[numbytes]='