日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發布時間:2023/11/30 linux 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用多线程实现linux下C语言的聊天室程序: 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載:http://www.360doc.com/content/16/0421/11/478627_552531090.shtml

利用多線程實現linux下C語言的聊天室程序:

客戶端代碼:

threadsend線程負責客戶端消息的發送;

threadrecv線程負責客戶端接受服務器端的消息。

[html]?view plain?copy
  • #include?<stdlib.h>??
  • #include?<stdio.h>??
  • #include?<errno.h>??
  • #include?<string.h>??
  • #include?<unistd.h>??
  • #include?<netdb.h>??
  • #include?<sys/socket.h>??
  • #include?<netinet/in.h>??
  • #include?<sys/types.h>??
  • #include?<arpa/inet.h>??
  • #include?<pthread.h>??
  • ??
  • #define?MAXLINE?100;??
  • void?*threadsend(void?*vargp);??
  • void?*threadrecv(void?*vargp);??
  • ??
  • int?main()??
  • {??
  • ??
  • int?*clientfdp;??
  • clientfdp?=?(int?*)malloc(sizeof(int));??
  • *clientfdp?=?socket(AF_INET,SOCK_STREAM,0);??
  • struct?sockaddr_in?serveraddr;??
  • struct?hostent?*hp;??
  • bzero((char?*)&serveraddr,sizeof(serveraddr));??
  • serveraddr.sin_family?=?AF_INET;??
  • serveraddr.sin_port?=?htons(15636);??
  • serveraddr.sin_addr.s_addr?=?inet_addr("127.0.0.1");??
  • if(connect(*clientfdp,(struct?sockaddr?*)&serveraddr,sizeof(serveraddr))?<?0){??
  • ????????printf("connect?error\n");??
  • ????????exit(1);??
  • }??
  • ??
  • pthread_t?tid1,tid2;??
  • printf("connected\n");??
  • while(1){??
  • pthread_create(&tid1,NULL,threadsend,clientfdp);??
  • ??
  • pthread_create(&tid2,NULL,threadrecv,clientfdp);??
  • }??
  • ??
  • return?EXIT_SUCCESS;??
  • }??
  • ??
  • void?*threadsend(void?*?vargp)??
  • {??
  • //pthread_t?tid2;??
  • int?connfd?=?*((int?*)vargp);??
  • ??
  • int?idata;??
  • char?temp[100];??
  • while(1){??
  • //printf("me:?\n?");??
  • fgets(temp,100,stdin);??
  • send(connfd,temp,100,0);??
  • printf("??????????client?send?OK\n");??
  • ??
  • }??
  • ??
  • ??
  • printf("client?send\n");??
  • return?NULL;??
  • }??
  • ??
  • ??
  • void?*threadrecv(void?*vargp)??
  • {??
  • char?temp[100];??
  • int?connfd?=?*((int?*)vargp);??
  • while(1){??
  • int?idata?=?0;??
  • idata?=?recv(connfd,temp,100,0);??
  • if(idata?>?0){??
  • printf("server?:\n%s\n",temp);??
  • }??
  • }??
  • ??
  • ??
  • return?NULL;??
  • }??
  • 服務器端代碼:

    threadsend負責服務器端發送信息;

    threadrecv負責接受客戶端信息。

    [html]?view plain?copy
  • #include?<stdlib.h>??
  • #include?<stdio.h>??
  • #include?<errno.h>??
  • #include?<string.h>??
  • #include?<unistd.h>??
  • #include?<netdb.h>??
  • #include?<sys/socket.h>??
  • #include?<netinet/in.h>??
  • #include?<sys/types.h>??
  • #include?<arpa/inet.h>??
  • #include?<pthread.h>??
  • #define?PORT?15636??
  • void?*thread(void?*vargp);??
  • void?*threadsend(void?*vargp);??
  • void?*threadrecv(void?*vargp);??
  • ??
  • int?main()??
  • {??
  • ??
  • int?listenfd?=?socket(AF_INET,?SOCK_STREAM,0);??
  • if(listenfd?<?0){??
  • ????????perror("socket");??
  • ????????exit(1);??
  • }??
  • ??
  • struct?hostent?*hp;??
  • struct?sockaddr_in?serveraddr;??
  • bzero((char?*)&serveraddr,sizeof(serveraddr));??
  • serveraddr.sin_family?=?AF_INET;??
  • serveraddr.sin_addr.s_addr?=?htonl(INADDR_ANY);??
  • serveraddr.sin_port?=?htons(PORT);??
  • ??
  • if(bind(listenfd,(struct?sockaddr?*)&serveraddr,sizeof(serveraddr))?<?0){??
  • ????????perror("connect");??
  • ????????exit(1);??
  • }??
  • ??
  • if(listen(listenfd,1024)?<?0){??
  • ????????perror("listen?error");??
  • ????????exit(1);??
  • }??
  • ??
  • //char?temp[100];??
  • struct?sockaddr_in?clientaddr;??
  • int?clientlen,?*connfdp;??
  • clientlen?=?sizeof(clientaddr);??
  • while(1){??
  • connfdp?=?(int?*)malloc(sizeof(int));??
  • *connfdp?=?accept(listenfd,(struct?sockaddr?*)&clientaddr,?&clientlen);??
  • pthread_t?tid;??
  • printf("Accepted!\n");??
  • pthread_create(&tid,NULL,thread,connfdp);??
  • }??
  • EXIT_SUCCESS;??
  • }??
  • ??
  • void?*thread(void?*vargp)??
  • {??
  • ??
  • pthread_t?tid1,tid2;??
  • int?connfd?=?*((int?*)vargp);??
  • int?idata;??
  • char?temp[100];??
  • pthread_create(&tid1,NULL,threadsend,vargp);??
  • pthread_create(&tid2,NULL,threadrecv,vargp);??
  • return?NULL;??
  • }??
  • ??
  • void?*threadsend(void?*?vargp)??
  • {??
  • ??
  • int?connfd?=?*((int?*)vargp);??
  • ??
  • int?idata;??
  • char?temp[100];??
  • while(1){??
  • //printf("server?input:??");??
  • fgets(temp,100,stdin);??
  • send(connfd,temp,100,0);??
  • printf("????????server?send?OK\n");??
  • }??
  • return?NULL;??
  • }??
  • ??
  • void?*threadrecv(void?*vargp)??
  • {??
  • char?temp[100];??
  • int?connfd?=?*((int?*)vargp);??
  • while(1){??
  • int?idata?=?0;??
  • idata?=?recv(connfd,temp,100,0);??
  • if(idata?>?0){??
  • printf("client?:\n%s\n",temp);??
  • }??
  • }??
  • return?NULL;??
  • }??

  • 問題:

    linux下編譯多線程代碼時,shell提示找不到 pthread_create函數,原因是 pthread.h不是linux系統默認加載的庫文件,應該使用類似如下gcc命令進行編譯:

    gcc echoserver.c -lpthread -o echoserver

    只要注意 -lpthread參數就可以了。


    運行結果:

    客戶端:

    [html]?view plain?copy
  • [root@localhost?unixIO]#?./echoclient??
  • connected??
  • hello!??
  • ??????????client?send?OK??
  • goodmorning??
  • ??????????client?send?OK??
  • server?:??
  • goodmorning?too!??
  • ??
  • server?:??
  • how?r?u???
  • ??
  • fine??
  • ??????????client?send?OK??

  • 服務器端:


    [html]?view plain?copy
  • [root@localhost?unixIO]#?./echoserver??
  • Accepted!??
  • client?:??
  • hello!??
  • ??
  • client?:??
  • goodmorning??
  • ??
  • goodmorning?too!??
  • ????????server?send?OK??
  • how?r?u???
  • ????????server?send?OK??
  • client?:??
  • fine ?

  • 總結

    以上是生活随笔為你收集整理的利用多线程实现linux下C语言的聊天室程序:的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。