Can two processes on same machine connect to the same port? - c

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.

Related

SQL client seems to be using random port to connect

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.

Port number 5300 not woking in DNS client

I'm making a DNS client and connecting it to a server. Whenever I provide port number 53(default for DNS server) it works fine.. But, whenever I provide port number 5300, the program freezes. Port number 5300 is asked to provide in the question for which I'm doing this code.
So, can anybody help why does my client freezes when I provide port 5300?
I'm using C language.
Thanks
Without looking at your code, I can't be 100% certain. I'm assuming that when you say you switch to port 5300, you're setting in both the client AND the server. Also, is the port listening on the server UDP instead of TCP? If you haven't already, try running netstat on your server to see what ports it is listening on.

Setting a range for ports for a client side connection in c

I am trying to write a TCP proxy in c. The server only works with non-ephemeral ports. The proxy is trying to mount a file-system. So when I call the mount command by-passing the proxy it chooses an appropriate port and it connects. But when I try to connect via the proxy an appropriate port is selected for the connection between the client and the proxy, but the proxy uses an ephemeral port when it connects to the server.
I have read that it is not wise to specify a port, so I am looking to define a range of ports for it to choose from. Is this possible?
Any advice and help is appreciated. Thanks.
I figured it out. I needed to bind to a reserved port prior to running the connect to the server.
use bindresvport(3) with the server socket.

Socket server not accepting clients from other computers (connect failed: 10060)

I am trying to use this c socket class, but it only works when I use it on my own computer.
Desktop only
Server is started like this: cSocketServer -p:2030 -i:192.168.178.22
Client connects: cSocketclient -p:2030 -s:192.168.178.22
Works fine.
Desktop server, laptop client
Server: cSocketServer -p:2030 -i:192.168.178.22
Client: cSocketclient -p:2030 -s:192.168.178.22
Exact same as above, but this fires the connect failed: 10060 error. Which essentially means it timed out.
Desktop only (external address)
Server: cSocketServer -p:2030 -i:192.168.178.22
Client: cSocketclient -p:2030 -s:xx.xx.xx.xx
Where xx.xx.xx.xx is my external ip address.
Same error: connect failed: 10060. Port 2030 is definitely open and accessible, because I tested it with a few unrelated applications that allow their users to choose their own ports (like utorrent). While those run, whatismyip.org states port 2030 is open. But when I run my application it sais it Timed-out. Those applications do not have any special privileges in the firewall.
But even if I did mess up some firewall/router settings (which I'm fairly sure I didn't) that wouldn't explain why I can't connect to the server from within my local network. Other services (such as file sharing) work fine so there is definitely a connection between the 2 computers.
Both client and server run on windows 7 64-bit.
Also; for some reason, each client that connects gets their own inbound port assigned or something? Is that normal? When clients connect the server states;
Accepted client: 192.168.178.22:55156
Accepted client: 192.168.178.22:55164
Accepted client: 192.168.178.22:55176
What's that all about?
If two TCP connections have the same source IP, destination IP, source port, and destination port, there would be no way to tell them apart. To ensure they differ somewhere, clients typically assign a unique source port to every outbound connection they make.
As for the errors, you really need to do some troubleshooting. Do the listening sockets show up in a 'netstat'? Do you get the same problem with the firewalls turned off? Are the server and client on the same LAN (for the internal address case)? Is port forwarding enabled and working in the router (for the external address case)?
My bet is that the external address case won't work because you haven't configured the port to be forwarded by your router or your router doesn't support hairpin (local access to external IP). Other programs may work because they support UPnP or don't rely on hairpin (all access to external IPs come from outside your LAN).
I have no immediate explanation for why your desktop-to-laptop won't work inside your LAN. Are you sure both computers are in the same LAN? Can they ping each other?
Get rid of the -i argument to the server, or specify 0.0.0.0 and fix the code so that isn't considered an error, which is itself an error.

sent data to paticular socket from server when have multiple client

I have 4 different application running.so every application can send data to server. now i want to send data to paticular socket (server and client are done through socket programming)
from server side.
When you create a socket, first you establish a connection between a server and a client (using connect system call on the client side, and bind, listen, and accept system calls on the server side). You can have many such connections, from a server to different clients. The server can send data on any of these established connections.
The Sockets Tutorial can assist you in this case.
If you want multiple reads/writes by the server to happen at the same time, you have to use non-blocking sockets or multiple threads.
(Assuming you're using TCP/IP, or UDP).
A socket endpoint is the IP address and port number combined. So, on your host you would have your various servers listen on different port numbers. For example a web server may listen on port 80, ssh on a different port etc.

Resources