AddO1f

Seti_3_Server

Apr 2nd, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Net;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         int bRec;
  11.         string data;
  12.         byte[] b = new byte[1024];
  13.  
  14.         IPHostEntry ipHost = Dns.GetHostEntry("localhost");
  15.         IPAddress ipAddr = ipHost.AddressList[1];
  16.         IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11320);
  17.  
  18.         Console.WriteLine("Сервер успешно загружен");
  19.         Console.WriteLine("Порт:"+ipEndPoint.Port);
  20.  
  21.         Socket sListener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);// создаем сокет
  22.         sListener.Bind(ipEndPoint);//  связываем сокет с точкой подключения
  23.         sListener.Listen(10);// запускаем прослушивание
  24.  
  25.         while (true)
  26.         {
  27.             Console.WriteLine();
  28.             Socket handler = sListener.Accept();
  29.             bRec = handler.Receive(b);
  30.             while (true)
  31.             {
  32.                 data = Encoding.UTF8.GetString(b, 0, bRec);// принимаем сообщение
  33.                 Console.Write(handler.LocalEndPoint + ": ");
  34.                 Console.WriteLine(data);                
  35.  
  36.                 byte[] msg = Encoding.UTF8.GetBytes("привет от сервера");
  37.                 handler.Send(msg);
  38.  
  39.                 bRec = handler.Receive(b);
  40.                 if (Encoding.UTF8.GetString(b, 0, bRec) == "end") { break; }
  41.             }
  42.             handler.Shutdown(SocketShutdown.Both);
  43.             handler.Close();
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment