Let's say we have a server that can accept multiple clients. First, it has to create a socket, then bind it with a port and an IP and finally listen to requests for connection from clients. After accept()ing a connection with a client, the server creates a new socket to communicate with the specific client. My question is whether or not the client is going to send its data to the same port it sent its initial request to, and if not how does it know where to send it?
A socket connection is uniquely identified by a tuple of [Protocol, Local IP, Local Port, Peer IP, Peer Port].
A TCP server creates a listening socket with a tuple of [TCP, Listen IP, Listen Port, 0, 0]. When a client requests to connect to a server, the network routes the request to the specified IP/Port. The receiving device then routes the request to a matching listening socket, performs a 3way handshake with the client, and puts it into a queue. Later, when accept() is called, it extracts the next pending client from the queue and returns a new socket identified with a tuple of [TCP, Listen IP, Listen Port, Client IP, Client Port]. Because of this, a single listening socket can accept multiple Clients from different Client IP/Port combinations.
A TCP client creates a connecting socket with a tuple of [TCP, Local IP, Local Port, 0, 0]. When the 3way handshake is complete, the socket's tuple is updated to [TCP, Local IP, Local Port, Server IP, Server Port]. Because of this, a Client can connect separate sockets to different Servers at differing Server IP/Port combinations.
All subsequent data exchanges use these tuples.
Data sent out from a Client's connecting socket will be sent to the associated Server IP/Port and stored in the buffer of the accepted Server socket whose tuple matches both the Client and Server.
Data sent out from a Server's listening socket will be ignored, since there is no associated Client.
Data sent out from an accepted Server socket will be sent to the associated Client IP/Port and stored in the buffer of the connected Client socket whose tuple matches the Client and Server.
Generally There is always a default port allotted for each kind of communication.Operating System may kept it open or close ,it can be checked .
Let's say for FTP connection, There is a separate port allotted for handshake,It don't matter how many new FTP connection are being requested, all new connection will go to that same port , Once handshake is completed data exchange is done via another port, Even if we don't specify port. If Network manager has Port List entries earlier it will request to the same port.
Example for SSH
if you request for
ssh -X <IP>
Even if you don't mention port , Your system know which port to request for and at server side there is always some port open who will be listening to your request and based on data you send while handshake it will continue listening or block you.
Bonus is you can open your custom port at server side who will be listening to your request. TCP implementation by default declare which port will be used for what kind of communication.
The client connects with a source IP and port to a server with a destination IP and port. After accept exactly the same IP and port on both sides are continued to be used for data exchange as for the establishment of the connection.
I have known that using TCP socket we can make chat server where client and server is going to send and acknowledge data to each other but can we do same thing where single server connect with multiple clients and clients can also transfer data to eachother​!!!
Sorry if this is not the place to ask, but I am not sure if there is a forum for SQL port questions. It seems that my SQL client is using a random port to connect to the SQL server, in spite of me stipulating a specific port in the ODBC DSN connector. this is from Microsoft Network Monitor I have included the image to better illustrate what I mean.
You are reading it wrong. I see two kinds of connections, from WEBSERVER to 192.*.6 which I assume to be the Sql Server (the requests), and vice versa (the responses from Sql back to the Web Server).
When WEBSERVER is making a request, the DstPort is always 49252. The SrcPort fluctuates in that case because that is just how TCP/IP works (and that is also how multiple parallel connections can be distinguished). The Sql Server then always replies to whatever port number initiated the request.
It is expected that the source port (client) be random. The destination port (server) will be constant. When a TCP/IP client connects, it chooses an unused client port and connects to the server listening on a known port. This is basically how TCP/IP sockets work.
Can I create 2 UDP sockets on the same client machine (but will be used in 2 different threads) to connect to a server? One thread sends datagrams to and the other receives datagrams from the server. Are these the correct steps:
create a sockaddr_in for the server's given address and UDP port.
create a socket and connect it to the server's sockaddr_in. This
is the sending socket.
Create another socket, assign a port and
sent the port number to the server.
Thank you.
I have a few basic questions about sockets programming. I am trying to write a program (in C, linux) in which several client processes connect to a server process and also the clients need to connect to themselves. Though the clients would reside on separate machines, them being on the same machine is also a case. These are my questions.
Can two client processes communicate with each other on the separate port? In that case none of them listens to a port (like how the server does). They just connect to a port using a socket and talk to each other. Is that possible?
If not, how can I make communication between the clients?
Any idea on this would be of help. Thanks.
no, in TCP this is not possible. When establishing the connection in TCP, you are always connecting to listening port, so one of the clients must listen.
one of the clients needs to open listening port, but which one? Use the server as an arbiter! Employ a protocol where server moderates:
server decides which client will open the port
that client opens the port, listens to it and sends its address (host:port) to the server
servers sends it to the other client
the other client can connect now!
And if you were asking if two processes can listen on the same port on the same machine, then the answer is no. But using the above protocol you can avoid this situation.
A socket connection always needs a connecting and a listening side - one side needs to listen. Have one client process create a listening port and the other connect to this port.
If you want to make clients to communicate each other, make use of a server which listens on a port and directs to the other client nothing but to direct to right clients.
If you dont want to use a server, then in you client application u have to make one port for listening and another port for spitting data. So it will be only one-one talking.
If I'm correct for the answer you are looking for: Yes, two processes on same machine can connecto to same port. It is nothing but two different entities trying to connect to a server.