Я не уверен, как это исправить, но у меня проблема с этой линией, в частности throw new System.ArgumentException («Синтаксис: timeclnt ServerName PortNumber 1»); ..... любая подсказка, что делать? Серверному приложению необходимо будет предоставить номер порта, на котором будет прослушиваться, но как?UDP-серверная прикладная программа
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;
}
}
1 .) Почему бы не args.Length! = 2? 2.) Исключение составляет 3 аргумента: ServerName PortName 1 <- Это означает, что он всегда! = 2 3.) Если пользователь переключает аргументы Servername и Port, вы получите другое исключение, потому что имя string servername нельзя отнести к целому. –
@ Алина Б. Я не уверен, как это исправить? Я попытался вставить это, но он сказал, что идентификатор ожидается. Я не очень хорош с C# – Cliff
Cliff, почему вы отредактировали свой вопрос таким образом? –