I wrote a c# app that listens to port 8000 and 8001.
There are two different applications that connects to 8000 and 8001.
I have used socket arrays so that any number of connections are accepted.
Will I be able to some how attach one 8000 connection and 8001 connection in such a way that what ever data send by the application connected at 8000 will be received by the app at 8001 and vise versa. What is this concept known as ? sorry if this is a very basic question.
That sounds quite a bit like a proxy or a tunneler.
http://en.wikipedia.org/wiki/Proxy_server
In which case, yes this is entirely possible.
Though, if you're allowing multiple connections on both ports how will you know which connection goes with which? Say there are connections A, B and C on 8000 and X, Y and Z on 8001. Does the data from A go to Y and visa versa or does the data from B go to Y and visa versa?
In Java there are PipedInputStream and PipedOutputStream. Maybe the C# library offers something similar?
Related
What I want to do is to allow two separate entities that can not connect directly (let's say they're both behind a NAT) to be able to open a socket to each other.
Here is the best solution I can come up with:
A → connects to well known address and requests a connection
A ← response: two port numbers
A → connects to well known address with first port number
Second port number is manually communicated to B (not ideal, but acceptable)
B → connects to well known address with second port number
Messages are proxied between these two ports
Is there some better way to solve this problem? Is there some off-the-shelf solution that implements this or something like it?
I started reading UNIX network programming by W. Richard Stevens and I am very confused between a port and a socket . when I read on internet it said that socket is an endpoint for a connection and for port number it was written that , IP address and port no form a unique pair .
So now my question is that :
(1) What is the difference between these two ?
(2)How are sockets and ports internally manipulated. Are sockets a file ?
(3) How is data sent when we send it using an application ?
(4) If sockets are there then why do we use port numbers ?
Sorry for my English.. Thanks in advance for the reply.
(1) What is the difference between these two ?
A computer running IP networking always has a fixed number of ports -- 65535 TCP ports and 65535 UDP ports. A network packet's header contains a 16-bit unsigned-short field in it specifying which of those ports the packet should be delivered to.
Sockets, on the other hand, are demand-allocated by each program. A socket serves as a handle/interface between the program and the OS's networking stack, and is used to build and specify a context for a particular networking task. A socket may or may not be bound to a port, and it's also possible (and common) to have more than one socket bound to a particular port at the same time.
(2)How are sockets and ports internally manipulated. Are sockets a
file ?
That's totally up to the OS; and different OS's do it different ways. It's unclear what you mean by "a file" in this question, but in general sockets do not have anything to do with the filesystem. On the other hand, one feature of Unix-style OS's is that socket descriptors are also usable in the much same way that filesystem file descriptors are -- i.e. you can pass them to read()/write()/select(), etc and get useful results. Other OS's, such as Windows, do not support that feature and for them you must use a completely separate set of function calls for sockets vs files.
(3) How is data sent when we send it using an application ?
The application calls the send() function (or a similar function such as sendto()), passes in the relevant socket descriptor along with a pointer to the data it wants to send, and then it is up to the network stack to copy that data into a packet and deliver it to the appropriate networking device for transmission.
(4) If sockets are there then why do we use port numbers ?
Because you need a way to communicate with particular programs on other computers, and computer A has no way of knowing what sockets are present (if any) on computer B. But port numbers are fixed, so it is possible for programmers to use them as a rendezvous point for communication -- for example, your web browser knows that a web server is almost certain to be listening for incoming HTTP requests on port 80 whenever the server is running, so it can send its requests to port 80 with a reasonable expectation of getting a useful response back. If it had to specify a socket as a target instead, what would it specify? The server's socket numbers are arbitrary and likely to be different every time the server runs.
1) What is the difference between these two ?
(2)How are sockets and ports internally manipulated. Are sockets a file ?
A socket is (IP+Port):
A socket is like a telephone (i.e. end to end device for communication)
IP is like your telephone number (i.e. address of your socket)
Port is like the person you want to talk to (i.e. the service you want to order from that address)
A socket is part of a process. A process in linux is a file.
(3) How is data sent when we send it using an application ?
Data is sent by converting it to bytes. There is little/big endian problem regarding the ordering in bytes so you have to take this into consideration when coding.
(4) If sockets are there then why do we use port numbers ?
A socket is (address + port) that means the person you want to talk to (port) can be reachable from many telephone numbers (IPs) and thus from many sockets (that does not mean that the person on one telephone number will reply to you the same as the one in the other telephone number because his job here/there may be different).
If I want to use (UDP) sockets as an inter-process communication mechanism on a single PC, are there restrictions on what I can set up due to the two endpoints having the same IP address?
I imagine that in order to have two processes A and B both listening on the same IP/port address, SO_REUSADDR would be necessary - correct? And even though that might conceptually allow for full duplex comms over a single socket, there are other questions I have if I try to go full duplex:
would I end up receiving my own transmissions, and have to filter them out?
would I be exposing myself to other processes injecting spurious or malicious data into my sockets due to the use of SO_REUSEADDR... or do I face this possibility simply by using (connectionless) UDP?
how would things be different (in an addressing/security/restrictions sense) if I chose to use TCP instead?
I'm confident that there is a viable solution using two sockets at each end (one for A -> B data, one for B ->A data)... but is there a viable solution using a single socket at each end? Would there be any clear advantages to using one full-duplex socket per process if it is possible?
The question arises from a misunderstanding. The misunderstanding arises from reading variable names like receivePort and sendPort with different values, and reading them as if they have an implicit link to the socket at the local end. This might make one (mistakenly) believe that two sockets are being used, or must be used - one for send, one for receive. This is wrong - a single socket (at each end) is all that is required.
If using variables to refer to ports on a single host, it is preferable to name them such that it is clear that one is local or pertaining to "this" process, and the other is remote or peer and pertains to the address of a different process, despite being on the same local host. Then it should be clearer that, like any socket, it is entirely possibly to support both send and receive from the single socket with its single port number.
In this scenario (inter-process communication on the same host necessarily using different port numbers for the single socket at each end) all the other questions (SO_REUSEADDR, TCP vs UDP and receiving one's own transmissions) are distractions arising from the misunderstanding.
I have a scenario which requires the use of a TCP Relay. Before I set out to write something custom, I wanted to see if anyone knows of existing software that can do this for me.
I have 2 devices on separate networks that cannot connect to each other. Let's call them networks A and B. These devices need to communicate, and they can do so via a "middleman" relay on network C. A can connect to C, and B can connect to C. C cannot connect to either A or B.
A -> C <- B
The idea is as follows:
A establishes a TCP connection to C and simply waits
B establishes a TCP connection to C when it wants something from A.
C reads the data from B and responds with it to the already open connection from A.
A processes the data and responds to C, which relays to B.
Is there an existing tool out there that can do this?
As explained here: https://serverfault.com/questions/634516/existing-tcp-relay-solutions/634519
socat TCP4-LISTEN:12345 TCP4-LISTEN:54321
(where 12345 and 54321 are the ports on which the server listens for each connection). One of the clients connects to one port, the other on the other port, and then data is exchanged in both directions. If one machine sends data before the other connects, it is buffered and sent after the connection.
TCP uses port numbers to identify sending and receiving application end-points on a host, or Internet sockets. Each side of a TCP connection has an associated 16-bit unsigned port number (0-65535) reserved by the sending or receiving application
Now if we want to create tcp connection and keep it alive i cannot go more then 65535
What should be the best strategy to cross the limit 65k?
adding multiple interface can increase the possibility of creating more connection is there any other stategy
TCP requires that the tuple (server-ip, server-port, client-ip, client-port) is different for each connection. You can change any one of those to get a new connection. A different server-ip is OK, as is a different client-port. The two port ranges alone give you 2^16*2^16 ~ 4 billion connections.
Each side of a TCP connection has an associated 16-bit unsigned port number (0-65535) reserved by the sending or receiving application
No. (1) It is 1-65535, not 0-65535. (2) Client side ports are usually reserved by the operating system, not by the application. (3) There is no such thing as the 'sending or receiving application'. There are client and server applications.
If we want to create tcp connection and keep it alive i cannot go more then 65535
No again. If you want to create connections in a client you cannot create more than 65535 to the same target. if you want to accept connections in a server you can accept as many as you like, subject to the prior limit at the client end in each client.
What should be the best strategy to cross the limit 65k?
65535 is 64k-1, not 65k, and there really isn't any such limit except as above, which isn't any kind of limit in practice. You don't need 64k client connections to the same target.
Just because a port number is limited to 64K value does not mean you are limited to 64K connections maximum. You can connect to the same port on different servers (think of how many websites you visit at a time, they all listen on port 80 or 443), and you can reuse the same local port for multiple connections as long as they are connected to different servers. It is the combination of [LocalIP:LocalPort]+[RemoteIP:RemotePort] that uniquely identifies a TCP connection, so that gives you flexibility to tweak those values to allow more connections.