|
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Program
{
static void Main(string[] args)
{
ArrayList Clients = new ArrayList();
ArrayList drawing = new ArrayList();
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
UdpClient server = new UdpClient(localEP);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
string theAnswer = "";
byte[] data;
string msg;
while (true)
{
data = server.Receive(ref remoteEP);
Console.Write("Received from {0}: ", remoteEP.ToString());
msg = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine(msg);
if (msg.StartsWith("join"))
{
if (msg.EndsWith("drawer"))
{
if (Clients.Count == 0)
addDrawer(Clients, localEP, server, remoteEP);
else
{
msg = "denied";
data = Encoding.ASCII.GetBytes(msg);
server.Send(data, data.Length, remoteEP);
}
}
if (msg.EndsWith("guesser"))
{
if (Clients.Count == 0)
{
msg = "denied";
data = Encoding.ASCII.GetBytes(msg);
server.Send(data, data.Length, remoteEP);
}
else
{
addGuesser(Clients, localEP, server, remoteEP);
}
}
}
if (msg.StartsWith("quit"))
{
if (msg.EndsWith("drawer"))
foreach (IPEndPoint endpoint in Clients)
{
data = Encoding.ASCII.GetBytes(msg);
server.Send(data, data.Length, remoteEP);
}
Clients.Remove(remoteEP);
}
if (msg.StartsWith("draw"))
{
foreach (IPEndPoint endpoint in Clients)
{
data = Encoding.ASCII.GetBytes(msg);
server.Send(data, data.Length, endpoint);
}
}
if (msg.StartsWith("gues"))
{
if (msg.EndsWith(theAnswer))
msg += "true";
else
msg += "false";
foreach (IPEndPoint endpoint in Clients)
{
data = Encoding.ASCII.GetBytes(msg);
server.Send(data, data.Length, endpoint);
}
}
if (msg.StartsWith("submit"))
{
theAnswer = msg.Substring(7).ToLower();
}
}
}
public static void addDrawer(ArrayList Clients, IPEndPoint localEP, UdpClient server, IPEndPoint remoteEP)
{
Clients.Add(remoteEP);
string msg = "accept";
byte[] data = Encoding.ASCII.GetBytes(msg);
server.Send(data, data.Length, remoteEP);
}
public static void addGuesser(ArrayList Clients, IPEndPoint localEP, UdpClient server, IPEndPoint remoteEP)
{
Clients.Add(remoteEP);
string msg = "accept";
byte[] data = Encoding.ASCII.GetBytes(msg);
server.Send(data, data.Length, remoteEP);
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.