I have a c# udp socket I'm trying to send data from to my home. The server socket is hosted at aws, and the client is at my house. Using the UDPSocket.cs example on GitHub, I modified the code to send a response back to the client after the server receives the first message sent by the client.
Here is the client code:
public void Client(string address, int port)
{
_socket.Connect(IPAddress.Parse(address), port);
Receive();
}
public void Send(string text)
{
byte[] data = Encoding.ASCII.GetBytes(text);
_socket.BeginSend(data, 0, data.Length, SocketFlags.None, (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = _socket.EndSend(ar);
Console.WriteLine("SEND: {0}, {1}", bytes, text);
}, state);
}
public void Receive()
{
_socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = _socket.EndReceiveFrom(ar, ref epFrom);
_socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
}, state);
}
int port = 27000;
UDPSocket c = new UDPSocket();
int new_port = 40156;
IPEndPoint bep = new IPEndPoint(IPAddress.Parse([MY_HOME_INTERNAL_IP]), new_port );
if( bep != null ){
Console.WriteLine( "binding_to_ep" );
c._socket.Bind( bep );
c.Receive();
}
c.Client([REMOTE_SERVER_EXTERNAL_IP], port );
c.Send("TEST!");
Console.ReadKey();
here is the server code:
public void Server(string address, int port)
{
_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
_socket.Bind(new IPEndPoint(IPAddress.Parse(address), port));
Receive();
}
private void Receive()
{
_socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = _socket.EndReceiveFrom(ar, ref epFrom);
_socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
//this is where I attempt to send back to the client, but nothing get's received by the client it seems.
byte[] data = Encoding.ASCII.GetBytes("12");
//EndPoint epFrom1 = new IPEndPoint(IPAddress.Parse( [MY_HOME_EXTERNAL_IP] ), 40156);
EndPoint epFrom1 = new IPEndPoint(IPAddress.Parse([MY_HOME_EXTERNAL_IP]), 27000 );
_socket.SendTo(data, epFrom1 );
}, state);
}
UDPSocket s = new UDPSocket();
s.Server( [THE_SERVER_LOCAL_IP] , port );
so far I've gotten the message through to the server, but in the server receive code where I try and call SendTo to send back to the client, nothing appears in the client receive method. previously I was receiving a "connection refused" error message when trying to send to the client, but now I don't receive any message, which makes me think that perhaps the data got through, but I don't now. anyone have any suggestions. it'd be greatly appreciated. I figured this would be pretty straight forward and I wouldn't have to mess around with firewall rules and stuff because the connection is already/clearly established, therefore my server should easily be able to send data back to the client.
also, in case anyone is wondering, the aws server allows all output traffic and prohibits inbound traffic to port 27000, which is what is necessary as specified by the code.
also, a typical message I get from the client when that first message is received by the server is: RECV: [MY_HOME_EXTERNAL_IP_ADDRESS]:[NUMBER_OF_BYTES]: 5, TEST!
I tried running the above code and only sending the message to the server worked. the server didn't seem to be able to successfully send a message back to the client, and there is no error message to confirm what happened to the message sent back.
I was expecting a message back at the client in its Received method.
UPDATE: I tried running this code locally on two home machines. it worked fully as expected where the client sends a message to the server and the server sends a message back. the only thing I changed was where I set epFrom1 to send a message back from the server to the client: EndPoint epFrom1 = new IPEndPoint(IPAddress.Parse("10.0.1.125"), port ); and of course I changed all necessary ips so they could be used for this local experiment. other than that, everything remained the same; and it worked just as expected.