why we don't use client address inside TCP programe? - c

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

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.

why we don't use client address inside TCP program?
Because we usually don't need to. See below.
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.
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.
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.
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.
Please explain the entire concepts about server and client
Too broad.

Related

How to retrieve server program ip by client program by only knowing the port number in a LAN?

I am a rookie trying to learn winsock programming in c. Please tell me if the following is possible.
And sorry if the question is silly.But I really want to know the answer.
I am trying a client program which can search the network for its relative server with only the specified port number . The client must find the IP which provides the service in that specified port and then retrieve that ip back to client program.All this in the same system or in a LAN.
one of my friends said that I must use the Multicast DNS or UDP in the first part of the client to get the ip. After getting the ip then I could create the tcp connection with the server.
Is it really possible to send the clients ip to server through udp and initiate a tcp connection from the server to the client.
I have a basic idea what multicast DNS is.But I am not versed enough to use it in a live program.
Can anyone tell what winsock2 commands I must use for these operations?????
And sorry if I am being silly to your eyes.
one of my friends said that I must use the Multicast DNS or UDP in the first part of the client to get the ip. After getting the ip then I could create the tcp connection with the server.
What your friend is referring to is typically implemented using a UDP subnet broadcast. The client sends a UDP message to the LAN subnet's broadcast IP address on a specific port. IF the server is on the same subnet as the client, and IF it is listening on that UDP port, it will receive the message and can send a response back to the client IP/port that sent the message. When the client receives that response, it will know the server's IP and can then connect to the server's TCP port.
Another option is IF the server uses Multicast to broadcast its TCP IP/port periodically. A client could join the multicast group, receive the broadcasted IP/port, and then connect to it.
Is it really possible to send the clients ip to server through udp
Yes, by sending a UDP broadcast message to the entire subnet on a given port. All machines listening on that UDP port will receive the same message, and can choose to either ignore it or respond to it.
and initiate a tcp connection from the server to the client.
The client would still have to initiate the connection to the server. The broadcast simply allows the client to discover the server's IP without having to hunt for it.
Can anyone tell what winsock2 commands I must use for these operations?
IF the server can respond to a UDP broadcast, the client can use sendto() or WSASendTo() to send the broadcast, and then use recvfrom() or WSARecvFrom() to receive the response. Use GetAdaptersInfo() or GetAdaptersAddresses() to discover the LAN's subnet broadcast IP address. The broadcast port must be known ahead of time.
IF the server broadcasts its IP using multicast, the client can use setsockopt(IP_ADD_MEMBERSHIP) or setsockopt(MCAST_JOIN_GROUP) to join the multicast group, and then use recvfrom() or WSARecvFrom() to receive the broadcasts.
Of course, in either approach, the actual format of the broadcast message(s) must be known ahead of time.
And IF the server does not support either approach, you will have to resort to a manual brute-force scan of the subnet. Given the client's assigned IP address and subnet mask (again, obtainable from GetAdaptersInfo() or GetAdaptersAddresses()), you can easily calculate the first and last IP addresses of the subnet, and then connect to the TCP port on every IP address of the subnet until you find the server.
Yes, It is possible...
See what you need to do is broadcast initial request(What should be the response , must be known to you.. Kinda like handshaking) in your network on that specific port. Now whichever device will respond that way you will know that..
Make server in such a way that for initial request it would reply with it's IP and details...
If you are in same LAN then only it is possible.

In socket programming in c,why to specify the socket address in server program?

In socket programming in c,why to specify the socket address in server program?
Im unable to understand why to specify socket address in server program because socket address we anyways specify in client program,what is the need to specify in server program.
Here is the code:
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
serv_addr.sin_port=htons(atoi(argv[2]));
Most servers don't specify the socket address explicitly, they use INADDR_ANY (as #ybo addresses).
The reason a server might specify the address, however, is to control which interface clients arrive on. For example, you might bind to the address 127.0.0.1 (localhost) to ensure that clients are running on the local machine only, reducing a security risk associated with remote connections. You also might bind explicitly to an external port in order to better sandbox remote clients.
You don't have to, you can use INADDR_ANY instead of the real address, but it can be useful when you have multiple network interfaces on your machine.
Actually same answer as the rest, but in other words:
A server usually just uses 1 public IP address. And also has 1 or more internal IP addresses (like localhost 127.0.0.1 and maybe for lan 192.168.0.1).
But a server can easily also have multiple public IP addresses. Your hosting provider will give these to you (and may be charge you for them.)
A server even NEEDS multiple public IP addresses if it will host multiple HTTPS certificates on port 443, as each one is bound to a specific IP address.
When listening , you can listen on 1 specific IP address, and thus ingore traffic from the other IP addresses. You can even have other applications use the same port number on the other IP adresses.
If for security reasons you only want applications to connect from localhost (eg client and server are on the same machine), you are better off listening only on 127.0.0.1 rather than ALL ip's.
Your computer may have many IP addresses. (Even 127.0.0.1 can be thought of as a separate IP from your "real" one.) On the server socket you can choose which of these addresses you're "listening" to. Following the above example, I believe that binding a server socket to 127.0.0.1 means you'll only be able to connect to that server program locally.

