Possible duplicates: UDP Response or Receiving a response through UDP
I've been writing a udp server-client setup over the last month and have a working server. This server is meant to be a communications program to retrieve data about the computer it's hosted on that was requested by a remote client machine. The general idea is outlined below:
Client -> requests data -> Server -> processes request -> sends response -> Client
And the client receives the data and does what it needs with that data.
I'm able to do everything in that outline, except where the client receives the packets and outputs them to my test console. If you want to see the UdpServer class, it's simple, however just comment and I'll post it up here in an edit.
Here's how I'm receiving data (testing application):
executes: "test.exe <array index>", e.g. "test.exe 4" -> executes against 204.229.219.35
public class Program {
static void Main(String[] args) {
string pre = "204.229.219";
string[] servers = {
"31", "32", "33", "34", "35", "36",
"37", "38", "39", "40", "41", "42",
"88", "89"
};
if (args.Length < 1)
{
Environment.Exit(1);
}
int idx = Int32.Parse(args[0]);
Console.Title = "UDPSERVERTEST";
UdpClient c = new UdpClient();
Byte[] data = Encoding.ASCII.GetBytes("net user");
IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(String.Format("{0}.{1}", pre, servers[idx])).AddressList[0], 8915);
Console.WriteLine("\tQuerying: {0}", String.Format("{0}.{1}", pre, servers[idx]));
c.Send(data, data.Length, ep);
UdpState s = new UdpState();
s.endPoint = new IPEndPoint(IPAddress.Any, 0);
s.host = c;
s.encoding = Encoding.ASCII;
ReceiveCallback(s);
Console.ReadKey(true);
}
private static void ReceiveCallback(UdpState s)
{
byte[] data = s.host.Receive(ref s.endPoint);
Console.WriteLine("Data received from ({0}) -> {1}", s.endPoint.ToString(), s.encoding.GetString(data));
}
private static void callback(IAsyncResult ar)
{
UdpState s = (UdpState)ar.AsyncState;
UdpClient u = s.host;
IPEndPoint e = s.endPoint;
byte[] data = u.EndReceive(ar, ref e);
}
}
On either of my receiving methods, I get an error like the following:
An existing connection was forcibly closed by the remote host
Thanks Stackoverflow!