Тема: UDP сервер
Є сервер
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Server
{
class Program
{
public static string[] ip = null;
public static int i = 0;
static void Main(string[] args)
{
StartListener();
}
private const int listenPort = 11000;
private static void StartListener()
{
bool done = false;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
try
{
while (!done)
{
byte[] bytes = listener.Receive(ref groupEP);
if (Encoding.ASCII.GetString(bytes, 0, bytes.Length) == "Connect")
{
ip[i] = groupEP.ToString();
i++;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}
}
}
Клієнт
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
IPAddress broadcast = IPAddress.Parse("127.0.0.1");
byte[] sendbuf = Encoding.ASCII.GetBytes("Connect");
IPEndPoint ep = new IPEndPoint(broadcast, 11000);
s.SendTo(sendbuf, ep);
Console.WriteLine("Message sent to the broadcast address");
}
}
}
Чому якщо запускаю клиент сервер закриваєтся?
Ошибок нет.
Якщо видалити
ip[i] = groupEP.ToString(); i++;
то не закриваєтся