多线程网络服务-创新互联
1、多线程网络服务
专注于为中小企业提供成都网站设计、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业桓仁免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。:多线程网络模式类似于多进程网络模式;不同的是:新客户端到来时,启动的是一个线程(每来一个客户,将创建一个线程)。
模型分析
2、代码实现
同样用处理整数运算来模拟多线程的并发处理
(1)、utili.h
#include#include #include #include #include #include #include #include #define SERVER_PORT 9090 #define SERVER_IP "127.0.0.1" #define LISTEN_QUEUE 5 #define BUFFER_SIZE 255 typedef enum{ADD,SUB,MUL,DIV,MOD, QUIT}OPER_TYPE; typedef struct OperStruct{ int op1; int op2; OPER_TYPE oper; }OperStruct;
(2)、ser.c
#include"../utili.h" void* Thread_Handler(void *arg); void* Thread_Handler(void *arg){ int sockConn = *(int *)arg; OperStruct op; int result; while(1){ int res = recv(sockConn, &op, sizeof(op), 0); if(res == -1){ printf("recv data fail.\n"); continue; } if(op.oper == ADD){ result = op.op1 + op.op2; }else if(op.oper == SUB){ result = op.op1 - op.op2; }else if(op.oper == MUL){ result = op.op1 * op.op2; }else if(op.oper == DIV){ result = op.op1 / op.op2; }else if(op.oper == QUIT){ break; } res = send(sockConn, &result, sizeof(result), 0); if(res == -1){ printf("send data fail.\n"); continue; } } close(sockConn); pthread_exit(0); } int main(void){ int sockSer = socket(AF_INET, SOCK_STREAM, 0); if(sockSer == -1){ perror("socket"); return -1; } struct sockaddr_in addrSer, addrCli; addrSer.sin_family = AF_INET; addrSer.sin_port = htons(SERVER_PORT); addrSer.sin_addr.s_addr = inet_addr(SERVER_IP); socklen_t len = sizeof(struct sockaddr); int res = bind(sockSer, (struct sockaddr*)&addrSer, len); if(res == -1){ perror("bind"); close(sockSer); return -1; } listen(sockSer, LISTEN_QUEUE); int sockConn; while(1){ printf("Server Wait Client Connect.......\n"); sockConn = accept(sockSer, (struct sockaddr*)&addrCli, &len); if(sockConn == -1){ printf("Server Accept Client Connect Fail.\n"); continue; }else{ printf("Server Accept Client Connect Success.\n"); printf("Client IP:>%s\n", inet_ntoa(addrCli.sin_addr)); printf("Client Port:>%d\n",ntohs(addrCli.sin_port)); } pthread_t tid; pthread_create(&tid, NULL, Thread_Handler, &sockConn); } close(sockSer); return 0; }
(3)、cli.c
#include"utili.h" void InputData(OperStruct *pt); void InputData(OperStruct *pt){ printf("please input op1 and op2 : "); scanf("%d %d", &(pt->op1), &(pt->op2)); } //Cli int main(void){ int sockCli = socket(AF_INET, SOCK_STREAM, 0); if(sockCli == -1){ perror("socket"); return -1; } struct sockaddr_in addrSer; addrSer.sin_family = AF_INET; addrSer.sin_port = htons(SERVER_PORT); addrSer.sin_addr.s_addr = inet_addr(SERVER_IP); socklen_t len = sizeof(struct sockaddr); int res = connect(sockCli, (struct sockaddr*)&addrSer, len); if(res == -1){ perror("connect"); close(sockCli); return -1; }else{ printf("Client Connect Server Success.\n"); } char cmd[2]; OperStruct op; int result; while(1){ printf("Please input operator : "); scanf("%s",cmd); if(strcmp(cmd, "+") == 0){ op.oper = ADD; InputData(&op); }else if(strcmp(cmd,"-") == 0){ op.oper = SUB; InputData(&op); }else if(strcmp(cmd,"*") == 0){ op.oper = MUL; InputData(&op); }else if(strcmp(cmd,"/") == 0){ op.oper = DIV; InputData(&op); }else if(strcmp(cmd, "quit") == 0){ op.oper = QUIT; }else{ printf("Cmd invalid.\n"); } res = send(sockCli, &op, sizeof(op), 0); if(res == -1){ printf("send data fail.\n"); continue; } if(op.oper == QUIT) break; res = recv(sockCli, &result, sizeof(result), 0); if(res == -1){ printf("recv data fail.\n"); continue; } printf("result = %d\n", result); } close(sockCli); return 0; }
运行结果
服务器端
客户1
客户2
3、分析总结
多线程网络服务也存在线程的动态申请与释放,还是有一定的开销,若存在大量用户在线,很可能带来线程间切换开销。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文题目:多线程网络服务-创新互联
文章分享:http://lswzjz.com/article/dihjis.html