-3

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

tech_123
  • 90
  • 1
  • 10
  • 1
    Take a look to [How the clients (client sockets) are identified?](http://stackoverflow.com/questions/2439472/how-the-clients-client-sockets-are-identified) – David Ranieri Apr 11 '15 at 08:55
  • 3
    You are basically asking how TCP works. Why don't you read a book about it first. – Philip Stuyck Apr 11 '15 at 12:58

2 Answers2

3

I know that client program initialize the port and ipaddress of server . Is it true ?

The client app must specify the specific remote server IP/port to connect to, yes.

why we use port and ipaddress inside server program ?

A server app may specify a specific local port that it wants to listen on. If a port is not specified, the OS picks a random available port. Either way, this is the port that clients connect to.

The server machine may have multiple local IPs, if it is attached to multiple networks, so the server app may specify a specific local IP to listen on. If an IP is not specified, the server listens on all local IPs. This is used to control which network(s) clients are allowed to connect from.

why we don't use client port and ipaddress inside client program ?

You can, if you need to. This is optional.

A client machine may have multiple local IPs, if it is attached to multiple networks. The client may specify a specific local IP to connect from, if it knows the specific network connection that reaches the server. If an IP is not specified, the OS uses its internal routing tables to figure out which network to use and then connects from that local IP.

A client app may specify a local port that it wants to connect from. Some protocols require this, or a firewall/router policy may require it. If a port is not specified, the OS picks a random available port. Either way, this is the port that the client connects from.

how server identifies this port and address of client machine ?

accept() and getpeername() report the remote IP/port of a client that is communicating with the server.

Please Explain the entire concepts about server and client

There are entire books on that subject. It is out of scope for StackOverflow.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-3
  1. why we don't use client address inside TCP program?

Because we usually don't need to. See below.

  1. I know that client program initialize the port and ipaddress of server. Is it true?

The client needs to know the IP address and port of the server, and use that to initialize a data structure to connect to it, but it isn't accurate to say that the client initializes anything of the server in any way.

  1. If it is true then why we use port and ipaddress inside server program? I am confused.

The server needs to initialize its own IP address and port so that it can listen to it. The client needs to iniatialize another data structure in its own memory space to connect to the server. Just because it has the same name doesn't mean it is the same piece of memory.

  1. another question why we don't use client port and ipaddress inside client program?

The client doesn't need to know its own address and port.

  1. how server identifies this port and address of client machine?

It doesn't. It doesn't need to know. It has a socket which is connected to the client. In most cases that's all it needs. The client sends a request on the connection; the server replies over the same connection.

  1. Please explain the entire concepts about server and client

Too broad.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 2
    This is unfair. His questions are good ones even if poorly stated. I'd expect someone with 139k of reputation to spend his time editing and improving the _question_ rather than in a sarcastic reply. – Brian White Apr 11 '15 at 21:20
  • @BrianWhite I have answered all his good questions. Your comment baffles me completely. – user207421 Oct 17 '16 at 02:14
  • It wasn't the factual information. It was the critical remarks that have since been edited out. But I think you knew that or you wouldn't have done the editing. – Brian White Oct 17 '16 at 23:24