Linux下基于socket多线程并发通信的实现
分类:Linux2011-05-21 18:138455人阅读评论(5)收藏举报socket多线程linuxserverstruct服务器[cpp]view plaincopy
- /***************************************************
- *文件名:pthread_server.c
- *文件描述:创建子线程来接收客户端的数据
- ***************************************************/
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<stdio.h>
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<pthread.h>
- void*rec_data(void*fd);
- intmain(intargc,char*argv[])
- {
- intserver_sockfd;
- int*client_sockfd;
- intserver_len,client_len;
- structsockaddr_inserver_address;
- structsockaddr_inclient_address;
- structsockaddr_intempaddr;
- inti,byte;
- charchar_recv,char_send;
- socklen_ttemplen;
- server_sockfd=socket(AF_INET,SOCK_STREAM,0);//创建套接字
- server_address.sin_family=AF_INET;
- server_address.sin_addr.s_addr=htonl(INADDR_ANY);
- server_address.sin_port=htons(9734);
- server_len=sizeof(server_address);
- bind(server_sockfd,(structsockaddr*)&server_address,server_len);//绑定套接字
- templen=sizeof(structsockaddr);
- printf("serverwaitingforconnect/n");
- while(1){
- pthread_tthread;//创建不同的子线程以区别不同的客户端
- client_sockfd=(int*)malloc(sizeof(int));
- client_len=sizeof(client_address);
- *client_sockfd=accept(server_sockfd,(structsockaddr*)&client_address,(socklen_t*)&client_len);
- if(-1==*client_sockfd){
- perror("accept");
- continue;
- }
- if(pthread_create(&thread,NULL,rec_data,client_sockfd)!=0)//创建子线程
- {
- perror("pthread_create");
- break;
- }
- }
- shutdown(*client_sockfd,2);
- shutdown(server_sockfd,2);
- }
- /*****************************************
- *函数名称:rec_data
- *功能描述:接受客户端的数据
- *参数列表:fd——连接套接字
- *返回结果:void
- *****************************************/
- void*rec_data(void*fd)
- {
- intclient_sockfd;
- inti,byte;
- charchar_recv[100];//存放数据
- client_sockfd=*((int*)fd);
- for(;;)
- {
- if((byte=recv(client_sockfd,char_recv,100,0))==-1)
- {
- perror("recv");
- exit(EXIT_FAILURE);
- }
- if(strcmp(char_recv,"exit")==0)//接受到exit时,跳出循环
- break;
- printf("receivefromclientis%s/n",char_recv);//打印收到的数据
- }
- free(fd);
- close(client_sockfd);
- pthread_exit(NULL);
- }
- /***************************************************
- *文件名:pthread_client.c
- *文件描述:创建子线程来接收客户端的数据
- ***************************************************/
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<stdio.h>
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<unistd.h>
- #include<stdlib.h>
- intmain(intargc,char*argv[])
- {
- intsockfd;
- intlen;
- structsockaddr_inaddress;
- intresult;
- inti,byte;
- charchar_send[100]={0};
- if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- if(argc!=3){
- printf("Usage:fileclient<address><port>/n");//用法:文件名服务器IP地址服务器端口地址
- return0;
- }
- if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
- perror("sock");
- exit(1);
- }
- bzero(&address,sizeof(address));
- address.sin_family=AF_INET;
- address.sin_port=htons(atoi(argv[2]));
- inet_pton(AF_INET,argv[1],&address.sin_addr);
- len=sizeof(address);
- if((result=connect(sockfd,(structsockaddr*)&address,len))==-1)
- {
- perror("connect");
- exit(EXIT_FAILURE);
- }
- for(;;)
- {
- scanf("%s",char_send);//输入发送数据
- fflush(stdin);//清除输入缓存
- if(strcmp(char_send,"exit")==0){//如果输入exit,跳出循环
- if((byte=send(sockfd,char_send,100,0))==-1)
- {
- perror("send");
- exit(EXIT_FAILURE);
- }
- break;
- }
- if((byte=send(sockfd,char_send,100,0))==-1)
- {
- perror("send");
- exit(EXIT_FAILURE);
- }
- }
- close(sockfd);
- exit(0);
- }
编译服务器端程序pthread_server.c:
$gcc pthread_server.c –o server –lpthread
编译客户端程序pthread_client.c:
$gcc pthread_client.c –o client
编译在开发板上跑的客户端程序:
$arm-linux-gcc client.c –o arm_client
先启动服务器端的程序server:
$./server
打开另一个终端,启动客户端的程序client:
$./client 127.0.0.1 9734
把arm_client放到开发板上,启动arm_client:
$./arm_client 219.222.170.9 9734
结果:
服务器端:
tongs@tong"s-desktop:~/c_c++_program/sock_inet_comm2$ ./server
server waiting for connect
receive from client is client
receive from client is client
receive from client is arm_client
receive from client is arm_client
客户端:
tongs@tong"s-desktop:~/c_c++_program/sock_inet_comm2$ ./client 127.0.0.1 9734
client
client
exit
开发板服务器端
[/mnt/yaffs/nfs_share/sock_tcp/thread_socket]./arm_client219.222.170.9 9734
arm_client
arm_client
exit
所属专题: