AddO1f

Seti_4_Client

Apr 2nd, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net.Sockets;
  4. using System.Net;
  5. using System.Text;
  6.  
  7. class Program
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         string name;
  12.         string message;
  13.         int bytesSend;
  14.         byte[] bytes = new byte[1024];
  15.  
  16.         FileStream myFStream = new FileStream("temp.dat",FileMode.OpenOrCreate,FileAccess.ReadWrite);
  17.         BinaryWriter binWr = new BinaryWriter(myFStream);
  18.  
  19.         Console.WriteLine("Для завершения работы клиента пропишите end");
  20.         Console.Write("Введите ник:");
  21.         name = Console.ReadLine();
  22.  
  23.         IPHostEntry ipHost = Dns.GetHostEntry("localhost");
  24.         IPAddress ipAddr = ipHost.AddressList[1];
  25.         IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11320);
  26.  
  27.         Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  28.         sender.Connect(ipEndPoint);
  29.  
  30.         binWr.Write(name);
  31.         bytesSend = sender.Send(Encoding.UTF8.GetBytes(name));
  32.         Console.WriteLine("Соединение установлено!");
  33.         Console.WriteLine();
  34.  
  35.         while (true)
  36.         {
  37.             message = Console.ReadLine();
  38.             binWr.Write(message);
  39.             bytesSend = sender.Send(Encoding.UTF8.GetBytes(message));
  40.             if (message.Equals("end"))
  41.             {
  42.                 sender.Close();
  43.                 break;
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment