public virtual void OnAccept( IAsyncResult ar ) { try { Socket newSocket = listenSocket.EndAccept( ar ); if ( newSocket != null ) { ClientConnection newClient = new ClientConnection( newSocket, new RemoveClientDelegate( this.RemoveClient ) ); clients.Add( newClient ); newClient.Start(); } } catch {} try { listenSocket.BeginAccept( new AsyncCallback( this.OnAccept ), listenSocket ); } catch { Dispose(); } }
public ClientConnection(TcpClient nclient, int nid, ClientConnection nOtherPlayer) : this(nclient, nid) { this.otherPlayer = nOtherPlayer; }
private void receiveConnection() { try { listen = new TcpListener(ipAddr, port); listen.Start(); while (_connect) { if (listen != null) { TcpClient ncli = listen.AcceptTcpClient(); ArrayList name = new ArrayList(); foreach (ClientConnection cli in clients) name.Add(cli.Username); ClientConnection c = new ClientConnection(this, ncli, passw, name); clients.Add(c); TextMessage ms = new TextMessage(); ms.messageType = MessageType.USER_JOIN; ms.username = c.Username; ms.message = "nuovo utente connesso"; DispatchMsg(ms); } } } catch (ClientConnectionFail ae) { TextMessage ms = new TextMessage(); ms.messageType = MessageType.ADMIN; ms.message = ae.Message; DispatchMsg(ms); } catch (IOException) { if (listen != null) { listen.Stop(); listen = null; } return; } catch (Exception) { if (listen != null) { listen.Stop(); listen = null; } return; } }
/// <summary> /// Stops all clients abruptly and will not /// return until successfull. /// </summary> public void Stop() { ClientConnection.StopAllClients(); }