Advertisement
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;
- public class SocketServer
- {
- public static void Main(string [] args)
- {
- // устанавливаем для сокета локальную конечную точку
- IPHostEntry ipHost = Dns.Resolve("localhost");
- IPAddress ipAddr = ipHost.AddressList[0];
- IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
- // создаем сокет TCP/IP
- Socket sListener = new Socket(AddressFamily.InterNetwork,
- SocketType.Stream, ProtocolType.Tcp);
- // назначаем сокет локальной конечной точке и
- // слушаем входящие сокеты
- try
- {
- sListener.Bind(ipEndPoint);
- sListener.Listen(10);
- // Начинаем слушать соединение
- while (true)
- {
- Console.WriteLine ("Waiting for a connection on port 0",
- ipEndPoint);
- // программа приостанавливается, ожидая входящее соединение
- Socket handler = sListener.Accept();
- string data = null;
- // мы дождались клиента, пытающегося с нами соединиться
- while (true)
- {
- byte[] bytes = new byte[1024];
- int bytesRec = handler.Receive(bytes);
- data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
- if (data.IndexOf("<TheEnd>") > -1)
- {
- break;
- }
- }
- // показываем данные на консоли
- Console.WriteLine("Text Received: 0", data);
- string theReply = "Thank you for those "
- + data.Length.ToString() + " characters. . .";
- byte[] msg = Encoding.ASCII.GetBytes(theReply);
- handler.Send(msg);
- handler.Shutdown(SocketShutdown.Both);
- handler.Close;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- } //конец программы Main
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement