Advertisement
Whistik

WinView - Client side

May 17th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Net.Sockets;
  8.  
  9. namespace WinView___Client
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             bool isConnected = false;
  16.             bool isQuited = false;
  17.             while (!isConnected)
  18.             {
  19.                 TcpClient tcpclient = new TcpClient("127.0.0.1", 1200);
  20.                 Console.WriteLine("Trying to connect...");
  21.                 NetworkStream stream = tcpclient.GetStream();
  22.                 Console.WriteLine("Connected to server!");
  23.                 isConnected = true;
  24.                 while (!isQuited)
  25.                 {
  26.                     string clientCommand = Console.ReadLine();
  27.                     byte[] clientCommandByte = Encoding.Unicode.GetBytes(clientCommand);
  28.                    
  29.                     if (clientCommand != "!quit")
  30.                     {
  31.                         stream.Write(clientCommandByte, 0, clientCommandByte.Length);
  32.                         Console.WriteLine(clientCommand);
  33.                     }
  34.                     else
  35.                     {
  36.                         string quitMessage = "Client has been disconnected!";
  37.                         byte[] quitMessagebyte = Encoding.Unicode.GetBytes(quitMessage);
  38.                         stream.Write(quitMessagebyte, 0, quitMessagebyte.Length);
  39.                         tcpclient.Close();
  40.                         isQuited = true;
  41.                     }
  42.                    
  43.                 }
  44.             }
  45.            
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement