当前位置:   article > 正文

TCP socket 编程示例_tcp协议程序设计

tcp协议程序设计

本代码为老师提供的示例程序

一、TCP socket 编程模型

            

二、使用TCP协议实现一个简单的通讯程序

1.服务器端

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<sys/socket.h>
  4. #include<netdb.h>
  5. #include<string.h>
  6. #include<unistd.h>
  7. #include<netinet/in.h>
  8. #include<arpa/inet.h>
  9. #define PORT 8900
  10. int main(int argc,char** argv)
  11. {
  12. struct sockaddr_in server;
  13. struct sockaddr_in client;
  14. int len;
  15. int port;
  16. int listend;
  17. int connectd;
  18. int sendnum;
  19. int opt;
  20. int recvnum;
  21. char send_buf[2048];
  22. char recv_buf[2048];
  23. port= PORT;
  24. memset(send_buf,0,2048);
  25. memset(recv_buf,0,2048);
  26. opt = SO_REUSEADDR;
  27. if (-1==(listend=socket(AF_INET,SOCK_STREAM,0)))
  28. {
  29. perror("create listen socket error\n");
  30. exit(1);
  31. }
  32. setsockopt(listend,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
  33. memset(&server,0,sizeof(struct sockaddr_in));
  34. server.sin_family = AF_INET;
  35. server.sin_addr.s_addr = htonl(INADDR_ANY);
  36. server.sin_port = htons(port);
  37. if (-1==bind(listend,(struct sockaddr *)&server,sizeof(struct sockaddr)))
  38. {
  39. perror("bind error\n");
  40. exit(1);
  41. }
  42. if (-1==listen(listend,5))
  43. {
  44. perror("listen error\n");
  45. exit(1);
  46. }
  47. while (1)
  48. {
  49. if (-1==(connectd=accept(listend,(struct sockaddr*)&client,&len)))
  50. {
  51. perror("create connect socket error\n");
  52. continue;
  53. }
  54. sendnum = sprintf(send_buf,"hello,the guest from %s\n",inet_ntoa(client.sin_addr));
  55. if ( 0 >send(connectd,send_buf,sendnum,0))
  56. {
  57. perror("send error\n");
  58. close(connectd);
  59. continue;
  60. }
  61. if (0>(recvnum = recv(connectd,recv_buf,sizeof(recv_buf),0)))
  62. {
  63. perror("recv error\n");
  64. close(connectd);
  65. continue;
  66. }
  67. recv_buf[recvnum]='\0';
  68. printf ("the message from the client is: %s\n",recv_buf);
  69. if (0==strcmp(recv_buf,"quit"))
  70. {
  71. perror("the client break the server process\n");
  72. close(connectd);
  73. break;
  74. }
  75. sendnum = sprintf(send_buf,"%s\n",recv_buf);
  76. send(connectd,send_buf,sendnum,0);
  77. close(connectd);
  78. continue;
  79. }
  80. close(listend);
  81. return 0;
  82. }

2.客户端

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<sys/socket.h>
  4. #include<netdb.h>
  5. #include<string.h>
  6. #include<unistd.h>
  7. #include<netinet/in.h>
  8. #include<arpa/inet.h>
  9. #define PORT 8900
  10. void print_usage(char * cmd)
  11. {
  12. fprintf(stderr," %s usage:\n",cmd);
  13. fprintf(stderr,"%s IP_Addr [port]\n",cmd);
  14. }
  15. int main(int argc,char** argv)
  16. {
  17. struct sockaddr_in server;
  18. int ret;
  19. int len;
  20. int port;
  21. int sockfd;
  22. int sendnum;
  23. int recvnum;
  24. char send_buf[2048];
  25. char recv_buf[2048];
  26. if ((2>argc)|| (argc >3))
  27. {
  28. print_usage(argv[0]);
  29. exit(1);
  30. }
  31. if (3==argc)
  32. {
  33. port = atoi(argv[2]);
  34. }
  35. if (-1==(sockfd=socket(AF_INET,SOCK_STREAM,0)))
  36. {
  37. perror("can not create socket\n");
  38. exit(1);
  39. }
  40. memset(&server,0,sizeof(struct sockaddr_in));
  41. server.sin_family = AF_INET;
  42. server.sin_addr.s_addr = inet_addr(argv[1]);
  43. server.sin_port = htons(port);
  44. if (0>(ret=connect(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))))
  45. {
  46. perror("connect error");
  47. close(sockfd);
  48. exit(1);
  49. }
  50. printf("what words do you want to tell to server:\n");
  51. fgets(send_buf,2048,stdin);
  52. if (0>(len=send(sockfd,send_buf,strlen(send_buf),0)))
  53. {
  54. perror("send data error\n");
  55. close(sockfd);
  56. exit(1);
  57. }
  58. if (0>(len=recv(sockfd,recv_buf,2048,0)))
  59. {
  60. perror("recv data error\n");
  61. close(sockfd);
  62. exit(1);
  63. }
  64. recv_buf[len]='\0';
  65. printf("the message from the server is:%s\n",recv_buf);
  66. close(sockfd);
  67. }

三、运行结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/147268
推荐阅读
相关标签
  

闽ICP备14008679号