Клиент-серверное приложение с использование классов UdpClient и multicast - C#
Формулировка задачи:
Здравствуйте! Возможно ли создать подобие чата между клиентом и сервером, используя класс UdpClient и multicast? То есть сервер должен послать сообщение и ждать ответа от клиента, потом снова послать сообщение и получить ответ от клиента и т. д. Пока получилось отправить только одно сообщение. Сервер:
Клиент:
Заранее спасибо!
// Create object UdpClient
UdpClient udpClient = new UdpClient();
// IPAddress of group
IPAddress multicastaddress = IPAddress.Parse("224.192.168.255");
try
{
// Join group
udpClient.JoinMulticastGroup(multicastaddress);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.ToString());
}
// Create object IPEndPoint
IPEndPoint remoteep = new IPEndPoint(multicastaddress, 2345);
// Print message
Console.WriteLine("Server is running ...\n\nEnter message to send: ");
// Data in Byte []
Byte[] buffer = Encoding.Unicode.GetBytes(Console.ReadLine());
try
{
// Send data using IPEndPoint
udpClient.Send(buffer, buffer.Length, remoteep);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.ToString());
}
// Leave the group
udpClient.DropMulticastGroup(multicastaddress);
// Close connection
udpClient.Close();// Create UDP client
UdpClient client = new UdpClient();
// Create new IPEndPoint
IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 2345);
// Set socket options
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
// Bind to IPEndPoint
client.Client.Bind(localEp);
// IP address
IPAddress multicastaddress = IPAddress.Parse("224.192.168.255");
try
{
// Join group
client.JoinMulticastGroup(multicastaddress);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.ToString());
}
Console.WriteLine("Client is running ...\n\n");
// Receive data
Byte[] data = client.Receive(ref localEp);
string strData = Encoding.Unicode.GetString(data);
Console.WriteLine("Server: " + strData + "\n");
// Leave the group
client.DropMulticastGroup(multicastaddress);
// Close connection
client.Close();Решение задачи: «Клиент-серверное приложение с использование классов UdpClient и multicast»
textual
Листинг программы
using System;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Client
{
class Program
{
static void Main(string[] args)
{
// Create UDP client
UdpClient client = new UdpClient();
// Create new IPEndPoint
IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 2345);
// Set socket options
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
// Bind to IPEndPoint
client.Client.Bind(localEp);
// IP address
IPAddress multicastaddress = IPAddress.Parse("224.192.168.255");
try
{
// Join group
client.JoinMulticastGroup(multicastaddress);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.ToString());
}
Console.WriteLine("Client is running ...\n\n");
// Receive data
while (true)
{
Byte[] data = client.Receive(ref localEp);
string strData = Encoding.Unicode.GetString(data);
Console.WriteLine("Server: " + strData + "\n");
if (strData == "Bye!")
break;
}
// Leave the group
client.DropMulticastGroup(multicastaddress);
// Close connection
client.Close();
}
}
}