Encontré este "proyecto" que tenía empezado, es un server hecho en c#.
Creo que puede servirle de ayuda a cualquiera que esté interesado en empezar. ( el código es de hace tiempo, puede tener errores y ser sucio ).
Main:
static void Main(string[] args)
{
Program p = new Program();
p.Start();
}
public void Start()
{
Console.Title = "Server";
Server s = new Server();
s.Write(@" _________ ");
s.Write(@"/ _____/ ______________ __ ___________ ");
s.Write(@"\_____ \_/ __ \_ __ \ \/ // __ \_ __ \ ");
s.Write(@"/ \ ___/| | \/\ /\ ___/| | \/ ");
s.Write(@"/_______ /\___ >__| \_/ \___ >__| ");
s.Write(@" \/ \/ \/ ");
s.Write(@" By Nevachana ");
s.Write("Starting server [...]");
int startTime = int.Parse(DateTime.Now.Second.ToString());
tcpConexion conexion = new tcpConexion();
conexion.startServer();
int finalTime = int.Parse(DateTime.Now.Second.ToString()) - startTime;
s.Write(string.Format("Server started succesfully in: {0} seconds!",finalTime));
}
TcpConnection:
class tcpConexion
{
private TcpClient client;
private TcpListener listener;
private Server s = new Server();
private List<Tuple<TcpClient, int>> clientList = new List<Tuple<TcpClient, int>>();
public void startServer()
{
new Thread(() => this.Listener()).Start();
}
private void Listener()
{
this.listener = new TcpListener(IPAddress.Any, 3030);
this.listener.Start();
while (true)
{
this.client = this.listener.AcceptTcpClient();
handleClient.newClient(this.client);
}
}
public void startReading(TcpClient client, int clientID)
{
this.clientList.Add(new Tuple<TcpClient, int>(client, clientID));
this.s.Write(string.Format("{0} Client has connected!", clientID));
s.updateConsoleUsers(this.clientList.Count);
NetworkStream stream = client.GetStream();
Byte[] buffer = new Byte[client.Available];
while (client.Connected)
{
try
{// ‖
stream.Read(buffer, 0, buffer.Length);
this.s.Write(string.Format("client: {0} has sent: {1}", clientID, Encoding.Default.GetString(buffer).IndexOf("@")));
handleData(Encoding.Default.GetString(buffer).IndexOf("@").ToString());
stream.Flush();
}
catch
{
this.clientList.Remove(new Tuple<TcpClient, int>(client, clientID));
s.updateConsoleUsers(this.clientList.Count);
stream.Flush();
stream.Close();
client.Close();
this.s.Write(string.Format("{0} has disconnected", clientID));
}
}
}
public void handleData(string packet)
{
int packetID = int.Parse(Regex.Split(packet, "‖")[0]);
switch (packetID)
{
case 0:
Console.WriteLine("packet recibido ^^");
break;
}
}
}
Server:
class Server
{
public void Write(string txt)
{
Console.WriteLine(DateTime.Now.ToString() + " - " + txt);
}
public string updateConsoleUsers(int number)
{
return Console.Title = "Server - total users Online: " + number.ToString() + " || Last update: " + DateTime.Now.ToString();
}
}
HandleClient: ( debería llamarse newClient .. no? :rolleyes: ).
class handleClient
{
private static int getID;
private static Random rand = new Random();
private static tcpConexion conexion = new tcpConexion();
public static void newClient(TcpClient client)
{
getID = rand.Next(10000000, 99999999);
new Thread(() => conexion.startReading(client, getID)).Start();
}
}
Cuando tenga más tiempo intentaré comentarlo todo ^^.