0

I am trying to figure out why the client component of the array below is out of range. I know this happens when the element of an array that you are trying to access doesn't exist but I am new to Socket Programming and trying to write my first UDP script but not sure how to deal with it.

Client side code causing error at the args[0]:

class EmployeeUDPClient{
    public static void Main(string[] args){
        UdpClient udpc = new UdpClient(args[0],2055);   //Line causing error
        IPEndPoint ep = null;
        while(true){
            Console.Write("Name: ");
            string name = Console.ReadLine();
            if(name == "") break;
            byte[] sdata = Encoding.ASCII.GetBytes(name);
            udpc.Send(sdata,sdata.Length);
            byte[] rdata = udpc.Receive(ref ep);
            string job = Encoding.ASCII.GetString(rdata);
            Console.WriteLine(job);
        }
    }
}

This is the server side code which runs fine:

class EmployeeUDPServer{
    public static void Main(){
        UdpClient udpc = new UdpClient(2055);
        Console.WriteLine("Server started, servicing on port 2055");
        IPEndPoint ep = null;
        while(true){
            byte[] rdata = udpc.Receive(ref ep);
            string name = Encoding.ASCII.GetString(rdata);
            string job = ConfigurationSettings.AppSettings[name];
            if(job == null) job = "No such employee";
            byte[] sdata = Encoding.ASCII.GetBytes(job);
            udpc.Send(sdata,sdata.Length,ep);
        }
    }
}

Any thoughts on why I get this error? I am running the 2 scripts on the same computer so could that be the reason?

4

1 回答 1

0

该错误是因为您没有将任何参数传递给客户端类中的 Main() 函数。

换线:

UdpClient udpc = new UdpClient(2055);

至:

string[] host = new string[1];
host[0] = "127.0.0.1";
UdpClient udpc = new UdpClient(host);
于 2013-07-22T17:55:18.490 回答