转载:http://www.360doc.com/content/16/0421/11/478627_552531090.shtml

利用多线程实现linux下C语言的聊天室程序:

客户端代码:

threadsend线程负责客户端消息的发送;

threadrecv线程负责客户端接受服务器端的消息。

[html] view plain copy
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <netdb.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <sys/types.h>
  10. #include <arpa/inet.h>
  11. #include <pthread.h>
  12. #define MAXLINE 100;
  13. void *threadsend(void *vargp);
  14. void *threadrecv(void *vargp);
  15. int main()
  16. {
  17. int *clientfdp;
  18. clientfdp = (int *)malloc(sizeof(int));
  19. *clientfdp = socket(AF_INET,SOCK_STREAM,0);
  20. struct sockaddr_in serveraddr;
  21. struct hostent *hp;
  22. bzero((char *)&serveraddr,sizeof(serveraddr));
  23. serveraddr.sin_family = AF_INET;
  24. serveraddr.sin_port = htons(15636);
  25. serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  26. if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){
  27. printf("connect error\n");
  28. exit(1);
  29. }
  30. pthread_t tid1,tid2;
  31. printf("connected\n");
  32. while(1){
  33. pthread_create(&tid1,NULL,threadsend,clientfdp);
  34. pthread_create(&tid2,NULL,threadrecv,clientfdp);
  35. }
  36. return EXIT_SUCCESS;
  37. }
  38. void *threadsend(void * vargp)
  39. {
  40. //pthread_t tid2;
  41. int connfd = *((int *)vargp);
  42. int idata;
  43. char temp[100];
  44. while(1){
  45. //printf("me: \n ");
  46. fgets(temp,100,stdin);
  47. send(connfd,temp,100,0);
  48. printf("          client send OK\n");
  49. }
  50. printf("client send\n");
  51. return NULL;
  52. }
  53. void *threadrecv(void *vargp)
  54. {
  55. char temp[100];
  56. int connfd = *((int *)vargp);
  57. while(1){
  58. int idata = 0;
  59. idata = recv(connfd,temp,100,0);
  60. if(idata > 0){
  61. printf("server :\n%s\n",temp);
  62. }
  63. }
  64. return NULL;
  65. }

服务器端代码:

threadsend负责服务器端发送信息;

threadrecv负责接受客户端信息。

[html] view plain copy
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <netdb.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <sys/types.h>
  10. #include <arpa/inet.h>
  11. #include <pthread.h>
  12. #define PORT 15636
  13. void *thread(void *vargp);
  14. void *threadsend(void *vargp);
  15. void *threadrecv(void *vargp);
  16. int main()
  17. {
  18. int listenfd = socket(AF_INET, SOCK_STREAM,0);
  19. if(listenfd < 0){
  20. perror("socket");
  21. exit(1);
  22. }
  23. struct hostent *hp;
  24. struct sockaddr_in serveraddr;
  25. bzero((char *)&serveraddr,sizeof(serveraddr));
  26. serveraddr.sin_family = AF_INET;
  27. serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  28. serveraddr.sin_port = htons(PORT);
  29. if(bind(listenfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){
  30. perror("connect");
  31. exit(1);
  32. }
  33. if(listen(listenfd,1024) < 0){
  34. perror("listen error");
  35. exit(1);
  36. }
  37. //char temp[100];
  38. struct sockaddr_in clientaddr;
  39. int clientlen, *connfdp;
  40. clientlen = sizeof(clientaddr);
  41. while(1){
  42. connfdp = (int *)malloc(sizeof(int));
  43. *connfdp = accept(listenfd,(struct sockaddr *)&clientaddr, &clientlen);
  44. pthread_t tid;
  45. printf("Accepted!\n");
  46. pthread_create(&tid,NULL,thread,connfdp);
  47. }
  48. EXIT_SUCCESS;
  49. }
  50. void *thread(void *vargp)
  51. {
  52. pthread_t tid1,tid2;
  53. int connfd = *((int *)vargp);
  54. int idata;
  55. char temp[100];
  56. pthread_create(&tid1,NULL,threadsend,vargp);
  57. pthread_create(&tid2,NULL,threadrecv,vargp);
  58. return NULL;
  59. }
  60. void *threadsend(void * vargp)
  61. {
  62. int connfd = *((int *)vargp);
  63. int idata;
  64. char temp[100];
  65. while(1){
  66. //printf("server input:  ");
  67. fgets(temp,100,stdin);
  68. send(connfd,temp,100,0);
  69. printf("        server send OK\n");
  70. }
  71. return NULL;
  72. }
  73. void *threadrecv(void *vargp)
  74. {
  75. char temp[100];
  76. int connfd = *((int *)vargp);
  77. while(1){
  78. int idata = 0;
  79. idata = recv(connfd,temp,100,0);
  80. if(idata > 0){
  81. printf("client :\n%s\n",temp);
  82. }
  83. }
  84. return NULL;
  85. }

问题:

linux下编译多线程代码时,shell提示找不到 pthread_create函数,原因是 pthread.h不是linux系统默认加载的库文件,应该使用类似如下gcc命令进行编译:

gcc echoserver.c -lpthread -o echoserver

只要注意 -lpthread参数就可以了。

运行结果:

客户端:

[html] view plain copy
  1. [root@localhost unixIO]# ./echoclient
  2. connected
  3. hello!
  4. client send OK
  5. goodmorning
  6. client send OK
  7. server :
  8. goodmorning too!
  9. server :
  10. how r u?
  11. fine
  12. client send OK

服务器端:

[html] view plain copy
  1. [root@localhost unixIO]# ./echoserver
  2. Accepted!
  3. client :
  4. hello!
  5. client :
  6. goodmorning
  7. goodmorning too!
  8. server send OK
  9. how r u?
  10. server send OK
  11. client :
  12. fine

利用多线程实现linux下C语言的聊天室程序:相关推荐

  1. 基于Linux下的即时通讯聊天室项目(全代码 有注释 可直接运行)

    基于Linux下的即时通讯聊天室项目 一.序言 二.具体功能 三.系统客户要求 四.具体代码 1.服务器代码 2.客户端代码 一.序言 最近在写一个基于Linux下的聊天工具 它适合于局域网内所有人进 ...

  2. Linux下使用c++实现聊天室

    Linux使用c++我们会使g++来编码,使用gdb工具进行代码调试.同时在大型项目中,我们编写makefile来自动化编译. 知识点 使用 g++ 编译器编译代码 使用 gdb 进行调试 编写 ma ...

  3. Linux下socket多人聊天室

    目录 前言 一.聊天室的实验内容 二.逐个功能的简单分析 三.系统功能模块分解图 1.服务端功能模块图 2.客户端功能模块图 3.守护进程功能模块图 四.功能模块流程图 1.服务端流程图 2.客户端流 ...

  4. 利用多线程与网络编程编写的实时聊天小程序

    客户端代码: package Oracle; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  5. C语言网络聊天室程序

    共分为四部分,服务器两个,客户端两个 1.my_sever.c //******************************************************// //鐢熸垚鏃ユ湡 ...

  6. 您知道Linux下C语言编程的一些注意事项吗_教育中国

    您知道Linux下C语言编程的一些注意事项吗_教育中国 云风的 BLOG: 一个 C 接口设计的问题 一个 C 接口设计的问题 C 语言在本质上,参数传递都是值传递.不像 Pascal 和 C++ 可 ...

  7. Linux下C语言编程

    第1章 Linux下C语言编程简介 本章将简要介绍一下什么是Linux,C语言的特点,程序开发的预备知识,Linux下C语言开发的环境,程序设计的特点和原则以及编码风格等.通过本章的学习,可以对在Li ...

  8. 初识Linux下C语言编程

    本章将简要介绍一下什么是Linux,C语言的特点,程序开发的预备知识,Linux下C语言开发的环境,程序设计的特点和原则以及编码风格等.通过本章的学习,可以对在Linux下使用C语言编程有一个基本的了 ...

  9. Linux下C语言编程-进程的创建

    Linux下C语言编程-进程的创建 作者:hoyt 1.进程的概念 Linux操作系统是面向多用户的.在同一时间可以有许多用户向操作系统发出各种命令.那么操作系统是怎么实现多用户的环境呢?在现代的操作 ...

最新文章

  1. 【C++】C++11 STL算法(二):修改序列的操作(Modifying sequence operations)
  2. 微软发布全新多核心操作系统原型:Barrelfish
  3. 选择器Selector
  4. 洗澡或游泳等导致的耳朵进水的解决方案
  5. oracle常用的知识点
  6. php 租房子(练习题)
  7. input type=file美化
  8. Atitit.常见的异常分类 目录 1. 双元分类法 1 1.1. 按照语言分 java js c# php等 1 1.2. 通用常见异常vs 特定异常 1 1.3. Runtime ex vs c
  9. 网 络 响 应 状 态 码 常 见 的 错 误 代 码 及 错 误 原 因
  10. java 密钥库 口令_java密钥库和密码设置
  11. 时间序列分析实验报告总结_时间序列分析实验报告
  12. python安装scipy库出错_安装Scipy失败 解决途径
  13. ps的基础知识与教程
  14. 哪款国产ESD二极管可直接替代LC3311CCW?
  15. 低成本快速开发 LoRa 终端:从 0 到 1
  16. MySql按中文姓名排序
  17. 20220215-CTF-MISC-BUUCTF-ningen--binwalk分析---dd命令分离--ARCHPR暴力破解
  18. Java是一种强类型语言
  19. 西门子标准变频器MM430_MM440参数组切换应用方法示例
  20. 妖精的尾巴勇气之旅服务器维护,妖精的尾巴勇气之旅b服

热门文章

  1. pat 甲级 1072. Gas Station (30)
  2. 20162303《程序设计与数据结构》第一周学习总结
  3. VM克隆之后启动eth0找不到eth0:unknown interface:no such device
  4. 内存泄露从入门到精通三部曲之排查方法篇
  5. idhttp.post方式 调用datasnap rest 远程方法
  6. python学习笔记四——数据类型
  7. Windows Phone 内容滑动切换实现
  8. erp 维护费 要交吗_erp系统每年都要缴费吗
  9. netapp管理地址_NetApp常用管理命令总结
  10. linux ntp 'ntp_request.c'远程拒绝服务漏洞,NTP 'ntp_request.c'远程拒绝服务漏洞