socket通信传送结构体


socket通信可以直接传送结构体!

示例:

(发送端)

[c-sharp]  view plain copy
  1. struct student  
  2. {  
  3.     char name[20];  
  4.     int age;  
  5. };  
  6. struct student student1={"liuxiaobing",23};  

接受端:

[c-sharp]  view plain copy
  1. struct student  
  2. {  
  3.     char name[20];  
  4.     int age;  
  5. };  
  6. struct student *student_client = NULL;  
  7. while(1)  
  8.     {  
  9.         n = read(sockfd, recvline, MAXLINE);  
  10.         if(n == -1)  
  11.         {  
  12.             perror("read error");  
  13.             exit(1);  
  14.         }  
  15.         student_client = (struct student *)recvline;  
  16.         //recvline[n] = 0;   
  17.         printf("Receive:name=%s***age=%d/n",student_client->name,student_client->age);  
  18.         //fputs(recvline, stdout);  
  19.         //printf("%s/n",recvline);  
  20.     }  

 

 

UDPsocket通信简单的代码:

udpserv.c

[c-sharp]  view plain copy
  1. /*udpserv.c*/  
  2. #include <sys/types.h>  
  3. #include <sys/socket.h>  
  4. #include <string.h>  
  5. #include <netinet/in.h>  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <stddef.h>  
  9. #include <unistd.h>  
  10. #include <pthread.h>  
  11. #define MAXLINE 100  
  12. #define SERV_PORT 8888  
  13. int sockfd;  
  14. struct sockaddr_in servaddr, cliaddr;  
  15. socklen_t len;  
  16. struct student  
  17. {  
  18.     char name[20];  
  19.     int age;  
  20. };  
  21. struct student student1={"liuxiaobing",23};  
  22. void get_msg()  
  23. {  
  24.     int n;  
  25.     //socklen_t len;  
  26.     char mesg[MAXLINE];  
  27.       
  28.     while(1)  
  29.     {  
  30.         memset(mesg,0,MAXLINE);  
  31.         len = sizeof(cliaddr)+1;  
  32.         // waiting for receive data   
  33.         n = recvfrom(sockfd, mesg, MAXLINE, 0, (struct sockaddr *)&cliaddr, &len);  
  34.         printf("对方:%s/n",mesg);  
  35.         //printf("n = %d/n",n);  
  36.         // sent data back to client   
  37.         //sendto(sockfd, mesg, n, 0, (struct sockaddr *)&cliaddr, len);  
  38.     }  
  39. }  
  40. void send_msg()  
  41. {  
  42.     int n;  
  43.     char sendline[MAXLINE];  
  44.     //while(fgets(sendline, MAXLINE, stdin) != NULL)  
  45.     while(1)  
  46.     {  
  47.         //printf("%s/n",sendline);  
  48.         n = strlen(sendline);  
  49.         //printf("n=%d/n",n);  
  50.         sendto(sockfd, (void *)&student1, sizeof(student1), 0,   
  51.                                     (struct sockaddr *)&cliaddr, len);  
  52.         sleep(1);  
  53.     }   
  54. }  
  55. int main(void)  
  56. {  
  57.     pthread_t threadin,threadout;  
  58.     void *retval;  
  59.     printf("%s/n%d/n",student1.name,student1.age);  
  60.     sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */  
  61.     /* init servaddr */  
  62.     bzero(&servaddr, sizeof(servaddr));  
  63.     servaddr.sin_family = AF_INET;  
  64.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);  
  65.     servaddr.sin_port = htons(SERV_PORT);  
  66.     /* bind address and port to socket */  
  67.     if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)  
  68.     {  
  69.         perror("bind error");  
  70.         exit(1);  
  71.     }  
  72.       
  73.     //do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));  
  74.     pthread_create(&threadin,NULL,(void *)(&get_msg),NULL);  
  75.     pthread_create(&threadout,NULL,(void *)(&send_msg),NULL);  
  76.       
  77.     pthread_join(threadin,&retval);  
  78.     pthread_join(threadout,&retval);  
  79.     return 0;  
  80. }   

