UDP实现服务器和客户端的通信

Love The Way You Lie 2021-09-19 01:04 620阅读 0赞

一 服务端

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <strings.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <sys/stat.h>
  11. #include <dirent.h>
  12. #include <sys/mman.h>
  13. #include <sys/wait.h>
  14. #include <signal.h>
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. #include <sys/msg.h>
  18. #include <sys/sem.h>
  19. #include <pthread.h>
  20. #include <semaphore.h>
  21. #include <poll.h>
  22. #include <sys/epoll.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <netinet/in.h>
  27. char rbuf[50];
  28. int main()
  29. {
  30. int sockfd;
  31. int size;
  32. int ret;
  33. int on =1;
  34. struct sockaddr_in saddr;
  35. struct sockaddr_in raddr;
  36. //设置地址信息,ip信息
  37. size = sizeof(struct sockaddr_in);
  38. bzero(&saddr,size);
  39. saddr.sin_family = AF_INET;
  40. saddr.sin_port = htons(8888);
  41. saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  42. //创建udp 的套接字
  43. sockfd = socket(AF_INET,SOCK_DGRAM,0);
  44. if(sockfd<0)
  45. {
  46. perror("socket failed");
  47. return -1;
  48. }
  49. //设置端口复用
  50. setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
  51. //绑定地址信息,ip信息
  52. ret = bind(sockfd,(struct sockaddr*)&saddr,sizeof(struct sockaddr));
  53. if(ret<0)
  54. {
  55. perror("sbind failed");
  56. return -1;
  57. }
  58. socklen_t val = sizeof(struct sockaddr);
  59. //循环接收客户端发来的消息
  60. while(1)
  61. {
  62. puts("waiting data");
  63. // 没有数据过来时,就在recvfrom函数上阻塞着
  64. ret=recvfrom(sockfd,rbuf,50,0,(struct sockaddr*)&raddr,&val);
  65. if(ret <0)
  66. {
  67. perror("recvfrom failed");
  68. }
  69. printf("the data :%s\n",rbuf);
  70. bzero(rbuf,50);
  71. }
  72. //关闭udp套接字,这里不可达的。
  73. close(sockfd);
  74. return 0;
  75. }

二 客户端

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <strings.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <sys/stat.h>
  11. #include <dirent.h>
  12. #include <sys/mman.h>
  13. #include <sys/wait.h>
  14. #include <signal.h>
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. #include <sys/msg.h>
  18. #include <sys/sem.h>
  19. #include <pthread.h>
  20. #include <semaphore.h>
  21. #include <poll.h>
  22. #include <sys/epoll.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <netinet/in.h>
  27. char wbuf[50];
  28. int main()
  29. {
  30. int sockfd;
  31. int size,on = 1;
  32. struct sockaddr_in saddr;
  33. int ret;
  34. size = sizeof(struct sockaddr_in);
  35. bzero(&saddr,size);
  36. //设置地址信息,ip信息
  37. saddr.sin_family = AF_INET;
  38. saddr.sin_port = htons(8888);
  39. saddr.sin_addr.s_addr = inet_addr("192.168.0.110");//192.168.0.110为服务端所在的ip
  40. sockfd= socket(AF_INET,SOCK_DGRAM,0); //创建udp 的套接字
  41. if(sockfd<0)
  42. {
  43. perror("failed socket");
  44. return -1;
  45. }
  46. //设置端口复用
  47. setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
  48. //循环发送信息给服务端
  49. while(1)
  50. {
  51. puts("please enter data:");
  52. scanf("%s",wbuf);
  53. ret=sendto(sockfd,wbuf,50,0,(struct sockaddr*)&saddr,
  54. sizeof(struct sockaddr));
  55. if(ret<0)
  56. {
  57. perror("sendto failed");
  58. }
  59. bzero(wbuf,50);
  60. }
  61. close(sockfd);
  62. return 0;
  63. }

三 运行

1 先启动服务端

  1. [root@localhost test]# g++ test.cpp -o test
  2. [root@localhost test]# ./test
  3. waiting data

2 在启动客户端,并输入数据

  1. [root@localhost test]# g++ client.cpp -o client
  2. [root@localhost test]# ./client
  3. please enter data:
  4. 明天你来吗?

3 服务端结果

  1. [root@localhost test]# ./test
  2. waiting data
  3. the data :明天你来吗?
  4. waiting data

发表评论

表情:
评论列表 (有 0 条评论,620人围观)

还没有评论,来说两句吧...

相关阅读