private void btn_servidor_escuchar_Click(object sender, EventArgs e) { if (m_servidor.IsConnected()) { m_servidor.Desconectar(); } else { int port; if (int.TryParse(txt_servidor_puerto.Text, out port)) { m_servidor = new ServidorTCP(); m_servidor.DataReceived += OnDataReceivedServidor; m_servidor.Connected += OnStateChangeServidor; if (m_servidor.Escuchar(port)) { string strRecibido = string.Format("{0} Servidor-> {1}\r\n", DateTime.Now.ToString("HH:mm:ss"), "Escuchando..."); txt_servidor_recibido.BeginInvoke(new MethodInvoker(delegate { txt_servidor_recibido.Text += strRecibido; })); } } } }
private void bConectar_Click(object sender, EventArgs e) { if (host.Text == "localhost" || host.Text == "127.0.0.1") { try { //Comprobar puerto escucha ServidorTCP servidorTCP = new ServidorTCP(4444); servidorTCP.Desactivar(); //Iniciar servidor System.Diagnostics.ProcessStartInfo servidor = new System.Diagnostics.ProcessStartInfo(System.IO.Path.Combine(Application.StartupPath, "Servidor.exe")); #if DEBUG servidor.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; #else servidor.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; #endif procesoServidor = System.Diagnostics.Process.Start(servidor); ServidorActivo = true; System.Windows.Forms.Application.DoEvents(); } catch { ServidorActivo = true; } } try { Cliente cliente = new Cliente(nombreJugador.Text.Replace("'", "\""), host.Text, Programa.IdCliente); cliente.ClienteTcp.ControlInvoke = vPrincipal; cliente.Color = color.BackColor; clientes.Add(cliente); cliente.ClienteTcp.DatosRecibidos += ComandoRecibido; cliente.Conectar(Programa.IdCliente, InformacionPrograma.VersionActual); } catch (System.Net.Sockets.SocketException error) { MessageBox.Show("Error al conectar al servidor: " + error.Message + "\r\nCodigo de error: " + error.ErrorCode, "Error al conectar", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public Servidor() { Clientes = new Dictionary <ClienteTCP, Cliente>(); try { servidorTCP = new ServidorTCP(4444); } catch { Console.WriteLine( "El puerto usado por el servidor ya está en uso. Cierre todas las instancias para poder continuar"); Console.ReadLine(); Environment.Exit(1); } servidorTCP.DatosRecibidos += new DelegadoComandoRecibido(ComandoRecibido); Console.WriteLine("------>Esperando conexiones..."); //Console.WriteLine("------>{0}", Resources.WaitingForConnections); Mapa = new Mapa(); Mapa.ModoGrafico = false; Mapa.Semilla = new Aleatorios().Next(); Mapa.Neutrales = 10; }
private void Form1_Load(object sender, EventArgs e) { m_cliente = new ClienteTCP(); m_servidor = new ServidorTCP(); }