0

0

linux socket怎么实现使用线程独立收发消息

WBOY

WBOY

发布时间:2023-05-16 17:16:20

|

1338人浏览过

|

来源于亿速云

转载

代码实现

1、服务器端

/*tcpserver.c  2011.9.1 by yyg*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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
ok
exit
byebye

终端2:

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

相关专题

更多
线程和进程的区别
线程和进程的区别

线程和进程的区别:线程是进程的一部分,用于实现并发和并行操作,而线程共享进程的资源,通信更方便快捷,切换开销较小。本专题为大家提供线程和进程区别相关的各种文章、以及下载和课程。

469

2023.08.10

磁盘配额是什么
磁盘配额是什么

磁盘配额是计算机中指定磁盘的储存限制,就是管理员可以为用户所能使用的磁盘空间进行配额限制,每一用户只能使用最大配额范围内的磁盘空间。php中文网为大家提供各种磁盘配额相关的内容,教程,供大家免费下载安装。

1345

2023.06.21

如何安装LINUX
如何安装LINUX

本站专题提供如何安装LINUX的相关教程文章,还有相关的下载、课程,大家可以免费体验。

698

2023.06.29

linux find
linux find

find是linux命令,它将档案系统内符合 expression 的档案列出来。可以指要档案的名称、类别、时间、大小、权限等不同资讯的组合,只有完全相符的才会被列出来。find根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部分为 path,之后的是 expression。还有指DOS 命令 find,Excel 函数 find等。本站专题提供linux find相关教程文章,还有相关

293

2023.06.30

linux修改文件名
linux修改文件名

本专题为大家提供linux修改文件名相关的文章,这些文章可以帮助用户快速轻松地完成文件名的修改工作,大家可以免费体验。

773

2023.07.05

linux系统安装教程
linux系统安装教程

linux系统是一种可以免费使用,自由传播,多用户、多任务、多线程、多CPU的操作系统。本专题提供linux系统安装教程相关的文章,大家可以免费体验。

571

2023.07.06

linux查看文件夹大小
linux查看文件夹大小

Linux是一种自由和开放源码的类Unix操作系统,存在着许多不同的Linux版本,但它们都使用了Linux内核。Linux可安装在各种计算机硬件设备中,比如手机、平板电脑、路由器、视频游戏控制台、台式计算机、大型机和超级计算机。linux怎么查看文件夹大小呢?php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

544

2023.07.20

linux查看ip命令
linux查看ip命令

本专题为大家提供linux查看ip命令相关文章内容,感兴趣的朋友可以免费下载体验试试。

296

2023.07.20

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6.3万人学习

Git 教程
Git 教程

共21课时 | 2.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号