今天小編給大家分享一下linux socket怎么實(shí)現(xiàn)使用線程獨(dú)立收發(fā)消息的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

紅塔網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),紅塔網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為紅塔1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的紅塔做網(wǎng)站的公司定做!
代碼實(shí)現(xiàn)
1、服務(wù)器端
/*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){ //接收客戶端信息函數(shù)
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){ //接受客戶端連接請(qǐng)求函數(shù)
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));
/*創(chuàng)建子線程,用于接收信息*/
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;
/*創(chuàng)建套接字*/
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("socket");
exit(1);
}
/*初始化sockaddr_in結(jié)構(gòu)體相關(guān)參數(shù)*/
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);
}
/*監(jiān)聽客戶端套接字*/
if(listen(sockfd,BACKLOG)== -1){
perror("listen");
exit(1);
}
printf("listening...\n");
/*創(chuàng)建子線程,用于接收信息*/
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){ //發(fā)送信息,與客戶端交流
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;
/*接收信息函數(shù)*/
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地址,會(huì)提示錯(cuò)誤*/
if(argc != 2){
fprintf(stderr,"usage:client hostname\n");
exit(1);
}
/*獲取主機(jī)IP地址*/
if((he = gethostbyname(argv[1])) == NULL){
herror("gethostbyname");
exit(1);
}
/*創(chuàng)建套接字*/
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("socket");
exit(1);
}
/*初始化sockaddr_in結(jié)構(gòu)體*/
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);
/*向服務(wù)器發(fā)送連接請(qǐng)求*/
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1){
perror("connect");
exit(1);
}
/*創(chuàng)建子線程,用于接收信息*/
if((pthread_create(&recthread,NULL,(void*)recmessage,NULL))!= 0){
printf("create thread error!\r\n");
exit(1);
}
/*發(fā)送信息。接收發(fā)送信息用的是同一端口,都 是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;
}運(yùn)行結(jié)果:
終端1:
[root@localhost net]# ./tcpserver server:got connection from 172.31.100.236 Hello,World! client:hello client:xiaolian client:iou ok exit byebye
終端2:
[root@localhost net]# ./tcpclient 172.31.100.236 Server:Hello,World! hello xiaolian iou Server:ok Server is closed
以上就是“l(fā)inux socket怎么實(shí)現(xiàn)使用線程獨(dú)立收發(fā)消息”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:linuxsocket怎么實(shí)現(xiàn)使用線程獨(dú)立收發(fā)消息
當(dāng)前鏈接:http://chinadenli.net/article20/jpsgjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、域名注冊(cè)、云服務(wù)器、網(wǎng)站策劃、虛擬主機(jī)、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)