How to establish Socket Connection between Mobile device and Server - tcp

For TCP Socket, the Server is connected to the Client through the Socket.
But if the client is a mobile phone, and because its IP address keep changing would that break the socket between it and the server?
From the Server point of view, does it care the IP addressed of the Client?

If you are using TCP/IP, server need not know client's IP address.Its client which should know server's ip address and connect itself .Whenever IP will be changed for client,client will have next connect() and server will have next accept() call.

Related

In HTTP communication does server know client's IP address to send the response?

Once client sends an HTTP request to server, it disconnects from the server and waits for a response. Does server store clients IP and mac address to send the response? Is the clients information stored when the TCP/IP connection is established?
Every tcp/ip packet has source and destination in it. When the listening socket is created and receives the packet it has the source address from the packet. After that it uses that data to send the response.
The MAC address is in the packet as well. If you are on the same vlan it will be the source MAC address. If you are on a different network segment it will be the MAC address of the router. The source MAC address will be used in the response as well. However, the are other things at play with the MAC address like arp tables etc.
You should read about tcp/ip packets, sockets, and basic networking.

C# tcp server client

Okay so I have a tcp running on port 1302. I've port forwarded the servers IP on my router. But how would the client and server send messages to each other... Wouldn't the client have to port forward too. If so, is there any way around this?
I assume you have a server in local network (i.e. without global IP-address) and you set port-forwarding on your router to have clients sending message to your app which is running on the server on port 1302, right? If so, clients don't need to set their port-forwarding cause it is done automatically by their local provider network. It is typical: server (listener of income messages) without global IP needs port-forwarding but client (who is first to send message) doesn't need port-forwarding.

How does a browser establish connection with a web server on 80 port? Details?

(This question is inspired by a response to this thread: How WebSocket server handles multiple incoming connection requests?)
My understanding is this way:
Assume client IP = 1.1.1.1, server IP = 9.9.9.9
Browser choose a random local available port, say 5555, and initiate a connection to server's port 80. So on client, the socketfd_client should represent an IP connection like (1.1.1.1:5555, 9.9.9.9:80, TCP).
Server calls accept() on its port 80 and identified the connection request from client. Then server picks a random local available port, say 8888, to fulfill that connection request. So on server, the socketfd_server should represent an IP connection like (1.1.1.1:5555, 9.9.9.9:8888, TCP).
My question is:
If my above understanding is correct, socektfd_client and socketfd_server have different server port. Client has 80 while server has 8888. How could the communication be carried out? I think client should change to use the server port 8888 as well, but when and how?
Browser choose a random local available port, say 5555
No. The operating system does that: specifically, the TCP part of the network stack.
and initiate a connection to server's port 80. So on client, the socketfd_client should represent an IP connection like (1.1.1.1:5555, 9.9.9.9:80, TCP).
Correct.
Server calls accept() on its port 80 and identified the connection request from client.
Correct.
Then server picks a random local available port, say 8888
No.
to fulfill that connection request.
No.
So on server, the socketfd_server should represent an IP connection like (1.1.1.1:5555, 9.9.9.9:8888, TCP).
No. The connection at both ends is represented by {1.1.1.1:5555, 9.9.9.9:80}. There is no new port at the server end.
My question is:
If my above understanding is correct
It isn't.
socektfd_client and socketfd_server have different server port.
No.
Client has 80 while server has 8888. How could the communication be carried out? I think client should change to use the server port 8888 as well, but when and how?
Never.

How do browsers detect which HTTP response is theirs?

Given that you have multiple web browsers running, all which obviously listen on port 80, how would a browser figure if an incoming HTTP response was originated by itself? And whether or not catch the response and show it?
As part of the connection process a TCP/IP connection is assigned a client port. Browsers do not "listen on port 80"; rather a browser/clients initiate a request to port 80 on the server and waits for a reply on the client port from the server's IP.
After the client port is assigned (locally), each client [TCP/IP] connection is uniquely identified by (server IP, server port, client IP, client port) and the connection (and response sent over such) can be "connected back" to the correct browser. This same connection-identifying tuple is how a server doesn't confuse multiple requests coming from the same client/IP1
HTTP sits on top of the TCP/IP layer and doesn't have to concern itself with mixing up connection streams. (HTTP/2 introduces multiplexing, but that is a different beast and only affects connection from the same browser.)
See The Ephemeral Port Range for an overview:
A TCP/IPv4 connection consists of two endpoints, and each endpoint consists of an IP address and a port number. Therefore, when a client user connects to a server computer, an established connection can be thought of as the 4-tuple of (server IP, server port, client IP, client port). Usually three of the four are readily known -- client machine uses its own IP address and when connecting to a remote service, the server machine's IP address and service port number are required [leaving only the client port unknown and to be automatically assigned].
What is not immediately evident is that when a connection is established that the client side of the connection uses a port number. Unless a client program explicitly requests a specific port number, the port number used is an ephemeral port number. Ephemeral ports are temporary ports assigned by a machine's IP stack, and are assigned from a designated range of ports for this purpose. When the connection terminates, the ephemeral port is available for reuse, although most IP stacks won't reuse that port number until the entire pool of ephemeral ports have been used. So, if the client program reconnects, it will be assigned a different ephemeral port number for its side of the new connection.
See TCP/IP Client (Ephemeral) Ports and Client/Server Application Port Use for an additional gentle explanation:
To know where to send the reply, the server must know the port number the client is using. This [client port] is supplied by the client as the Source Port in the request, and then used by the server as the destination port to send the reply. Client processes don't use well-known or registered ports. Instead, each client process is assigned a temporary port number for its use. This is commonly called an ephemeral port number.
1 If there are multiple client computers (ie. different TCP/IP stacks each assigning possibly-duplicate ephemeral ports) using the same external IP then something like Network Address Translation must be used so the server still has a unique tuple per connection:
Network address translation (NAT) is a methodology of modifying network address information in Internet Protocol (IP) datagram packet headers while they are in transit across a traffic routing device for the purpose of remapping one IP address space into another.
thank you all for answers.
the hole listening thing over port 80 was my bad,I must have been dizzy last night :D
anyway,as I have read HTTP is connectionless.
browser initiates an HTTP request and after a request is made, the client disconnects from >the server and waits for a response. The server process the request and re-establish the >connection with the client to send response back.
therefor the browser does not maintain connection waiting for a response.so the answer is not that easy to just send the response back to the open socket.
here's the source
Pay attention browesers aren't listening on specific port to receive HTTP response. Web server listening on specific ports (usually 80 or 443). Browser open connection to web server, and send HTTP request to web server. Browser don't close connection before receive HTTP response. Web server writes HTTP response on opened connection.
Given that you have multiple web browsers running, all which obviously listen on port 80
Not obvious: just wrong. The HTTP server listens on port 80. The browsers connect to port 80.
how would a browser figure if an incoming HTTP response was originated by itself?
Because it comes back on the same connection and socket that was used to send the request.
And whether or not catch the response and show it?
Anything that comes back on the connected socket belongs to the guy who connected the socket.
And in any case all this is the function of TCP, not the browser.

After requesting a TCP connection request to a server, can client receive the reply on another port generated by server at its end

When TCP client requests conn'n on server's listening port, server will accept it and create a new port meant for this conn'n with this client. Hence forth the client will communicate with server on this new port.
if the above statement is true and possible, how server conveys the newly generated port to client. In reply to the conn'n request the packet from server to client will have what port as source port (Server's listening port OR New port generated by server for client).
Will Client accept this port and take into use or it will give error ? I need this to implement an architecture having 2 clients and one server in an embedded system using lwip stack.
regards,
ED
The server doesn't create a new port. It creates a new TCP connection and it sends its reply packets to the IP and port the client sent its connection request from. (A TCP connection has an IP address and port on each side.)
When you connect to a server, you get a port number yourself, which is assigned to you by the system (unless you bind the socket before connecting). When the network stack of the server replies to your connection request, the "source" port is the new port number of the server, and the "destination" port of the message is your port. That's how the network stack on the client side knows what port the server has.
The new port number on the server used for your connection can not be set or changed by the actual server program, it's the network stack on the server machine that just grabs an available port number.
Edit: You might also want to read up a little on how connections are established, a.k.a. the three-way handshake.

Resources