当前位置:   article > 正文

unix环境编程 tcp通信_unix tcp发送数据

unix tcp发送数据

TCP通信的过程为:

服务器端:

   1.socket(int domain,int type,int protocol):建立套接字;

   2 .bind(int sockid,struct sockaddr *addrp,socklen_t addrlen):绑定端口和地址信息

   3.listen(int sockid,int qsize):监听套接字;

   4.fd=accept(int sockid,struct sockaddr *callerid,socklen_t *addrlenp):等待套接字;

   5.recv(int fd,void *buf,size_t nbytes,int flags):从套接字接收数据;

   6.close(fd) close(sockid) 关闭套接字

  .客户端:

   1. socket():建立套接字;

   2.connect(int sockid,struct sockaddr *serv_addrp,socklen_t addrlen):连接到服务器;

   3. send(int sockfd,const void *buf,size_t nbytes,int flags):发送数据到服务器.

   4. close(sockid);关闭套接字

TCP通信服务器端程序:

  1. # include <stdio.h>
  2. # include <sys/socket.h>
  3. # include <arpa/inet.h>
  4. # include <string.h>
  5. # define MAX_SIZE 256
  6. int main()
  7. {
  8. int err;
  9. int listenfd,connfd;
  10. struct sockaddr_in servaddr,clientaddr;
  11. int n,addrlen=sizeof(struct sockaddr_in);
  12. char addr[16];
  13. char rbuf[MAX_SIZE];
  14. char sbuf[MAX_SIZE]="hello from server";
  15. inet_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr);
  16. memset(&servaddr,0,sizeof(servaddr));
  17. servaddr.sin_family=AF_INET;
  18. servaddr.sin_port=htons(10000);
  19. listenfd=socket(AF_INET,SOCK_STREAM,0);
  20. if(listenfd==-1)
  21. {
  22. perror("socket");
  23. return -1;
  24. }
  25. err=bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
  26. if(err==-1)
  27. {
  28. perror("bind");
  29. return -1;
  30. }
  31. err=listen(listenfd,20);
  32. if(err==-1)
  33. {
  34. perror("listen");
  35. return -1;
  36. }
  37. while(1)
  38. {
  39. connfd=accept(listenfd,(struct sockaddr*)&clientaddr,(socklen_t*)&addrlen);
  40. break;
  41. }
  42. n=recv(connfd,rbuf,MAX_SIZE,0);
  43. if(n==-1)
  44. {
  45. perror("recv");
  46. return -1;
  47. }
  48. rbuf[n]='\0';
  49. printf("%s\n",rbuf);
  50. n=send(connfd,sbuf,strlen(sbuf)+1,0);
  51. if(n==-1)
  52. {
  53. perror("send");
  54. return -1;
  55. }
  56. close(connfd);
  57. close(listenfd);
  58. return 0;
  59. }

客户端程序:

  1. # include <stdio.h>
  2. # include <string.h>
  3. # include <sys/socket.h>
  4. # include <arpa/inet.h>
  5. # define MAX_SIZE 256
  6. int main()
  7. {
  8. int err;
  9. int sockfd,n;
  10. char rbuf[MAX_SIZE],sbuf[MAX_SIZE]="hello from client";
  11. struct sockaddr_in servaddr;
  12. memset(&servaddr,0,sizeof(servaddr));
  13. servaddr.sin_family=AF_INET;
  14. servaddr.sin_port=htons(10000);
  15. inet_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr);
  16. sockfd=socket(AF_INET,SOCK_STREAM,0);
  17. if(sockfd==-1)
  18. {
  19. perror("socket");
  20. return -1;
  21. }
  22. err=connect(sockfd,(struct sockaddr*)&servaddr,(socklen_t)sizeof(servaddr));
  23. if(err<0)
  24. {
  25. perror("connect");
  26. return -1;
  27. }
  28. n=send(sockfd,sbuf,strlen(sbuf)+1,0);
  29. if(n<0)
  30. {
  31. perror("send");
  32. return -1;
  33. }
  34. n=recv(sockfd,rbuf,MAX_SIZE,0);
  35. if(n<0)
  36. {
  37. perror("recv");
  38. return -1;
  39. }
  40. rbuf[n]='\0';
  41. printf("%s\n",rbuf);
  42. close(sockfd);
  43. return 0;
  44. }

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

闽ICP备14008679号