TCP Echo on different adapter - c#

I'm trying to write a program for a computer with 2 network adapters (Private and Public) that echoes data received on Private to Public and vice versa. I only need it to listen on two specific ports, and more specifically, it should open a socket server on Private and a socket client on Public.
The computer's IP address on Public is 192.168.1.21 and its IP address on Private is 172.16.13.1, so I thought something like this would work:
IPEndPoint privateEndpoint1 = new IPEndPoint(IPAddress.Parse("172.16.13.1"), port1);
IPEndPoint privateEndpoint2 = new IPEndPoint(IPAddress.Parse("172.16.13.1"), port2);
IPEndPoint publicEndpoint1 = new IPEndPoint(IPAddress.Parse("192.168.1.21"), port1);
IPEndPoint publicEndpoint2 = new IPEndPoint(IPAddress.Parse("192.168.1.21"), port2);
TcpListener priList1 = new TcpListener(privateEndpoint1);
TcpListener priList2 = new TcpListener(privateEndpoint2);
TcpClient pubCli1 = new TcpClient(publicEndpoint1); //Error here
TcpClient pubCli2 = new TcpClient(publicEndpoint2);
I then get the streams and do the actual echoing.
The problem is that I get an error on the line that I marked:
Only one usage of each socket address (protocol/network address/port) is normally permitted
It seems from the error that the IPEndPoint constructor is not differentiating between the two IP addresses and the two network adapters. I've looked into using normal Sockets instead of TcpClients, but that didn't seem to change anything.

Related

C# UDP socket app to listen to messages from various sockets

I have a system monitor app that needs to listen to messages from various UDP sockets on another machine. The other sockets continuously send heartbeats to this given IP/port.
This exception gets thrown when calling BeginReceiveFrom:
"A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"
I shouldn't have to call connect because data is already getting sent to this ip endpoint. plus the data is coming from various sockets.
private Socket m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// bind socket
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
m_localEndPoint = new IPEndPoint(ipAddress, 19018);
m_socket.Bind(m_localEndPoint);
m_socket.BeginReceiveFrom(m_data,
m_nBytes,
MAX_READ_SIZE,
SocketFlags.None,
ref m_localEndPoint,
new AsyncCallback(OnReceive),
null);
}
private void OnReceive(IAsyncResult ar)
{
int nRead = m_socket.EndReceiveFrom(ar, ref m_localEndPoint);
}
I was able to get the desired behavior with the UdpClient class.
Your problem was you were binding to the IP assigned to you, you should bind to IP you want receive from - in your case:
m_socket.Bind(new IPEndPoint(IPAddress.Any, 12345));

How to set IP address of client manually in .net socket program

I created a multithreaded client-server chat application and want to test my application with multiple clients. I'm planning to create a simulator in client side which create a random port and IP. By that I mean my client system should run with multiple ports (without running multiple times).
I tried to find out the part of the code which gives client IP and port number in the client class, but couldn't figure it out. I only found the part which gives server IP and port.
This is my connection establishing part
private void cmdConnect_Click(object sender, System.EventArgs e)
{
try
{
//create a new client socket ...
m_socWorker = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
String szIPSelected = txtIPAddress.Text;
String szPort = txtPort.Text;
int alPort = System.Convert.ToInt16 (szPort,10);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socWorker.Connect(remoteEndPoint);
}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show ( se.Message );
}
}
and my data sending part
private void cmdSendData_Click(object sender, System.EventArgs e)
{
try
{
Object objData = txtData.Text;
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
m_socWorker.Send (byData);
}
catch(System.Net.Sockets.SocketException se)
{
MessageBox.Show (se.Message );
}
}
If you really need to set the client socket address before making a connection to the server, and it is often a valid scenario when one wants to force the kernel to bind to a specific local address on multihomed systems, you may call Socket.Bind() before you call Socket.Connect():
IPAddress localIPAddress = IPAddress.Parse(szLocalIP);
IPEndPoint localEndPoint = new IPEndPoint(localIPAddress, 0);
m_socWorker.Bind(localEndPoint);
m_socWorker.Connect(remoteEndPoint);
You may also explicitly specify the local port number in the IPEndPoint constructor, but you have to make sure, that the port is not in use. If you leave the port number equal to 0, then the system would pick an (almost) arbitrary free port number.
Note that you cannot just pick an arbitrary client IP address - it must be one of the addresses, configured on your system's enabled network interfaces. See the output of ipconfig /all for what is configured. You can assign more than one IP address to an interface if you simply want to test.

Does UDP socket need to go through the Accept Process like TCP sockets?

I'm working with UDP and i was wondering about the Accept Method when multiple machine need to connect to a server. So far i was working with UDPCliente class, IPEndPoint class and BeginRecieve / EndRecieve Method to create a server where multiple machine can connect at the same time.
My question is simple do i need to use Accept Method to handler incoming connection and create a new socket for each new connection ?
What is the best way to handler multiple connection with UDP ?
The code samples i've seen so far create a new UDPClient class and a IPEndPoint where the server is listening for connections after that, the code call the BeginRecieve passing a function where the data is recieved and then starts the process of BeginRecieve again.
These are the code samples i've been using so far:
public static void receiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
byte[] receiveBytes = u.EndReceive(ar, ref e);
UdpState s = new UdpState();
s.e = e;
s.u = u;
u.BeginReceive(new AsyncCallback(receiveCallback), s);
}
public static void receiveMessages()
{
IPEndPoint e = new IPEndPoint(IPAddress.Any, 5050);
UdpClient u = new UdpClient(e);
UdpState s = new UdpState();
s.e = e;
s.u = u;
u.BeginReceive(new AsyncCallback(receiveCallback), s);
}
UDP is connectionless, so there's nothing to accept. If you need connections over UDP, then you have to implement them. Ideally, you would assign each "connection" some kind of identifier and include it in each datagram sent to the server. In some cases, it may be sufficient just to rely on the IP address and port to identify "connections".
But you can do it however you want. UDP treats each datagram as independent.
Short answer - no, you don't use accept() with UDP.
In UDP there are no connections, only datagrams. One side sends them and the other might receive them. Each datagram has information about the sender (IP address and port) that your server app can extract to differentiate the clients.

Sending data over UDP to a destination with multiple clients open

I am currently using this function to send data from the server to the clients
private static void send_message(string ip, string message)
{
byte[] packetData = System.Text.UTF8Encoding.UTF8.GetBytes(message);
int port = 11000;
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ip), port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.SendTo(packetData, ep);
}
But this would mean a destination IP/port can only have one client open to receive the data, because having two clients open would mean one client can retrieve data that was meant for another (if I'm correct).. how do I solve this?
Receiving function:
private static Int32 port = 11000;
private static UdpClient udpClient = new UdpClient(port);
public static void receive_threaded()
{
Thread t = new Thread(() =>
{
while (true)
{
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);
byte[] content = udpClient.Receive(ref remoteIPEndPoint);
if (content.Length > 0)
{
string message = Encoding.UTF8.GetString(content);
parseMessage(message);
}
}
});
t.Start();
}
1) You should implement some kind of protocol so that your server has a "well known" port to accept connections. Use this port to inform your client ANOTHER port where the client must connect. Use a different port for each client.
Your client conects to the server at 11000. Your server assigns a unique port for the client, let's say 11001 for the firts client. Then the server opens a connection at 11001. The client closes connection at 11000 and opens a new connection at 11001 to receive the data.
2) Why UDP?
I don't see why you need to open a new socket at all. You already have each client's address and port, from the first packet they sent you. Just send a packet to that address:port. I absolutely don't get the other suggestion of setting up extra ports either.

How to specify source port of a UdpPacket?

I wanted to send UdpPacket to a specific remote host (I already know the public IP and Port).
I wanted to use C#'s UdpClient class.
static int Main()
{
UdpClient client = new UdpClient();
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("1.2.3.4"), 9999);
byte[] data = GetData();
client.Send(data, data.Length, remoteEP);
}
When sending a packet, the UdpClient choose an available port automatically. I want to manually set the port, from which I send the packets.
Thanks for your help in advance!
Try specifying the endpoint when you create the UdpClient:
UdpClient client = new UdpClient(localEndpoint);
EDIT: Note that you can also specify just the port number:
UdpClient client = new UdpClient(localPort);
That may be somewhat simpler :)

Categories