udpclient.c

[c-sharp]  view plain copy
  1. #include <sys/types.h>  
  2. #include <sys/socket.h>  
  3. #include <string.h>  
  4. #include <netinet/in.h>  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7. #include <arpa/inet.h>  
  8. #include <stddef.h>  
  9. #include <unistd.h>  
  10. #include <pthread.h>  
  11. #define MAXLINE 80  
  12. #define SERV_PORT 8888  
  13. int sockfd;  
  14. struct sockaddr_in servaddr;  
  15. struct student  
  16. {  
  17.     char name[20];  
  18.     int age;  
  19. };  
  20. void get_msg()  
  21. {  
  22.     int n;  
  23.     char sendline[MAXLINE], recvline[MAXLINE + 1];  
  24.     struct student *student_client = NULL;  
  25.     socklen_t servlen;  
  26.     servlen = sizeof(servaddr)+1;  
  27.     if(connect(sockfd, (struct sockaddr *)&servaddr, servlen) == -1)  
  28.     {  
  29.         perror("connect error");  
  30.         exit(1);  
  31.     }  
  32.     while(1)  
  33.     {  
  34.         n = read(sockfd, recvline, MAXLINE);  
  35.         if(n == -1)  
  36.         {  
  37.             perror("read error");  
  38.             exit(1);  
  39.         }  
  40.         student_client = (struct student *)recvline;  
  41.         //recvline[n] = 0;   
  42.         printf("Receive:name=%s***age=%d/n",student_client->name,student_client->age);  
  43.         //fputs(recvline, stdout);  
  44.         //printf("%s/n",recvline);  
  45.     }  
  46. }  
  47. void send_msg()  
  48. {  
  49.     char sendline[MAXLINE];  
  50.     while(fgets(sendline, MAXLINE, stdin) != NULL)  
  51.     {  
  52.         write(sockfd, sendline, strlen(sendline));  
  53.     }  
  54. }  
  55. int main(int argc, char **argv)  
  56. {     
  57.     pthread_t threadin,threadout;  
  58.     void *retval;  
  59.       
  60.     /* check args */  
  61.     if(argc != 2)  
  62.     {  
  63.         printf("usage: udpclient <IPaddress>/n");  
  64.         exit(1);  
  65.     }  
  66.     /* init servaddr */  
  67.     bzero(&servaddr, sizeof(servaddr));  
  68.     servaddr.sin_family = AF_INET;  
  69.     servaddr.sin_port = htons(SERV_PORT);  
  70.       
  71.     /*用inet_pton判断输入的ip(存在argv[1]中)是否合法,并转化格式*/  
  72.     if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)  
  73.     {  
  74.         printf("[%s] is not a valid IPaddress/n", argv[1]);  
  75.         exit(1);  
  76.     }  
  77.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);/*create a socket*/  
  78.     //do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));  
  79.     pthread_create(&threadin,NULL,(void *)(&get_msg),NULL);  
  80.     pthread_create(&threadout,NULL,(void *)(&send_msg),NULL);  
  81.       
  82.     pthread_join(threadin,&retval);  
  83.     pthread_join(threadout,&retval);  
  84.     return 0;  
  85. }  

 

分别编译:

gcc -lpthread -o udpserv udpserv.c

gcc -lpthread -o udpclient udpclient.c

[                  由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要                       加-lpthread参数:
                                                                   gcc -o pthread  pthread.c   -lpthread

                                                                   需要将链接库 -lpthread放在最后面

  ]

服务端(ip:10.10.19.225)运行:

#./udpserv

客户端运行:

#./udpclient 10.10.19.225

hello(输入hello回车)

观察两端效果.

由于UDP是不可靠的,非连接通信,所以要先从客户端发消息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值