/// <summary> /// Runs in background clientReceiveThread; Listens for incomming data. /// </summary> private void ListenForData() { try { socketConnection = new TcpClient("localhost", 8052); Byte[] bytes = new Byte[1024]; while (true) { // Get a stream object for reading using (NetworkStream stream = socketConnection.GetStream()) { int length; // Read incomming stream into byte arrary. while ((length = stream.Read(bytes, 0, bytes.Length)) != 0) { handler.dataUser = ByteHandler.ByteArrayToObject <DataUser>(bytes); } } } } catch (SocketException socketException) { Debug.Log("Socket exception: " + socketException); } }
private void ListenForIncommingRequests() { try { // Create listener on localhost port 8052. tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8052); tcpListener.Start(); Debug.Log("Server is listening"); byte[] bytes = new byte[1024]; while (true) { using (connectedTcpClient = tcpListener.AcceptTcpClient()) { // Get a stream object for reading using (NetworkStream stream = connectedTcpClient.GetStream()) { int length; // Read incomming stream into byte arrary. while ((length = stream.Read(bytes, 0, bytes.Length)) != 0) { var getData = ByteHandler.ByteArrayToObject <DataUser>(bytes); if (getData.Entry == Entry.SignUp) { user = getData; } else if (getData.Entry == Entry.SignIn && HandlerValidate.IsValidateData(user, getData)) { user.IsActivePanel = true; SendMessageClient(); } } } } } } catch (SocketException socketException) { Debug.Log("SocketException " + socketException.ToString()); } }