Chinaunix首页 | 论坛 | 博客
  • 博客访问: 106738
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 188
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-23 19:19
文章分类

全部博文(33)

文章存档

2015年(22)

2014年(11)

我的朋友

分类: C/C++

2015-01-14 09:38:36

原文地址:c语言udp通信实例(linux) 作者:txgc_wm

  1. server

  2. ---------------


  3. #include <stdio.h> /* These are the usual header files */
  4. #include <string.h>
  5. #include <unistd.h> /* for close() */
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <stdlib.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>

  11. #define PORT 1234 /* Port that will be opened */
  12. #define MAXDATASIZE 100 /* Max number of bytes of data */

  13. main()
  14. {
  15. int sockfd; /* socket descriptors */
  16. struct sockaddr_in server; /* server's address information */
  17. struct sockaddr_in client; /* client's address information */
  18. socklen_t sin_size;
  19. int num;
  20. char recvmsg[MAXDATASIZE]; /* buffer for message */
  21. char sendmsg[MAXDATASIZE];
  22. char condition[] = "quit";
  23. /* Creating UDP socket */
  24. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  25. /* handle exception */
  26. perror("Creating socket failed.");
  27. exit(1);
  28. }

  29. bzero(&server,sizeof(server));
  30. server.sin_family=AF_INET;
  31. server.sin_port=htons(PORT);
  32. server.sin_addr.s_addr = htonl (INADDR_ANY);
  33. if (bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
  34. /* handle exception */
  35. perror("Bind error.");
  36. exit(1);
  37. }

  38. sin_size=sizeof(struct sockaddr_in);
  39. while (1) {
  40. num = recvfrom(sockfd,recvmsg,MAXDATASIZE,0,(struct sockaddr *)&client,&sin_size);

  41. if (num < 0){
  42. perror("recvfrom error\n");
  43. exit(1);
  44. }

  45. recvmsg[num] = '\0';
  46. printf("You got a message (%s) from %s\n",recvmsg,inet_ntoa(client.sin_addr) ); /* prints client's IP */
  47.         if(strcmp(recvmsg,condition)==0) break;
  48.    int i=0;
  49. for(i = 0 ; i < num ; i ++)
  50. {
  51.    sendmsg[i] = recvmsg[num-1-i];
  52. }
  53. sendmsg[num]='\0';
  54. sendto(sockfd,sendmsg,strlen(sendmsg),0,(struct sockaddr *)&client,sin_size);

  55. }

  56. close(sockfd); /* close listenfd */
  57. }


  58. ---------------------------------------------------------

  59. client


  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <unistd.h>
  63.         #include <strings.h>
  64. #include <string.h>
  65. #include <sys/types.h>
  66. #include <sys/socket.h>
  67. #include <netinet/in.h>
  68. #include <netdb.h> /* netbd.h is needed for struct hostent =) */

  69. #define PORT 1234 /* Open Port on Remote Host */
  70. #define MAXDATASIZE 100 /* Max number of bytes of data */

  71. int main(int argc, char *argv[])
  72. {
  73. int fd, numbytes; /* files descriptors */
  74. char recvbuf[MAXDATASIZE]; /* buf will store received text */
  75.         char sendbuf[MAXDATASIZE];
  76. struct hostent *he; /* structure that will get information about remote host */
  77. struct sockaddr_in server,client; /* server's address information */

  78. if (argc !=2) { /* this is used because our program will need two argument (IP address and a message */
  79. printf("Usage: %s \n",argv[0]);
  80. exit(1);
  81. }

  82. if ((he=gethostbyname(argv[1]))==NULL){ /* calls gethostbyname() */
  83. printf("gethostbyname() error\n");
  84. exit(1);
  85. }

  86. if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1){ /* calls socket() */
  87. printf("socket() error\n");
  88. exit(1);
  89. }

  90. bzero(&server,sizeof(server));
  91. server.sin_family = AF_INET;
  92. server.sin_port = htons(PORT); /* htons() is needed again */
  93. server.sin_addr = *((struct in_addr *)he->h_addr); /*he->h_addr passes "*he"'s info to "h_addr" */

  94.         socklen_t len;
  95.          len=sizeof(struct sockaddr_in);
  96. while (1) {
  97.    printf("input message:");
  98.          fgets(sendbuf,40,stdin);
  99. sendto(fd,sendbuf,strlen(sendbuf),0,(struct sockaddr *)&server,len);
  100. if ((numbytes=recvfrom(fd,recvbuf,MAXDATASIZE,0,(struct sockaddr *)&server,&len)) == -1){ /* calls recvfrom() */
  101. printf("recvfrom() error\n");
  102. exit(1);
  103. }
  104. recvbuf[numbytes]='\0';
  105. printf("Server Return Reverse Message: %s\n",recvbuf); /* it prints server's welcome message */

  106. }

  107. close(fd); /* close fd */
  108. }


  109. --------------------------------------------------------------------------------

  110. ./client 127.0.0.1

  111. input:test
阅读(1504) | 评论(0) | 转发(0) |
0

上一篇:c语言汉字显示

下一篇:linux c 正则表达式

给主人留下些什么吧!~~