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);
#include "unp.h"
int main(int argc,char *argv[])
{ if(argc<2)
{
printf("please input server_ip\n");
return 0;
}
int fd=socket(AF_INET,SOCK_STREAM,0);
if(fd<0)
err_quit("socket create error");
struct sockaddr_in servaddr;
struct sockaddr_in clientaddr;
clientaddr.sin_family=AF_INET;
clientaddr.sin_port=htonl(5900);
clientaddr.sin_addr.s_addr=htons(INADDR_ANY);
bind(fd,(SA*)&clientaddr,sizeof(clientaddr));`
servaddr.sin_family=AF_INET;//message proctoal
servaddr.sin_port=htons(13);
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<0)
err_quit("inet_pton error");
if(connect(fd,(SA*)&servaddr,sizeof(servaddr))<0)
err_quit("connect error");
char buf[1024];
int read_length=0;
int count=0;
while((read_length=read(fd,buf,sizeof(buf)))>0)
{ count++;
buf[read_length]=‘\0‘;
printf("%s\n read %d ",buf,count);
}
printf("count=%d\n",count);
if(read_length<0)
err-quit("error of read");
close(fd);
return 0;
Service side
#include "unp.h"
#include <time.h>
int main(int argc,char *argv[])
{
int listenfd=Socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in servaddr;
servaddr.sin_family=AF_INET;
inet_pton(AF_INET,"0.0.0.0",&servaddr.sin_addr);
servaddr.sin_port=htons(13);
Bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
Listen(listenfd,10);
time_t ticks;
int i;
char buf[1024];
for(;;)
{
int client_fd=accept(listenfd,NULL,0);
ticks=time(NULL);
snprintf(buf,sizeof(buf),"%.24s\r\n",ctime(&ticks));
for(i=0;i<strlen(buf);++i)
Write(client_fd,&buf[i],1);
Close(client_fd);
//Close(listenfd);
}
return 0;
}
~
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