public void Process() { try { while (true) { try { int bytes = 0; byte[] data = new byte[1024]; bytes = client.Receive(data); Action action = (Action)(data[0] - 48); DefineAction(action, ref data, bytes); } catch (Exception ex) { Console.WriteLine(ex.Message); break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { server.RemoveConnection(this.Id); Close(); } }
public void Close() { server.RemoveConnection(Id); if (client != null) { client.Close(); } server.СlientSignInOutMessage(userName, Id, 0); }
public void Process() { try { Stream = client.GetStream(); // получаем имя пользователя string message = GetMessage(); userName = message; message = userName + " вошел в чат"; // посылаем сообщение о входе в чат всем подключенным пользователям server.BroadcastMessage(message, this.Id); Console.WriteLine(message); // в бесконечном цикле получаем сообщения от клиента while (true) { try { message = GetMessage(); message = String.Format("{0}: {1}", userName, message); Console.WriteLine(message); server.BroadcastMessage(message, this.Id); } catch { message = String.Format("{0}: покинул чат", userName); Console.WriteLine(message); server.BroadcastMessage(message, this.Id); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { // в случае выхода из цикла закрываем ресурсы server.RemoveConnection(this.Id); Close(); } }
//------------------------------------------------------------------------------------------------------- public void Process() { try { stream = client.GetStream(); string message = GetMessage(); userName = message; message = userName + " вошел в чат. "; server.BroadcastMessage(message, this.Id); Console.WriteLine(message); while (true) { try { message = GetMessage(); message = String.Format(message); Console.WriteLine(message); server.BroadcastMessage(message, this.Id); } catch { message = String.Format("{0} : покинул чат =(.", userName); Console.WriteLine(message); server.BroadcastMessage(message, this.Id); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { server.RemoveConnection(this.Id); Close(); } }
public void Process() { try { Stream = Client.GetStream(); // get username string message = GetMessage(); UserName = message; message = UserName + " entered the chat"; // send a message about entering the chat to all connected users Server.BroadcastMessage(message, this.Id); Console.WriteLine(message); // receive messages from the client in an endless loop while (true) { try { message = GetMessage(); message = String.Format("{0}: {1}", UserName, message); Console.WriteLine(message); Server.BroadcastMessage(message, this.Id); } catch { message = String.Format("{0}: left chat", UserName); Console.WriteLine(message); Server.BroadcastMessage(message, this.Id); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { Server.RemoveConnection(this.Id); Close(); } }