Client and Server fail to communicate

I have a client/server program in c. While the server runs, I can send it command via telnet and it works fine. In addition, nmap reports the port to be open. Similarly, I can successfully use (connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) if my IP is the address of google. However, If I try and use 127.0.0.1 to connect I get a -1 response from connect(). I'm using ports in the 13000 range.
Is there a reason why telnet would be able to communicate with my server but my client program could not?
You either have a local firewall that is preventing your client program from connecting (you may need to whitelist the client program if this is on Windows) or you're filling in the IP address you pass to connect incorrectly. Depending on the OS you're using you should either check errno or GetLastError() to see what went wrong.
You'd better get details about the problem with strerror(errno) if under linux, many reasons may lead to the disconnection.

IP address/MAC of a client

Is it possible to get the IP address of client machine from sockaddr.
From client I mean a system or browser using which a user is connected to a server.
I assume that client is behind some proxy/routers and is not directly connected to the server.
You can get the IP address that the server sends it response packets to. Often this is the IP address of the client computer, but it doesn't have to be:
IPv4 NAT devices (often used by corporate networks) hides the client's IP address and exposes an address of the NAT device instead.
6-to-4 translation. If the client is a native IPv6-only client your IPv4 server will see the IPv4 address of the 6-to-4 gateway that provides IPv4 connectivity.
Proxies where the client isn't directly connected to the Internet, but rather connects to a proxy server that performs requests on behalf of the server. In this case the server will see the proxy servers address.
If you need the IP address of the client computer, the client will have to send that as data to the server. Please note that there is no guarantee that the address you receive this way is the real one. Even if it is the real one, it cannot be guaranteed to be unique (many corporate networks use addresses in the 192.168.X.X series reserved for internal use). There is also no very likely that you won't be able to connect to the client on it's internal address.
Check this answer to a the same question (if i understand your question correctly) Getting IPV4 address from a sockaddr structure
socket.h states:
int accept (int socket, struct sockaddr *address,
socklen_t *address_len);
address: Either a null pointer, or a pointer to a sockaddr structure where the address of the connecting socket will be returned.

Do TCP connections get moved to another port after they are opened? [duplicate]

This question already has answers here:
Does the port change when a server accepts a TCP connection?
(3 answers)
Closed 8 years ago.
If a TCP socket server listens on port 28081 for incoming connections and then accepts a connection and start receiving data. Is the port that data is coming into still 28081 or does the port get changed.
for example what port does the incoming data come to in the pseudo code below? Is it still 28081 or does the OS assign a new port?:
bind
listen (on port 28081)
while 1
fd = accept
fork
if child process incoming data
A TCP connection is uniquely identified by two (IP address, TCP port) tuples (one for each endpoint). So by definition, one can't move a port or IP address of a connection but just open a different one.
If the server binds to port 28081 all accepted connections will have this port on the server side (although they most likely will have varying port numbers on the client side).
For example, if two processes from the same client machine will connect to the same server, the IP address and TCP port on the server side will be the same for both connections. On the client side however, they will have two different port numbers allowing the operating system on both sides to uniquely identify which process and file descriptor the received TCP packets should be assigned to.
Yes, it stays on that port, though some protocols (FTP) might open a second connection on another port. Dont think of a port as a physical path or plug, like a USB port that can only have one thing plugged into it. But rather think of it as an identifier for the service being requested.
Often, though, the new socket connection is passed off to another thread which handles the read/writes for that specific connection.
There can be more than one client connecting to one port, as the connection is identified by both the server and client IP address and port. So, accepting the connection from one client does not block others from connecting. You could even connect another time from the same client (using another client port).

Resources