我不知道如何解决这个问题,但我遇到了这一行的问题,特别是 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;
}
}