1

我不知道如何解决这个问题,但我遇到了这一行的问题,特别是 throw new System.ArgumentException("Syntax: timeclnt ServerName PortNumber 1"); .....知道该怎么做吗?服务器应用程序需要提供一个端口号来监听,但如何?

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class TimeClient
{
    public static int Main(String[] args)
    {
        if (args.Length < 2 || args.Length > 2)
            throw new System.ArgumentException("Syntax: timeclnt ServerName PortNumber 1");

        String hostName = args[0];
        int portNum = Int32.Parse(args[1]);

        try
        {
            // Define a string to send to the server
            string stringData = "From the timeClient";

            // Encode it properly
            byte[] data = Encoding.ASCII.GetBytes(stringData);

            // Define a UDP client connection
            UdpClient client = new UdpClient();

            // Send some data to the server
            client.Send(data, data.Length, hostName, portNum);

            //where to listen for a UDP response
            IPEndPoint recvpt = new IPEndPoint(IPAddress.Any, 0);

            // get the data back from the server
            byte[] receivedData = client.Receive(ref recvpt);

            // output the data received
            Console.WriteLine("{0}", Encoding.ASCII.GetString(receivedData));

            // all done
            client.Close();
        }

        catch (Exception e)
        {
            // display an error
            Console.WriteLine(e.ToString());
        }

        return 0;
    }
}
4

2 回答 2

0

正如我在评论中提到的:

System.ArgumentException("语法:timeclnt ServerName PortNumber 1");

这意味着将向您的代码传递 3 个参数:ServerName、PortNumber 和 1

将 3 个参数传递给您的方法,您必须将 if 语句更改为:

if (args.Length != 3)
于 2013-03-03T15:17:37.603 回答
0

从您的if情况推断,您并没有完全传递 2 个参数。如果您想要 3 个参数,请将您的条件更改为

 if (args.Length < 3 || args.Length > 3)
于 2013-03-03T05:56:07.123 回答