多线程、全双工、UDP、对话源代码 .

多线程、全双工、UDP、对话源代码

 

[c-sharp] view plain copy print ?
  1. 服务端:  
  2.   
  3. /*头文件*/ 
  4.  
  5. #include <netinet/in.h>  
  6.  
  7. #include <sys/socket.h>  
  8.  
  9. #include <stdlib.h>  
  10.  
  11. #include <string.h>  
  12.  
  13. #include <sys/types.h>  
  14.  
  15. #include <stdio.h>  
  16.  
  17. #include <arpa/inet.h>  
  18.  
  19. #include <unistd.h>  
  20.  
  21. #include <pthread.h>   
  22.   
  23.    
  24.   
  25. /*宏定义*/ 
  26.  
  27. #define BUFFER_SIZE 1024  
  28.  
  29. #define PORT 4001   
  30.   
  31.    
  32.   
  33. /*函数声明*/  
  34.   
  35. void *function(void *arg);  
  36.   
  37.    
  38.   
  39. /*结构体定义*/  
  40.   
  41. struct ARG  
  42.   
  43. {  
  44.   
  45.        int sockfd;  
  46.   
  47.        int len;  
  48.   
  49. };  
  50.   
  51.    
  52.   
  53. struct sockaddr_in client;      //定义全局结构体变量   
  54.   
  55.    
  56.   
  57. /*主函数*/  
  58.   
  59. main()  
  60.   
  61. {  
  62.   
  63.        /*定义变量*/  
  64.   
  65.        pthread_t tid;  
  66.   
  67.        struct ARG *arg;  
  68.   
  69.        int sockfd,len;  
  70.   
  71.        char se_mes[BUFFER_SIZE];  
  72.   
  73.        struct sockaddr_in server;  
  74.   
  75.    
  76.   
  77.        /*调用套接字函数*/  
  78.   
  79.        sockfd=socket(AF_INET,SOCK_DGRAM,0);  
  80.   
  81.        if(sockfd==-1)  
  82.   
  83.        {  
  84.   
  85.               perror("socket() error");  
  86.   
  87.               exit(0);  
  88.   
  89.        }  
  90.   
  91.        //printf("Socket success!/n");   
  92.   
  93.    
  94.   
  95.        /*初始化server套接字地址结构,并对地址结构中的成员变量赋值*/  
  96.   
  97.        bzero(&server,sizeof(server));  
  98.   
  99.        server.sin_family=AF_INET;  
  100.   
  101.        server.sin_port=htons(PORT);  
  102.   
  103.        server.sin_addr.s_addr=htonl(INADDR_ANY);  
  104.   
  105.    
  106.   
  107.        if(bind(sockfd,(struct sockaddr *)&server,sizeof(server))==-1)  
  108.   
  109.        {  
  110.   
  111.               perror("bind() error");  
  112.   
  113.               exit(0);  
  114.   
  115.        }  
  116.   
  117.        //printf("Bind success!/n");   
  118.   
  119.        printf("等待客户端连接....../n");  
  120.   
  121.    
  122.   
  123. len=sizeof(client);  
  124.   
  125.        arg=(struct ARG *)malloc(sizeof(struct ARG));     //给结构体分配内存空间   
  126.   
  127.    
  128.   
  129.        /*给结构体成员变量赋值*/  
  130.   
  131.        arg->sockfd=sockfd;   
  132.   
  133.        arg->len=len;  
  134.   
  135.    
  136.   
  137.        /*产生子线程*/  
  138.   
  139.        if(pthread_create(&tid,NULL,function,(void *)arg))  
  140.   
  141.        {  
  142.   
  143.               perror("pthread_create() error");  
  144.   
  145.               exit(0);  
  146.   
  147.        }  
  148.   
  149.    
  150.   
  151.        /*发送消息给客户端*/  
  152.   
  153.        while(1)  
  154.   
  155.        {  
  156.   
  157.               bzero(se_mes,sizeof(se_mes));  
  158.   
  159.               gets(se_mes);  
  160.   
  161.               sendto(sockfd,se_mes,BUFFER_SIZE,0,(struct sockaddr *)&client,sizeof(client));  
  162.   
  163.        }  
  164.   
  165.    
  166.   
  167.        /*关闭套接字*/  
  168.   
  169.        close(sockfd);  
  170.   
  171.    
  172.   
  173. }  
  174.   
  175.    
  176.   
  177.    
  178.   
  179. /*自定义函数*/  
  180.   
  181. void * function(void *arg)  
  182.   
  183. {  
  184.   
  185.        /*定义变量*/  
  186.   
  187.        int sockfd,recvbytes,len;  
  188.   
  189.        char re_mes[BUFFER_SIZE];  
  190.   
  191.        struct ARG *info;  
  192.   
  193.        info=(struct ARG *)arg;  
  194.   
  195.        sockfd=info->sockfd;  
  196.   
  197.        len=info->len;  
  198.   
  199.    
  200.   
  201.        /*接收客户端发来的消息*/  
  202.   
  203.        bzero(re_mes,sizeof(re_mes));  
  204.   
  205.        recvbytes=recvfrom(sockfd,re_mes,BUFFER_SIZE,0,(struct sockaddr *)&client,&len);  
  206.   
  207.        if(recvbytes==-1)  
  208.   
  209.        {  
  210.   
  211.               perror("recvfrom() error");  
  212.   
  213.               exit(0);  
  214.   
  215.        }  
  216.   
  217.        printf("You got a message from client./nIt's ip is %s,port is %d./n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));          //显示客户端信息   
  218.   
  219.        printf("client: %s/n",re_mes);  
  220.   
  221.          
  222.   
  223.        sendto(sockfd,"欢迎你",9,0,(struct sockaddr *)&client,len);  
  224.   
  225.        while(1)  
  226.   
  227.        {  
  228.   
  229.               bzero(re_mes,sizeof(re_mes));  
  230.   
  231.               recvbytes=recvfrom(sockfd,re_mes,BUFFER_SIZE,0,(struct sockaddr *)&client,&len);  
  232.   
  233.               if(recvbytes==-1)  
  234.   
  235.               {  
  236.   
  237.                      perror("recvfrom() error");  
  238.   
  239.                      exit(0);  
  240.   
  241.               }  
  242.   
  243.               printf("客户端消息: %s/n",re_mes);  
  244.   
  245.                 
  246.   
  247.               if(!strcmp(re_mes,"bye"))  
  248.   
  249.               {  
  250.   
  251.                             sendto(sockfd,"byebye",6,0,(struct sockaddr *)&client,len);  
  252.   
  253.                             break;  
  254.   
  255.               }  
  256.   
  257.               if(!strcmp(re_mes,"byebye"))  
  258.   
  259.               {  
  260.   
  261.                             break;  
  262.   
  263.               }  
  264.   
  265.        }  
  266.   
  267.        free(arg);        //释放内存空间   
  268.   
  269.        close(sockfd);  
  270.   
  271.        exit(0);  
  272.   
  273. }  
  274.   
  275.    
  276.   
  277. 客户端:  
  278.   
  279. /*头文件*/ 
  280.  
  281. #include <netinet/in.h>  
  282.  
  283. #include <netdb.h>  
  284.  
  285. #include <sys/socket.h>  
  286.  
  287. #include <stdio.h>  
  288.  
  289. #include <stdlib.h>  
  290.  
  291. #include <string.h>  
  292.  
  293. #include <sys/types.h>  
  294.  
  295. #include <unistd.h>  
  296.  
  297. #include <pthread.h>   
  298.   
  299.    
  300.   
  301. /*宏定义*/ 
  302.  
  303. #define BUFFER_SIZE 1024  
  304.  
  305. #define PORT 4001   
  306.   
  307.    
  308.   
  309. /*结构体定义*/  
  310.   
  311. struct ARG  
  312.   
  313. {  
  314.   
  315.               int sockfd;  
  316.   
  317. };  
  318.   
  319.    
  320.   
  321. /*函数声明*/  
  322.   
  323. void *function(void *arg);  
  324.   
  325.    
  326.   
  327. /*主函数*/  
  328.   
  329. main(int argc,char *argv[])  
  330.   
  331. {  
  332.   
  333.        /*定义变量*/  
  334.   
  335.        pthread_t tid;  
  336.   
  337.        struct ARG *arg;  
  338.   
  339.        int sockfd,len;  
  340.   
  341.        char se_mes[BUFFER_SIZE];  
  342.   
  343.        struct sockaddr_in server;  
  344.   
  345.        struct hostent *he;  
  346.   
  347.        /*判断命令行参数*/  
  348.   
  349.        if(argc!=2)  
  350.   
  351.        {  
  352.   
  353.               printf("Usage: %s <IP Address>/n",argv[0]);  
  354.   
  355.               exit(0);  
  356.   
  357.        }  
  358.   
  359.    
  360.   
  361.        he=gethostbyname(argv[1]);  
  362.   
  363.        if(he==NULL)  
  364.   
  365.        {  
  366.   
  367.               printf("gethostbyname() error");  
  368.   
  369.               exit(0);  
  370.   
  371.        }  
  372.   
  373.    
  374.   
  375.        /*调用套接字函数*/  
  376.   
  377.        sockfd=socket(AF_INET,SOCK_DGRAM,0);  
  378.   
  379.        if(sockfd==-1)  
  380.   
  381.        {  
  382.   
  383.               perror("socket() error");  
  384.   
  385.               exit(0);  
  386.   
  387.        }  
  388.   
  389.    
  390.   
  391.        /*初始化server的地址结构,并为地址结构的成员赋值*/  
  392.   
  393.        bzero(&server,sizeof(server));  
  394.   
  395.        server.sin_family=AF_INET;  
  396.   
  397.        server.sin_port=htons(PORT);  
  398.   
  399.        server.sin_addr=*((struct in_addr *)he->h_addr);  
  400.   
  401.    
  402.   
  403.        sendto(sockfd,"我来了",9,0,(struct sockaddr *)&server,sizeof(server));  
  404.   
  405.          
  406.   
  407.        arg=(struct ARG *)malloc(sizeof(struct ARG));          //给结构体分配内存空间   
  408.   
  409.        arg->sockfd=sockfd;            /*给结构体成员变量赋值*/  
  410.   
  411.          
  412.   
  413.        /*产生子线程*/  
  414.   
  415.        if(pthread_create(&tid,NULL,function,(void *)arg))  
  416.   
  417.        {  
  418.   
  419.                      perror("pthread_create() error");  
  420.   
  421.                      exit(0);  
  422.   
  423.        }  
  424.   
  425.    
  426.   
  427.        /*发送消息给服务器*/  
  428.   
  429.        while(1)  
  430.   
  431.        {  
  432.   
  433.                      bzero(se_mes,sizeof(se_mes));  
  434.   
  435.                      gets(se_mes);  
  436.   
  437.                      sendto(sockfd,se_mes,BUFFER_SIZE,0,(struct sockaddr *)&server,sizeof(server));  
  438.   
  439.        }  
  440.   
  441.    
  442.   
  443.        /*关闭套接字*/  
  444.   
  445.        close(sockfd);  
  446.   
  447. }  
  448.   
  449.    
  450.   
  451.    
  452.   
  453. /*自定义函数*/  
  454.   
  455. void * function(void *arg)  
  456.   
  457. {  
  458.   
  459.               int sockfd,recvbytes,len;  
  460.   
  461.               struct sockaddr_in peer;  
  462.   
  463.               char re_mes[BUFFER_SIZE];  
  464.   
  465.               struct ARG *info;  
  466.   
  467.               info=(struct ARG *)arg;  
  468.   
  469.               sockfd=info->sockfd;  
  470.   
  471.    
  472.   
  473.               len=sizeof(peer);  
  474.   
  475.    
  476.   
  477.               /*接收服务器发来的消息*/  
  478.   
  479.               while(1)  
  480.   
  481.               {  
  482.   
  483.                      bzero(re_mes,sizeof(re_mes));  
  484.   
  485.                      recvbytes=recvfrom(sockfd,re_mes,BUFFER_SIZE,0,(struct sockaddr *)&peer,&len);  
  486.   
  487.                      if(recvbytes==-1)  
  488.   
  489.                      {  
  490.   
  491.                             perror("recv() error");  
  492.   
  493.                             exit(0);  
  494.   
  495.                      }  
  496.   
  497.                      printf("服务器消息: %s/n",re_mes);  
  498.   
  499.    
  500.   
  501.                      if(!strcmp(re_mes,"byebye"))  
  502.   
  503.                      {  
  504.   
  505.                                    break;  
  506.   
  507.                      }  
  508.   
  509.                      if(!strcmp(re_mes,"bye"))  
  510.   
  511.                      {  
  512.   
  513.                                    sendto(sockfd,"byebye",6,0,(struct sockaddr *)&peer,sizeof(peer));  
  514.   
  515.                                    break;  
  516.   
  517.                      }  
  518.   
  519.               }  
  520.   
  521.               free(arg);        //释放内存空间   
  522.   
  523.               close(sockfd);  
  524.   
  525.               exit(0);  
  526.   
  527. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值