Chinaunix首页 | 论坛 | 博客
  • 博客访问: 377405
  • 博文数量: 47
  • 博客积分: 967
  • 博客等级: 准尉
  • 技术积分: 1290
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-25 16:14
文章分类

全部博文(47)

文章存档

2019年(1)

2014年(1)

2013年(9)

2012年(36)

分类: LINUX

2012-10-30 09:54:29

  我们老师让写一个简单的聊天软件,并且实现不同机子之间的通信,我用的是SOCKET编程。不废话多说了,先附上代码:

服务器端server.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <netinet/in.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <sys/time.h>
  11.   
  12. #define BUFLEN 1024
  13. #define PORT 6666
  14. #define LISTNUM 20
  15.   
  16. int main()
  17. {
  18.     int sockfd, newfd;
  19.     struct sockaddr_in s_addr, c_addr;
  20.     char buf[BUFLEN];
  21.     socklen_t len;
  22.     unsigned int port, listnum;
  23.     fd_set rfds;
  24.     struct timeval tv;
  25.     int retval,maxfd;
  26.       
  27.     /*建立socket*/
  28.     if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1){
  29.         perror("socket");
  30.         exit(errno);
  31.     }else
  32.         printf("socket create success!\n");
  33.     memset(&s_addr,0,sizeof(s_addr));
  34.     s_addr.sin_family = AF_INET;
  35.     s_addr.sin_port = htons(PORT);
  36.     s_addr.sin_addr.s_addr = htons(INADDR_ANY);
  37.     
  38.     /*把地址和端口帮定到套接字上*/
  39.     if((bind(sockfd, (struct sockaddr*) &s_addr,sizeof(struct sockaddr))) == -1){
  40.         perror("bind");
  41.         exit(errno);
  42.     }else
  43.         printf("bind success!\n");
  44.     /*侦听本地端口*/
  45.     if(listen(sockfd,listnum) == -1){
  46.         perror("listen");
  47.         exit(errno);
  48.     }else
  49.         printf("the server is listening!\n");
  50.     while(1){
  51.         printf("*****************聊天开始***************\n");
  52.         len = sizeof(struct sockaddr);
  53.         if((newfd = accept(sockfd,(struct sockaddr*) &c_addr, &len)) == -1){
  54.             perror("accept");
  55.             exit(errno);
  56.         }else
  57.             printf("正在与您聊天的客户端是:%s: %d\n",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port));
  58.         while(1){
  59.             FD_ZERO(&rfds);
  60.             FD_SET(0, &rfds);
  61.             maxfd = 0;
  62.             FD_SET(newfd, &rfds);
  63.             /*找出文件描述符集合中最大的文件描述符*/
  64.             if(maxfd < newfd)
  65.                 maxfd = newfd;
  66.             /*设置超时时间*/
  67.             tv.tv_sec = 6;
  68.             tv.tv_usec = 0;
  69.             /*等待聊天*/
  70.             retval = select(maxfd+1, &rfds, NULL, NULL, &tv);
  71.             if(retval == -1){
  72.                 printf("select出错,与该客户端连接的程序将退出\n");
  73.                 break;
  74.             }else if(retval == 0){
  75.                 printf("waiting...\n");
  76.                 continue;
  77.             }else{
  78.                 /*用户输入信息了*/
  79.                 if(FD_ISSET(0, &rfds)){
  80.             
  81.                     /******发送消息*******/
  82.                     memset(buf,0,sizeof(buf));
  83.                     /*fgets函数:从流中读取BUFLEN-1个字符*/
  84.                     fgets(buf,BUFLEN,stdin);
  85.                     /*打印发送的消息*/
  86.                     //fputs(buf,stdout);
  87.                     if(!strncasecmp(buf,"quit",4)){
  88.                         printf("server 请求终止聊天!\n");
  89.                         break;
  90.                     }
  91.                         len = send(newfd,buf,strlen(buf),0);
  92.                     if(len > 0)
  93.                         printf("\t消息发送成功:%s\n",buf);
  94.                     else{
  95.                         printf("消息发送失败!\n");
  96.                         break;
  97.                     }
  98.                 }
  99.                 /*客户端发来了消息*/
  100.                 if(FD_ISSET(newfd, &rfds)){
  101.                     /******接收消息*******/
  102.                     memset(buf,0,sizeof(buf));
  103.                     /*fgets函数:从流中读取BUFLEN-1个字符*/
  104.                     len = recv(newfd,buf,BUFLEN,0);
  105.                     if(len > 0)
  106.                         printf("客户端发来的信息是:%s\n",buf);
  107.                     else{
  108.                         if(len < 0 )
  109.                             printf("接受消息失败!\n");
  110.                         else
  111.                             printf("客户端退出了,聊天终止!\n");
  112.                         break;
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.         /*关闭聊天的套接字*/
  118.         close(newfd);
  119.         /*是否退出服务器*/
  120.         printf("服务器是否退出程序:y->是;n->否? ");
  121.         bzero(buf, BUFLEN);
  122.         fgets(buf,BUFLEN, stdin);
  123.         if(!strncasecmp(buf,"y",1)){
  124.             printf("server 退出!\n");
  125.             break;
  126.         }
  127.     }
  128.     /*关闭服务器的套接字*/
  129.     close(sockfd);
  130.     return 0;
  131. }

客户端client.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <netinet/in.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <sys/time.h>

  11. #define BUFLEN 1024
  12. #define PORT 6666

  13. int main(int argc, char **argv)
  14. {
  15.     int sockfd;
  16.     struct sockaddr_in s_addr;
  17.     socklen_t len;
  18.     unsigned int port;
  19.     char buf[BUFLEN];
  20.     fd_set rfds;
  21.     struct timeval tv;
  22.     int retval, maxfd;
  23.     
  24.     /*建立socket*/
  25.     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
  26.         perror("socket");
  27.         exit(errno);
  28.     }else
  29.         printf("socket create success!\n");

  30.     
  31.     /*设置服务器ip*/
  32.     memset(&s_addr,0,sizeof(s_addr));
  33.     s_addr.sin_family = AF_INET;
  34.      s_addr.sin_port = htons(PORT);
  35.     if (inet_aton(argv[1], (struct in_addr *)&s_addr.sin_addr.s_addr) == 0) {
  36.         perror(argv[1]);
  37.         exit(errno);
  38.     }
  39.     /*开始连接服务器*/
  40.     if(connect(sockfd,(struct sockaddr*)&s_addr,sizeof(struct sockaddr)) == -1){
  41.         perror("connect");
  42.         exit(errno);
  43.     }else
  44.         printf("conncet success!\n");
  45.     
  46.     while(1){
  47.         FD_ZERO(&rfds);
  48.         FD_SET(0, &rfds);
  49.         maxfd = 0;
  50.         FD_SET(sockfd, &rfds);
  51.         if(maxfd < sockfd)
  52.             maxfd = sockfd;
  53.         tv.tv_sec = 6;
  54.         tv.tv_usec = 0;
  55.         retval = select(maxfd+1, &rfds, NULL, NULL, &tv);
  56.         if(retval == -1){
  57.             printf("select出错,客户端程序退出\n");
  58.             break;
  59.         }else if(retval == 0){
  60.             printf("waiting...\n");
  61.             continue;
  62.         }else{
  63.             /*服务器发来了消息*/
  64.             if(FD_ISSET(sockfd,&rfds)){
  65.                 /******接收消息*******/
  66.                 bzero(buf,BUFLEN);
  67.                 len = recv(sockfd,buf,BUFLEN,0);
  68.                 if(len > 0)
  69.                     printf("服务器发来的消息是:%s\n",buf);
  70.                 else{
  71.                     if(len < 0 )
  72.                         printf("接受消息失败!\n");
  73.                     else
  74.                         printf("服务器退出了,聊天终止!\n");
  75.                 break;
  76.                 }
  77.             }
  78.             /*用户输入信息了,开始处理信息并发送*/
  79.             if(FD_ISSET(0, &rfds)){
  80.                 /******发送消息*******/
  81.                 bzero(buf,BUFLEN);
  82.                 fgets(buf,BUFLEN,stdin);
  83.                
  84.                 if(!strncasecmp(buf,"quit",4)){
  85.                     printf("client 请求终止聊天!\n");
  86.                     break;
  87.                 }
  88.                     len = send(sockfd,buf,strlen(buf),0);
  89.                 if(len > 0)
  90.                     printf("\t消息发送成功:%s\n",buf);
  91.                 else{
  92.                     printf("消息发送失败!\n");
  93.                     break;
  94.                 }
  95.             }
  96.         }
  97.     
  98.     }
  99.     /*关闭连接*/
  100.     close(sockfd);

  101.     return 0;
  102. }
 在程序的服务端输入
  1. lwp@lwp-linux:~/Desktop/1234$ ./server 
在程序的客户端输入
  1. lwp@lwp-linux:~/Desktop/1234$ ./client 192.168.100.80
当然这都是我在我的机子上截的图,客户端我们可以放到任何一台机子上,只要保证都能在同一个局域网下,连到我的IP地址就行。开启服务端后,在开启客户端,我们就可以聊天了:

server端:

  1. waiting...
  2. waiting...
  3. waiting...
  4. nihal
  5.     消息发送成功:nihal

  6. 客户端发来的信息是:zhuzhu
  7. hehe
  8.     消息发送成功:hehe

  9. 客户端发来的信息是:haha
  10. 客户端退出了,聊天终止!
  11. 服务器是否退出程序:y->是;n->? y
  12. server端退出。

client端:

  1. waiting...
  2. waiting...
  3. waiting...
  4. 服务器发来的消息是:nihal
  5. zhuzhu
  6. 消息发送成功:zhuzhu
  7. 服务器发来的消息是:hehe
  8. haha
  9. 消息发送成功:haha








  1. lwp@lwp-linux:~/Desktop/1234$ ./server 
阅读(25534) | 评论(5) | 转发(6) |
给主人留下些什么吧!~~

钺钺钺2015-07-01 09:55:26

c:\\documents and settings\\administrator\\a.c(5) : fatal error C1083: Cannot open include file: \'sys/socket.h\': No such file or directory
Error executing cl.exe.这是怎么回事!!

lmnos2012-11-05 10:21:14

lwp225: ,我的邮箱是lwp225@gmail.com,还请你多多指导呀,看了你的博客LMOS2发布了,恭喜。。。。.....
谢谢,欢迎评论和建议

lwp2252012-11-02 19:33:08

,我的邮箱是lwp225@gmail.com,还请你多多指导呀,看了你的博客LMOS2发布了,恭喜。。。。

最大行业软件2012-11-02 16:27:45

ADAPT_BUILDER_ABI_V2009_PLUS_BUILD2
ADAPTSOFT_ADAPT_BUILDER_V1.5.4
ADAPT_BUILDER_EX_V3.1
ADAPT_BUILDER_MAT_EX_V2.10.2
ADAPT_ABI_V4.50.3
ADAPT_PT_V8.0房屋与停车场结构里的梁、版、楼版系统之先进后拉预力软体
ADAPT_RC_V5.00.3
ARRIS.v9.0.112 建筑设计师设计软件
Better.Homes.and.Gardens.Interior.Designer.v7.05
Big.Hammer.Do.It.Yourself.Fence.Designer.v5 庭園設計
Big.Hammer.Do.It.Yourself.Patio.Designer.v5 內院設計
Broderbund_3D_Home_Landscape_Designer_Deluxe_5_READNFO, 庭

lmnos2012-11-02 11:29:59

哇,不错,不错你有联系方式吗,比如QQ、MSN、邮箱……