Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.Diagnostics;
- namespace Client
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stopwatch s = new Stopwatch();
- s.Reset();
- TcpClient client = null;
- (new Thread(new ThreadStart(() =>
- {
- Console.WriteLine("Connecting to server 127.0.0.1");
- while (true)
- {
- bool connected = false;
- while (!connected)
- {
- Console.WriteLine("Connecting to server 127.0.0.1");
- try
- {
- client = new TcpClient("127.0.0.1", 1234);
- connected = true;
- Console.WriteLine("Connected!");
- }
- catch { connected = false; }
- }
- while (true)
- {
- int bytesRead = 0;
- byte[] buff = new byte[1024 * 1024];
- try
- {
- bytesRead = 0;
- bytesRead = client.GetStream().Read(buff, 0, buff.Length);
- }
- catch { }
- if (bytesRead > 0)
- {
- string txt = System.Text.ASCIIEncoding.UTF8.GetString(buff, 0, bytesRead);
- Console.WriteLine("Received:" + txt);
- if (txt == "pong")
- {
- s.Stop();
- Console.WriteLine("Time elapsed: " + (((double)s.Elapsed.Milliseconds)/((double)1000)));
- s.Reset();
- }
- }
- }
- }
- }))).Start();
- string input="";
- while ((input = Console.ReadLine()) != "Exit")
- {
- if (input == "ping")
- s.Start();
- byte[] b=System.Text.ASCIIEncoding.UTF8.GetBytes(input);
- client.GetStream().Write(b, 0, b.Length);
- client.GetStream().Flush();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement