久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

linux socket怎么實現使用線程獨立收發消息

171次閱讀
沒有評論

共計 4037 個字符,預計需要花費 11 分鐘才能閱讀完成。

今天丸趣 TV 小編給大家分享一下 linux socket 怎么實現使用線程獨立收發消息的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

代碼實現

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 1024
int 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]= \0 
 if(strcmp(buf, exit)==0){ // 若收到的是 exit 字符,則代表退出通信
 printf( Client is closed\n 
 close(new_fd);
 close(sockfd);
 exit(1);
 }
 printf(client:%s\n ,buf);
 }/*while*/
void acceptconnect(void){ // 接受客戶端連接請求函數
 struct sockaddr_in their_addr;
 int sin_size;
 sin_size = sizeof(struct sockaddr_in);
 if((new_fd = accept(sockfd,(struct sockaddr *) their_addr, sin_size))==-1){
 perror( accept 
 exit(1);
 }
 printf(server:got connection from %s\n ,inet_ntoa(their_addr.sin_addr));
 /* 創建子線程,用于接收信息 */
 if((pthread_create( recthread,NULL,(void*)recmessage,NULL)) != 0){
 printf( create thread error!\r\n 
 exit(1);
 }
int main(void){
 struct sockaddr_in my_addr;
 /* 創建套接字 */
 if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
 perror( socket 
 exit(1);
 }
 /* 初始化 sockaddr_in 結構體相關參數 */
 my_addr.sin_family = AF_INET;
 my_addr.sin_port = htons(MYPORT);
 my_addr.sin_addr.s_addr = INADDR_ANY;
 bzero((my_addr.sin_zero),8);
 /* 綁定端口與套接字 */
 if(bind(sockfd,(struct sockaddr*) my_addr,sizeof(struct sockaddr)) == -1){
 perror( bind 
 exit(1);
 }
 /* 監聽客戶端套接字 */
 if(listen(sockfd,BACKLOG)== -1){
 perror( listen 
 exit(1);
 }
 printf( listening...\n 
 /* 創建子線程,用于接收信息 */
 if((pthread_create( accthread,NULL,(void*)acceptconnect,NULL))!=0){
 printf( create thread error!\n 
 exit(1);
 }
 while(1){ char msg[MAXDATASIZE];
 scanf(%s ,msg);
 if(send(new_fd,msg,strlen(msg),0) == -1){ // 發送信息,與客戶端交流
 perror( send 
 exit(1);
 }
 if(strcmp(msg, exit) ==0){
 printf( byebye\n 
 close(new_fd);
 close(sockfd);
 exit(1);
 }
 }/*while*/
 return 0;
}/*main*/

2. 客戶端

/*tcpclient.c 2010.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 
#include  netdb.h 
#define PORT 3490
#define BACKLOG 10
#define MAXDATASIZE 1024
int sockfd;
pthread_t recthread;
/* 接收信息函數 */
void recmessage(void){ while(1){
 int numbytes;
 char buf[MAXDATASIZE];
 
 if((numbytes = recv(sockfd,buf,MAXDATASIZE,0))==-1){
 perror( recv 
 exit(1);
 }
 buf[numbytes]= \0 
 if(strcmp(buf, exit)==0){
 printf( Server is closed\n 
 close(sockfd);
 exit(1);
 }
 printf(Server:%s\n ,buf);
 }/*while*/
int main(int argc,char *argv[]){
 struct hostent *he;
 struct sockaddr_in their_addr;
 /* 客戶端輸入方式:./client 172.31.100.236, 若無輸入后面 IP 地址,會提示錯誤 */
 if(argc != 2){
 fprintf(stderr, usage:client hostname\n 
 exit(1);
 }
 /* 獲取主機 IP 地址 */
 if((he = gethostbyname(argv[1])) == NULL){
 herror( gethostbyname 
 exit(1);
 }
 /* 創建套接字 */
 if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
 perror( socket 
 exit(1);
 }
 /* 初始化 sockaddr_in 結構體 */
 their_addr.sin_family = AF_INET;
 their_addr.sin_port = htons(PORT);
 their_addr.sin_addr = *((struct in_addr *)he- h_addr);
 bzero((their_addr.sin_zero),8);
 /* 向服務器發送連接請求 */
 if(connect(sockfd,(struct sockaddr *) their_addr,sizeof(struct sockaddr)) == -1){
 perror( connect 
 exit(1);
 }
 /* 創建子線程,用于接收信息 */
 if((pthread_create( recthread,NULL,(void*)recmessage,NULL))!= 0){
 printf( create thread error!\r\n 
 exit(1);
 }
 /* 發送信息。接收發送信息用的是同一端口,都   是 sockfd*/
 while(1){ char msg[MAXDATASIZE];
 scanf(%s ,msg);
 if(send(sockfd,msg,strlen(msg),0) == -1){
 perror( send 
 exit(1);
 }
 if(strcmp(msg, exit) ==0){
 printf( byebye\n 
 close(sockfd);
 exit(1);
 }
 }/*while*/
 return 0;
}

運行結果:

終端 1:

[root@localhost net]# ./tcpserver
server:got connection from 172.31.100.236
Hello,World! 
client:hello
client:xiaolian
client:iou
byebye

終端 2:

[root@localhost net]# ./tcpclient 172.31.100.236
Server:Hello,World!
hello
xiaolian iou
Server:ok
Server is closed

以上就是“linux socket 怎么實現使用線程獨立收發消息”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,丸趣 TV 小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注丸趣 TV 行業資訊頻道。

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-15發表,共計4037字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 会理县| 西藏| 伽师县| 湖南省| 榆社县| 富锦市| 静乐县| 中西区| 淮滨县| 时尚| 丰都县| 东海县| 景洪市| 枣庄市| 酉阳| 南雄市| 景谷| 台山市| 达日县| 北安市| 高唐县| 罗甸县| 卓资县| 胶州市| 色达县| 都兰县| 清原| 平湖市| 百色市| 仁怀市| 五原县| 新绛县| 西乡县| 周至县| 会理县| 长春市| 尼木县| 太仆寺旗| 抚顺市| 辛集市| 桐梓县|