class Program
{
static void Main(string[] args)
{
UDPSocket s = new UDPSocket();
s.Server(Console.ReadLine(), Convert.ToInt32(Console.ReadLine()));
Console.WriteLine("Сервер запущен, ожидайте...");
Console.ReadKey();
}
}
class UDPSocket
{
private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private const int bufSize = 8 * 1024;
private State state = new State();
private EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0);
private AsyncCallback recv = null;
private List<Clients> clients = new List<Clients>();
public void AddUser(Clients client)
{
clients.Add(client);
}
public class State
{
public byte[] buffer = new byte[bufSize];
}
public void Server(string address, int port)
{
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
socket.Bind(new IPEndPoint(IPAddress.Parse(address), port));
Recive();
}
private void Recive()
{
socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
{
bool addClient = true;
State so = (State)ar.AsyncState;
int bytes = socket.EndReceiveFrom(ar, ref epFrom);
socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
for (int i = 0; i < clients.Count; i++)
if (clients[i].epFrom == epFrom)
addClient = true;
if (addClient) { Clients client = new Clients(epFrom, this); }
BroadcastMessage(epFrom, Encoding.ASCII.GetString(so.buffer, 0, bytes));
}, state);
}
public void BroadcastMessage(EndPoint end, string msg)
{
byte[] data = Encoding.ASCII.GetBytes(msg);
for(int i = 0; i < clients.Count; i++)
{
socket.BeginSendTo(data, 0, data.Length, SocketFlags.None, clients[i].epFrom, (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = socket.EndSend(ar);
}, state);
}
}
}
class Clients
{
public EndPoint epFrom = null;
UDPSocket udpSocket = null;
public Clients(EndPoint end, UDPSocket udp)
{
epFrom = end;
udpSocket = udp;
udpSocket.AddUser(this);
}
}
class Program
{
static void Main(string[] args)
{
UDPSocket c = new UDPSocket();
Console.WriteLine("Введите:IP для отправки сообщений\nPort для отправки сообщений");
c.Client(Console.ReadLine(), Convert.ToInt32(Console.ReadLine()));
c.Send("TEST 1");
Console.ReadKey();
c.Send("Test $2");
Console.ReadKey();
}
}
class UDPSocket
{
private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private const int bufSize = 8 * 1024;
private State state = new State();
private EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0);
private AsyncCallback recv = null;
public class State
{
public byte[] buffer = new byte[bufSize];
}
public void Client(string address, int port)
{
socket.Connect(IPAddress.Parse(address), port);
Recive();
}
public void Send(string text)
{
byte[] data = Encoding.ASCII.GetBytes(text);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = socket.EndSend(ar);
Console.WriteLine("SEND: {0}, {1}", bytes, text);
}, state);
}
private void Recive()
{
socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = socket.EndReceiveFrom(ar, ref epFrom);
socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
}, state);
}
}