Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net.Sockets;
- using System.Net;
- using System.Text;
- class Program
- {
- static void Main(string[] args)
- {
- int bRec;
- string data;
- byte[] b = new byte[1024];
- IPHostEntry ipHost = Dns.GetHostEntry("localhost");
- IPAddress ipAddr = ipHost.AddressList[1];
- IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11320);
- Console.WriteLine("Сервер успешно загружен");
- Console.WriteLine("Порт:"+ipEndPoint.Port);
- Socket sListener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);// создаем сокет
- sListener.Bind(ipEndPoint);// связываем сокет с точкой подключения
- sListener.Listen(10);// запускаем прослушивание
- while (true)
- {
- Console.WriteLine();
- Socket handler = sListener.Accept();
- bRec = handler.Receive(b);
- while (true)
- {
- data = Encoding.UTF8.GetString(b, 0, bRec);// принимаем сообщение
- Console.Write(handler.LocalEndPoint + ": ");
- Console.WriteLine(data);
- byte[] msg = Encoding.UTF8.GetBytes("привет от сервера");
- handler.Send(msg);
- bRec = handler.Receive(b);
- if (Encoding.UTF8.GetString(b, 0, bRec) == "end") { break; }
- }
- handler.Shutdown(SocketShutdown.Both);
- handler.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment