Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- class Program {
- private static Random random = new Random();
- static void Main(string[] args) {
- IPAddress ip = IPAddress.Parse("127.0.0.1");
- int port = 5000;
- TcpClient client = new TcpClient();
- client.Connect(ip, port);
- Console.WriteLine("client connected!!");
- Thread thread = new Thread(o => ReceiveData((TcpClient)o));
- thread.Start(client);
- Console.ReadKey();
- client.Client.Shutdown(SocketShutdown.Send);
- thread.Join();
- //ns.Close();
- client.Close();
- Console.WriteLine("disconnect from server!!");
- Console.ReadKey();
- }
- static void ReceiveData(TcpClient client) {
- NetworkStream ns = client.GetStream();
- byte[] receivedBytes = new byte[1024];
- int byte_count;
- while ((byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length)) > 0) {
- String receivedDt = Encoding.ASCII.GetString(receivedBytes, 0, byte_count);
- Console.Write("in:" + receivedDt);
- int val = random.Next(3, 8) * 1000;
- Thread.Sleep(val);
- byte[] buffer = Encoding.ASCII.GetBytes(receivedDt);
- ns.Write(buffer, 0, buffer.Length);
- Console.Write("ot:" + val);
- }
- }
- }
Add Comment
Please, Sign In to add comment