How to know the port number used by c# UdpClient? [duplicate] - c#

This question already has answers here:
How do I find the port number assigned to a UDP client (in .net/C#)?
(2 answers)
Closed 9 years ago.
I am creating a client server application using c sharp. The server is using tcplistener with fixed port number. the client connect to the server using tcpclient. Once connected, both client and server communicate using this connection. The application then create new udp connection to send and receive message. Since the server suppose to accept multiple connection from single client, i have to differentiate each connection with different port. In order to do this, i have to first
1. at server, create a udpclient (automatically use unused udp port at the server).
2. sends the port number used by the server udpclient to the client.
3. the client sends data to the server using specified port number.
The problem is, how to create a udpclient where you can know the port number used?

Here are the answer to my questions.
UdpClient udpClient = new UdpClient(0));
Console.WriteLine("UDP port : " + ((IPEndPoint)udpClient.Client.LocalEndPoint).Port.ToString());
0 as the constructor parameter set the app to automatically find free udp port. ((IPEndPoint)udpClient.Client.LocalEndPoint)).Port.ToString() is used to find the port number.

I believe you can use the Socket.RemoteEndPoint property to know what the IP/Port of the client connected to the server is (you know your local IP/port because you started the socket on that port, but it is also available through the LocalEndPoint property.
Also see the MSDN UdpClient for a simple example on how to use the UdpClient properly.

I think you cannot use UdpClient at server side to achieve your goal, as it does not have a Bind method to bind to an IPEndPoint.
You should use a Socket object to do that, which allows you to monitor a port for incoming UDP messages. Then no doubt you can tell the client which port the server is monitoring.

Related

C# TCP Listen and Connect on the same Local Port

I have a C# Console Application where I'm trying to implement TCP Hole Punching.
I need to listen on a Local Port and at the same time (simultaneously/asynchronously) connect to 2 different remote hosts (in reality a remote hosts public and private endpoints) using the same Local Port.
As I understand I somehow need to bind the Sockets/Ports but I can't figure this out in C#.
There's the TCPListener, TCPClient and Socket classes and I don't know which ones to use to accomplish what I need.
I'm following this guide http://www.bford.info/pub/net/p2pnat/index.html Chapter 4.2
From the same local TCP ports that A and B (Clients) used to register with (Server) S, A and B each asynchronously make outgoing connection attempts to the other's public and private endpoints as reported by S, while simultaneously listening for incoming connections on their respective local TCP ports.
I've already implemented the server part using NodeJS and it's working fine, I'm struggling with the Local Port stuff mentioned above.
I'm pretty sure that TCP only allows a 1-2-1 connection between client and server and ports. The only way to setup the multiple connections is to create two different sockets.
The TCP hole punching you're referring too I have tried before. You need to basically use the relay server to tell both A and B how to connect.. So do as follows:
1) Client A connects to Server on one port
2) Server tells Client B your IP and the port (this will be a new connection you will be setting up, different to your connection to the server)
3) Server tells Client A the IP and Port that Client B will be using
4) Client A uses the info provided to create a new connection directly to client B
5) Client B uses its info about your "new" connection to try and accept the incoming request
6) It might fail to handshake a couple of times due to latency, so build in some sort of repeater to keep trying the connection
7) You eventually should have a direct connection to B

Sending from a certain port in c#

I have written code that sends data using TCP on port 30175 on the client side server. The problem I am having is that even though I close the port between each string it is always sending from the same port on the server side causing the client to not recognize it as a new connection or new data. I would like to be able to specify the port i am sending from my server side. The program is written in c#.
The server listening on port 30175 will send data from port 30175. If you want a different port number then you must open a new port on the server and get the client to connect to it.
My suggestion is that you fix the problem that the client is not recognizing the new connection and new data.
First, lets address your issue. You choose which port the server is using when you Bind() to the socket. At this point, you have the option to specify a port. (If you don't then it chooses an available one for you.)
The question I have for you is... if you have your server switches ports all the time... Then how will the client know what port to Connect() to?
A Different Approach
Instead of closing the port between each string as you described before... consider setting up a protocol. For example: Have the client connect, then send the length (int) of the string in ASCII. Then send the ASCII byte[]. After this, repeat the process. (So the server would be ready for another int.)
This way your code isn't spending as much time binding/connecting to sockets.

How should I bridge TCP traffic using C#? Need to copy traffic on one port and send it to another

I need to bridge traffic from one port (say port 3000) and send it to a new port (say port 4000) and have full bidirectional TCP support.
How should I go about solving this? Should I use the socket class or the TCPClient class?
Would this be as simple as sending the stream of data from one TCPClient to another?
Is there anything else I should be considering?
You program needs to act as both a server and a client:
It should act at a server regarding port 3000, where your program receives connections. Each time you receive a new connection you yourself create a new connection to the actual server on port 4000. Keep these two connections (the one initiated on port 3000 and the one you created to port 4000) together, so you know they are a pair. When you receive data on any of these connection, just send it on the the other connection in the pair.

Different port Socket C# for sending and receive

i'm using UDP async socket in C#.net and i want to make the server and a client communicate in different port for sending and receive,
the server send to client with port A and receive from client with port B
the client receive from server with port A and send data to server with port B
is it possible??
In the client bind the socket to port A, and in the client bind to port B. It's as simple as that. The server shouldn't really know A, but get it from the messages it receives messages from the client (using something like ReceiveFrom.
Remember that using UDP, the client has to be the first to send messages, otherwise it's not really a client-server system but more distributed system.
This is exactly how it happens already. The source port for a client is a random port chosen by the OS.
It's not possible to do this, since an endpoint consists of only one IP address and one port number. You would need to use two different sockets and establish two connections with the server in order to use port A and port B.
If you were using TCP rather than UDP as part of the constructor of the TCPClient you can specify which EndPoint you want the outgoing connection to use.

About C# UDP Sockets

I am supposed to connect to external server using UDP sockets in C#..
I could not understand these 2 lines in server usage notes:
"Use of dedicated sockets is enforced."
and
"If the server looses UDP connectivity with the client, it will ..."
I thought that the UDP socket is connectionless!
So what did "looses connectivity" mean? and how to avoid it?
Does there is a known way to ensure "dedicated sockets"?
Thanks
"Use of dedicated sockets is
enforced."
To me this says, create one unique socket for each connection and use it throughout that connection.
EDIT: Just to expand on this, from the servers point of view.
UDP sockets are not identified by the
remote address, but only by the local
address, although each message has an
associated remote address. (source).
That way the server can distinguish from which client each message came from. Because the remote address is made up of an ip address and port combination, you should use the same socket throughout your communication of the sever. This is because if you don't, it's possible you could get assigned a different port next time you change the underlying socket.
"If the server looses UDP connectivity
with the client, it will ..."
It is possible to loose UPD connectivity e.g. either of the endpoints in the connection is lost, say I go to the server and pull the plug?
EDIT2:
Dan Bryant makes an excellent point in the comments, that links in with what I was saying about.
One thing worth noting is that it's
possible for a call to a UDP socket to
throw a SocketException with
SocketError.ConnectionReset as the
error code. UDP does not have any sort
of session with structured
connect/disconnect, but it does use a
dynamically-assigned remote port to
allow replies, which is a kind of
'connection'.
After 2 hours trying different -may be random solutions:
The server wants you to introduce yourself on a port other than the one you will use to actually send data. "dedicated sockets"
You know which IP and Port you are sending start info on, but you do not know which will be used for actual data transmission..
Solution
1- You will create your socket -with known IPEndpoint, and send on it the 'start message'..
2- Then wait to receive from Any IP...
3- The server will response 'welcome message', stating the Endpoint it will use.(by changing parameter ref remoteEP of Socket.ReceiveFrom())
4- You must then change the port you are sending on = remote Endpoint port + 1 (why? standard way or something?)
5- At last you can send and receive normally using these ports

Categories