I have this simple TCP echo server, and whenever a client connects to it, it displays the client's IP and port. But when I run netstat -a
, a different port for the client is shown. I'm running the server and client on the same computer.
In my program, it shows client connected: 127.0.0.1:34997
, but the result of netstat -a|grep 6969
is:
tcp 0 0 0.0.0.0:6969 0.0.0.0:* LISTEN
tcp 0 0 localhost:46472 localhost:6969 ESTABLISHED
tcp 0 0 localhost:6969 localhost:46472 ESTABLISHED
The code of echo server is:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc,char **argv){
int listenfd,confd,n;
struct sockaddr_in server,client;
pid_t pid;
char buffer[100];
memset(&server,0,sizeof server);
server.sin_family=AF_INET;
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port=htons(6969);
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(bind(listenfd,(struct sockaddr *)&server,sizeof server)==-1){
perror("bind error");return-1;
}
if(listen(listenfd,20)==-1){
perror("listen error");return -1;
}
printf("listening for connection..\n");
for(;;){
socklen_t cllen = sizeof(client);
confd=accept(listenfd,(struct sockaddr *)&client,&cllen);
if((pid=fork())==0){
printf("client connected: %s:%d\n",inet_ntoa(client.sin_addr),client.sin_port);
close(listenfd);
for(;;){
n=read(confd,&buffer,sizeof buffer);
if(n==0) break;
write(confd,&buffer,n);
}
printf("client disconnected: %s:%d",inet_ntoa(client.sin_addr),client.sin_port);
exit(0);
}
close(confd);
}
}