Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Net;
  4. using System.Text;
  5.  
  6. namespace MultiClient
  7. {
  8.     class Program
  9.     {
  10.         private static readonly Socket ClientSocket = new Socket
  11.             (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  12.  
  13.         private const int PORT = 100;
  14.  
  15.         static void Main()
  16.         {
  17.             Console.Title = "Client";
  18.             ConnectToServer();
  19.             RequestLoop();
  20.             Exit();
  21.         }
  22.  
  23.         private static void ConnectToServer()
  24.         {
  25.             int attempts = 0;
  26.  
  27.             while (!ClientSocket.Connected)
  28.             {
  29.                 try
  30.                 {
  31.                     attempts++;
  32.                     Console.WriteLine("Connection attempt " + attempts);
  33.                     // Change IPAddress.Loopback to a remote IP to connect to a remote host.
  34.                     ClientSocket.Connect(IPAddress.Loopback, PORT);
  35.                 }
  36.                 catch (SocketException)
  37.                 {
  38.                     Console.Clear();
  39.                 }
  40.             }
  41.  
  42.             Console.Clear();
  43.             Console.WriteLine("Connected");
  44.         }
  45.  
  46.         private static void RequestLoop()
  47.         {
  48.             Console.WriteLine(@"<Type ""exit"" to properly disconnect client>");
  49.  
  50.             while (true)
  51.             {
  52.                 SendRequest();
  53.                 ReceiveResponse();
  54.             }
  55.         }
  56.  
  57.         /// <summary>
  58.         /// Close socket and exit program.
  59.         /// </summary>
  60.         private static void Exit()
  61.         {
  62.             SendString("exit"); // Tell the server we are exiting
  63.             ClientSocket.Shutdown(SocketShutdown.Both);
  64.             ClientSocket.Close();
  65.             Environment.Exit(0);
  66.         }
  67.  
  68.         private static void SendRequest()
  69.         {
  70.             Console.Write("Send a request: ");
  71.             string request = Console.ReadLine();
  72.             SendString(request);
  73.  
  74.             if (request.ToLower() == "exit")
  75.             {
  76.                 Exit();
  77.             }
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Sends a string to the server with ASCII encoding.
  82.         /// </summary>
  83.         private static void SendString(string text)
  84.         {
  85.             byte[] buffer = Encoding.ASCII.GetBytes(text);
  86.             ClientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
  87.         }
  88.  
  89.         private static void ReceiveResponse()
  90.         {
  91.             var buffer = new byte[2048];
  92.             int received = ClientSocket.Receive(buffer, SocketFlags.None);
  93.             if (received == 0) return;
  94.             var data = new byte[received];
  95.             Array.Copy(buffer, data, received);
  96.             string text = Encoding.ASCII.GetString(data);
  97.             Console.WriteLine(text);
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement