开发者

Sending UDP packet to server and getting response

开发者 https://www.devze.com 2022-12-13 16:50 出处:网络
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 communic

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 ou开发者_开发百科tline, 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!


Can you verify that your text program is getting the "Receive Data" event to fire? If you aren't getting anything to output to your test bench, there are two possible problems:

  1. The reply is being blocked by a firewall, but you make it sound like this works on other computers, so I will assume this isn't the problem.
  2. Your data receive event isn't firing. This could be because you don't have your data port object configured correctly. Without more information about how you are doing this, that would be my best bet.

What I would do is confirm that you are getting your event to fire first. If your data port object isn't configured correctly, that can easily be your entire problem. If that is configured correctly, confirm that your server is actually sending a response. It may be that it is not sending and response and, therefore, your client isn't receiving one.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号