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:
Código
static void Main(string[] args) { p.Start(); } public void Start() { Console.Title = "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()); conexion.startServer(); int finalTime = int.Parse(DateTime.Now.Second.ToString()) - startTime; s.Write(string.Format("Server started succesfully in: {0} seconds!",finalTime)); }
TcpConnection:
Código
class tcpConexion { private TcpClient client; private TcpListener listener; public void startServer() { } private void Listener() { this.listener.Start(); while (true) { this.client = this.listener.AcceptTcpClient(); handleClient.newClient(this.client); } } public void startReading(TcpClient client, int clientID) { this.s.Write(string.Format("{0} Client has connected!", clientID)); s.updateConsoleUsers(this.clientList.Count); NetworkStream stream = client.GetStream(); 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 { 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:
Código
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? ).
Código
class handleClient { private static int getID; public static void newClient(TcpClient client) { getID = rand.Next(10000000, 99999999); } }
Cuando tenga más tiempo intentaré comentarlo todo ^^.