I've an application in c# windows forms through which I stream photo taken by a web cam in few seconds interval. The photo data get sent to a server listening on TCP port.
My question is if this application is installed on hundreds of computers, shall there be an issue to listen on one single port or should I assign a different port to each client? Keeping in mind that photos get sent after every few seconds.
Here is my code of server listener.
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Parse("some ip"),5001));
Task.Factory.StartNew(() =>
{
socket.Listen(500);
//socket listening code.
}, TaskCreationOptions.LongRunning);
Thanks.
TCP Connections are characterised by four parameters - source IP Address and Port and destination IP Address and Port. Provided those four are unique, no confusion ensues.
Given that source's are either machines actually assigned unique addresses (more likely with IPv6) or are tunnelling through NAT devices (and thus asigned unique source ports), there's no need to do anything on the server side.
If you're thinking of assigning anything to each connected client (beyond the socket and some buffers), you're probably doing something wrong. Don't dedicate any exclusive resources to the clients.
See: the C10k problem.
Note that when you use a listener port, behind the scenes you end up using an ephemeral port for the established connection after the listener has handed it over - but from the connecting client's perspective, that doesn't matter - the listener port remains a single point of entry for all the eventual sockets.
FWIW: the web-sockets server that drives the real-time updates here on Stack Overflow currently averages 14,500 sockets per port, 3 ports per service instance, 9 service instances, for a total of about 392,000 sockets. We've seen it much higher at some points in the past. So yes, you can easily support a few hundred clients o a single listener port, but: it gets non-trivial the more sockets you want to support, and you absolutely shouldn't use theads per client if you're going over a very small number of clients.
Don't worry you can listen multiple sockets on a same port, so multiple clients can communicate on the same port.
For example, you probably know that when you access on a website, you're connecting to a server on the port 80 (or 443 for https), well... Fortunatly, a website can be accessed by multiple clients, and all on the same port ! ;)
Related
is it possible to have one socket router that would pass incoming sockets to socket servers?
I need this because I want to have multiple servers for handling sockets but only one port for clients to connect to. so if one of the servers goes down, router would send the socket to other healthy socket servers.
Is this even possible and how? or any other solution for my problem?
Yes this is definitely possible, and is used very widespread.
Basically you need to have one TCP server running on that one port you want clients to connect to. Then you can listen for connections. When a connection is established you can reroute packets to other servers (you choose).
Listen on a port with your main server
Accept connections as they come in.
When a connection is opened, read the data in another thread. Take that data and send it to the other server you want to.
Basically, it is a proxy.
I had an argument with a colleague on the selection.
We have two processes running on the same machine.
=> NamedPipe and UDP are KERNEL OBJECT so as far as i understand this is same overhead.
The advantage of UDP is that if tomorrow we will separate those two processes and they will run on two different computers so I do not have to change anything.
I think that the NamedPipe performance are better since there is no need to use a network card to send the information to the same machine (am I right .. sending localhost will use the network card - right ?)
Can anyone advise us please ??
Thanks
Before Implementation , you can care below points :
Named pipes:
Named pipes provide interprocess communication between a pipe server and one or more pipe clients.
They support message-based communication and allow multiple clients to connect simultaneously to the server process using the same pipe name.
Named pipes also support impersonation, which enables connecting processes to use their own permissions on remote servers.
User Datagram Protocol :
User Datagram Protocol (UDP) is a simple protocol that makes a best effort to deliver data to a remote host.
The UDP protocol is a connectionless protocol, UDP datagrams sent to the remote endpoint are not guaranteed to arrive, nor are they guaranteed to arrive in the same sequence in which they are sent.
Applications that use UDP must be prepared to handle missing, duplicate, and out-of-sequence datagrams.
Many questions relating to port 80 being used have answers saying that there are many programs that use it as their default port. This post mentions some: Skype, IIS, Apache...
Since only one application can listen on any one port at a time - How can that be? And if the answer is that that's only their default port - how will an application know it has to send information to a different port? For example - if iis will listen on port 81 because Skype is listening on 80 - how will anyone requesting a web page know to send the request to theip:81 as opposed to theip:80?
My goal is to have a robust way of setting up a connection between programs, when any hard coded port might fail due to some application already listening on it. The port will only need to be used once in order to communicate what dynamic port will be used for the rest of the session. This is a problem for both network connections and for connecting several applications on the same computer.
Registering with IANA is not always possible, and won't even necessarily solve the problem - someone might still be listening on a registered port. And obviously the solution of "hope for no collisions" - just doesn't cut it.
(I do understand that a connection has two sockets (and a protocol) and therefore one socket can have multiple connections. My question is about listening on a socket in order to establish the connection.)
What I would expect, is there to exist some service on the OS (Windows) that I could register my application with, and receive all incoming traffic with some signature - even if it's simply some magic string. Or perhaps some port where multiple applications can listen concurrently - and all would get every incoming message. But I haven't found anything like that so far.
How can that be? Simply...it's not. Only one application will listen on each port. – Adriano Repetti
Right. When Skype listens on those ports before I start my web-server, the server fails. It took me a while to find out why.
Only one app can listen on a socket in a sane way. The OS allows multiple apps to listen on the same port if you specify special options but that's insane. Accepted connections are then dispatched to different applications in an unspecified (i.e. random) way.
IIS can run multiple web-apps on the same port because it opens the port once in kernel mode and dispatches connections to its worker processes.
I do not believe it is ever possible for multiple sockets to listen on the same (TCP) port. If you try to bind a socket to a port that is already open, you will get an error.
I believe Skype gets around the problem you describe by using their own servers as a rendezvous point. The simple explanation being:
Alice starts her client, connects to the central server, and informs it of what port she is listening on.
Bob starts his client and likewise informs the central server.
Now, Alice wants to connect to Bob, but doesn't know which port to send packets to.
Alice will then query the central server for Bob's port number.
With this information, a direct connection is then established with Bob using that port.
The logic can of course extend to learning the other party's IP address as well as even obtaining public keys.
Note that there's actually a bit more involved with most modern peer-to-peer applications, Skype being no exception. The problem being that most computers are now behind at least one NAT router. Getting two devices each behind their own router to connect to each other is known as NAT traversal - the most common technique having a central coordinating server instruct both clients to simultaneously connect to each other. For more information on this, I recommend Steve Gibson's Security Now!, episode #42
on the port forwarding setup page i just put a comma and add an other port to open. it wont allow you to set up an additional rule with listen port 80 but it will allow you to trigger multiple ports with that one listen port
For TCP, you can only have one application listening on a single port
at one time. Now if you had 2 network cards or created a virtual
interface, you could have one application listen on the first IP and
the second one on the second IP using the same port number.
For UDP (Multicasts), multiple applications can subscribe to the same
port.
one application listening on a single port that's the reason why ports exist. To allow multiple applications to share the network without conflicts.
But there are ways to do what you requested:
You could write a master process, which possesses the port and notifies slave processes using some separation logic.
On Linux and BSD you can set up remapping rules that redirect packets from the visible port to different ones(such as listener app), again by using some separation logic(e.g. redirect according to network origin etc.).
Note: For TCP, you multiple applications can listen on the same socket by using SO_REUSEADDR option before binding but what this does is redirect the incoming connection to only one of the listeners.
I am designing a server client app in C#.
the client connect and communicate with the sever threw tcp socket.
in the server side I am using the socket.accept() method in order to handle new connection from client. when client is connecting, the server use a random port in order to communicate with the client.
so my question is.. how many clients the server can receive in this kind of form?
is there another form that I should use in order to handle lots of clients?
This is practically limited by the OS. You have to test this. On Windows you must use fully asynchronous socket IO at this scale. You will probably be limited by memory usage.
On a TCP level there is no practical limit. There can be one connection for each combination of (server port, server ip, client port, client ip). So with one server port and one server ip you can serve an unlimited amount of clients as long as they have less than 65k connections per client.
You do not need to pick a random port on the server. This is a common misconception.
in the server side i am using the socket.accept() method in order to handle new connection from client. when client is connecting, the server use a random port in order to communicate with the client.
Not unless you open another, pointless, connection from server to client, and you won't be doing that for firewall reasons. The accepted socket uses the same local port number as the listening socket. Contrary to several answers and comments here.
Your question is therefore founded on a misconception. Whatever you run out of, and it could be memory, thread handles, socket handles, socket buffer space, CPUs, CPU power, virtual memory, disk space, ..., it won't be TCP ports.
EDIT Adherents of the new-random-port theory need to explain the following netstat output:
TCP 127.0.0.4:8009 0.0.0.0:0 LISTENING
TCP 127.0.0.4:8009 127.0.0.1:53777 ESTABLISHED
TCP 127.0.0.4:8009 127.0.0.1:53793 ESTABLISHED
TCP 127.0.0.4:8009 127.0.0.1:53794 ESTABLISHED
TCP 127.0.0.4:8009 127.0.0.1:53795 ESTABLISHED
TCP 127.0.0.4:8009 127.0.0.1:53796 ESTABLISHED
TCP 127.0.0.4:8009 127.0.0.1:53798 ESTABLISHED
TCP 127.0.0.4:8009 127.0.0.1:53935 ESTABLISHED
and show where in RFC 793 it says anything about allocating a new port to an accepted socket, and where in the TCP connect-handshake exchange the new port number is conveyed.
You may like to see this question I asked in similar vein: https://softwareengineering.stackexchange.com/questions/234672/is-there-are-problem-holding-large-numbers-of-open-socket-connections-for-length, and particularly some of the comments.
The answer seems to be that there is no practical limit. The combination of receive port and send port must be unique, and each of them can have 64K values. The total number of combinations is extremely large. There really are servers out there with extremely large numbers of open connections, but to get there you have to solve a number of other interesting problems. The question above contains a link to an article about a million connection server. See also How to retain one million simultaneous TCP connections?. And do a web search for the C10K problem.
What you probably cannot do is use synchronous ports and threads because you run into thread limits, not port limits. You have to use asynchronous ports and a thread pool. And you will have to write one to throw away, just to find out how to do it.
i am wondering how tcp servers work because there is no multicasting. I am interested in using tcp for my game since it won't require to much packets to be sent like some other games. It still needs to have a client / server architecture though.
Since there is no multicasting, is there just a loop to send everything directly from server to client for every client? Is this what minecraft does (cuz i read it uses tcp)
it was my understanding that only 1 socket can be bound to a port. With udp, the server socket can accept connections from IPAdress.Any, so it can receive information from all clients. Since TCP is connection only, how would this work? Can multiple TCP connections be made on the same socket?
Only one listening connection can exist per port on the server. However, many clients can connect to that one listening port. A "Connection" under the hood is the combination of ServerIP + ServerPort + ClientIP + ClientPort, also the client port does not need to be the same every time (only the server side port needs to stay static), the OS chooses a random high number port and give that to the client side for the connection. That is why you can have many outgoing connections on a client but only one listening connection on the server.
Look at this page for a example on how to set up multiple connections to one port.