Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace AsyncConnectorTest
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             Console.Write("Enter IP to connect to (with port): ");
  17.             var ip = Console.ReadLine();
  18.             Console.Write("Enter name: ");
  19.             var name = Console.ReadLine();
  20.             var handshake = "name:" + name;
  21.             var client = new TcpClient();
  22.             client.Connect(ip.Split(':')[0], int.Parse(ip.Split(':')[1]));
  23.             var ns = client.GetStream();
  24.             var sw = new StreamWriter(ns);
  25.             Thread.Sleep(1000); // let the server catch up
  26.             sw.WriteLine(handshake);
  27.             Console.WriteLine("Staying connected.. Press ESCAPE to exit.");
  28.             while (true)
  29.             {
  30.                 // stay connected
  31.                 if (!Console.KeyAvailable) continue;
  32.                 var key = Console.ReadKey();
  33.                 if (key.Key == ConsoleKey.Escape)
  34.                     Environment.Exit(1);
  35.             }
  36.  
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement