public void Stop() { if (!IsRunning) { return; } lock (tcp) { Debug.Log("Stopping client..."); stream.Close(); tcp.Close(); // Drop all memory tcp = null; stream = null; packetQueue = null; packetProtocol = null; } }
private void RunMain() { try { Debug.Log("Connecting to server..."); tcp = new TcpClient(host, port); stream = tcp.GetStream(); Debug.Log("Connected."); packetProtocol = new PacketProtocol(stream, packetQueue); while (true) { if (!packetProtocol.GetPacket()) { throw new SocketException(10053); } } } catch (SocketException e) { switch (e.ErrorCode) { case 10048: // Port already in use (Only applies to servers?) Debug.LogError("Port already in use."); Stop(); break; case 10049: // Port not available to system. (Only applies to servers?) Debug.LogError("Port not available."); Stop(); break; case 10051: // Unknown host Debug.LogError("Unknown host."); Stop(); break; case 10004: case 10053: // Connection closed by this machine Debug.LogError("Connection closed by system."); Stop(); break; case 10050: // No internet Debug.LogError("No internet connection."); Stop(); break; case 10061: // Connection refused Debug.LogError("Connection refused by server."); Stop(); break; default: // Unexpected Debug.LogError("Disconnection Error: " + e.ErrorCode); Debug.LogException(e); Stop(); break; } } }