一、多线程并发完整代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <pthread.h>#define PORT 8808
#define IP "192.168.122.92"struct msg
{int newfd;struct sockaddr_in cin;
};void *callBack(void *arg);
void handle(int sig)
{while(waitpid(-1,NULL,WNOHANG) > 0);
}
int main(int argc, const char *argv[])
{if(signal(17,handle) == SIG_ERR){perror("signal");return -1;}//创建流式套接字int sfd = socket(AF_INET,SOCK_STREAM,0);if(sfd < 0){perror("socket");fprintf(stderr,"socket failed __%d__\n",__LINE__);return -1;}printf("socket success...\n");//设置允许端口号复用int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {perror("setsockopt");return -1;}//填充地址信息结构体,真是的地址信息结构体根据地址族制定struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(PORT);sin.sin_addr.s_addr = inet_addr(IP);//绑定服务器IP和端口号if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0){perror("bind");fprintf(stderr,"bind failed __%d__\n",__LINE__);return -1;}printf("bind success...\n");//将套接字设置为被动监听状态if(listen(sfd,10) < 0){perror("listen");return -1;}printf("listen success...\n");//从已完成连接的队列中获取一个客户信息,生成一个新的文件描述符,该文件描述符才是与客户端通信的文件描述符struct sockaddr_in cin;socklen_t addrlen = sizeof(cin);int newfd = -1;pthread_t tid;struct msg info;while(1){newfd = accept(sfd,(struct sockaddr *)&cin,&addrlen);if(newfd < 0){perror("accept");return -1;}printf("[%s : %d] accept success\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));info.newfd = newfd;info.cin = cin;if(pthread_create(&tid,NULL,callBack,&info) != 0){fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);return -1;}pthread_detach(tid);}//关闭所有文件描述符close(sfd);return 0;
}
void *callBack(void *arg)
{int newfd = ((struct msg *)arg)->newfd;struct sockaddr_in cin = ((struct msg *)arg)->cin;ssize_t res = 0;char buf[128] = "";while(1){bzero(buf,sizeof(buf));res = recv(newfd,buf,sizeof(buf),0);if(res < 0){perror("recv");return NULL;}else if(0 ==res){printf("[%s : %d] client offline\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));break;}printf("[%s : %d] %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);//发送数据if(send(newfd,buf,sizeof(buf),0) < 0){perror("send");return NULL;}}close(newfd);pthread_exit(NULL);}
二、多线程中的newfd,能否修改成全局
答:不行,newfd必须要另存,因为同一个进程下的线程共享其附属进程的所有资源,如果使用全局,则会导致每次连接客户端后, newfd和cin会被覆盖。
现象:
由此可见,最后一次出错,客户端1被客户端2覆盖
三、多线程中分支线程的newfd能否不另存,直接用指针间接访问主线程的newfd
答:不行,如果使用指针间接访问外部成员变量,会导致,成员变量被覆盖,和上题类似。