I am beginner in TCP socket programming . I want to clarify some doubt about TCP programming concepts . I have a client and server program . This is my C code . server code :
#define MYPORT 39937
struct sockaddr_in serveraddress,cliaddr;
sd = socket( AF_INET, SOCK_STREAM, 0 );
memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(MYPORT);//PORT NO
serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);//ADDRESS
retbind=bind(sd,(struct sockaddr*)&serveraddress,sizeof(serveraddress));
connfd=accept(sd,(struct sockaddr*)&cliaddr,&len);
client code :
struct sockaddr_in serveraddress;
sd = socket( AF_INET, SOCK_STREAM, 0 );
argv[1]//for ip address
argv[2]//for port
argv[3]//for string to send
memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(V[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS
if(connect(sd,(struct sockaddr*)&serveraddress,
sizeof(serveraddress)) < 0)
{
printf("Cannot Connect to server");
exit(1);
}
write(sd, V[3], strlen(V[3]));
I know that client program initialize the port and ipaddress of server . Is it true ? if it is true then why we use port and ipaddress inside server program ? I am confused . another question why we don't use client port and ipaddress inside client program ? how server identifies this port and address of client machine ? Please Explain the entire concepts about server and client