UNIX Network Programming Chapter I Demo

Source: Internet
Author: User
Tags htons

It was thought that the time_wait state was the side of the active shutdown. Then this port is not available. I actually found the server listening on a port. After the client sends the connection. After the data is transmitted. After the server closes the client socket. With Netstat-nat | The grep port looks at the resulting time_wait. However, the client can continue to connect to the server. Then the server continues to shut down. It does not affect the listening port. It turns out that the listener port descriptor is turned off only by the servernot accept the connection. Although, the ports are the same), start this port again within 2MLS time: You will be prompted to address already in use In addition Inet_pton (Af_inet,ip address, &serv_addr.sin_addr);
  
 
  1. #include "unp.h"
  2. int main(int argc,char *argv[])
  3. { if(argc<2)
  4. {
  5. printf("please input server_ip\n");
  6. return 0;
  7. }
  8. int fd=socket(AF_INET,SOCK_STREAM,0);
  9. if(fd<0)
  10. err_quit("socket create error");
  11. struct sockaddr_in servaddr;
  12. struct sockaddr_in clientaddr;
  13. clientaddr.sin_family=AF_INET;
  14. clientaddr.sin_port=htonl(5900);
  15. clientaddr.sin_addr.s_addr=htons(INADDR_ANY);
  16. bind(fd,(SA*)&clientaddr,sizeof(clientaddr));`
  17. servaddr.sin_family=AF_INET;//message proctoal
  18. servaddr.sin_port=htons(13);
  19. if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<0)
  20. err_quit("inet_pton error");
  21. if(connect(fd,(SA*)&servaddr,sizeof(servaddr))<0)
  22. err_quit("connect error");
  23. char buf[1024];
  24. int read_length=0;
  25. int count=0;
  26. while((read_length=read(fd,buf,sizeof(buf)))>0)
  27. { count++;
  28. buf[read_length]=‘\0‘;
  29. printf("%s\n read %d ",buf,count);
  30. }
  31. printf("count=%d\n",count);
  32. if(read_length<0)
  33.      err-quit("error of read");
  34.      close(fd);
  35.      return 0;

Service side
  
 
  1. #include "unp.h"
  2. #include <time.h>
  3. int main(int argc,char *argv[])
  4. {
  5. int listenfd=Socket(AF_INET,SOCK_STREAM,0);
  6. struct sockaddr_in servaddr;
  7. servaddr.sin_family=AF_INET;
  8. inet_pton(AF_INET,"0.0.0.0",&servaddr.sin_addr);
  9. servaddr.sin_port=htons(13);
  10. Bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
  11. Listen(listenfd,10);
  12. time_t ticks;
  13. int i;
  14. char buf[1024];
  15. for(;;)
  16. {
  17. int client_fd=accept(listenfd,NULL,0);
  18. ticks=time(NULL);
  19. snprintf(buf,sizeof(buf),"%.24s\r\n",ctime(&ticks));
  20. for(i=0;i<strlen(buf);++i)
  21. Write(client_fd,&buf[i],1);
  22. Close(client_fd);
  23. //Close(listenfd);
  24. }
  25. return 0;
  26. }
  27. ~
1.5 Different TCP handles the data differently, and each read will find that count is different.


From for notes (Wiz)

UNIX Network Programming Chapter I Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.