Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConnectPort
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int port = 9999;
  16.             IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, port);
  17.             Console.WriteLine(">>>  Server Start On Port "+ port);
  18.             Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  19.             sock.Bind(ipEnd);
  20.             sock.Listen(100);
  21.             Socket clientSock = sock.Accept();
  22.             byte[] clientData = new byte[1024];
  23.             int receivedBytesLen;  
  24.             Console.WriteLine(">>>  Accept Client");
  25.             Console.WriteLine();
  26.             receivedBytesLen = clientSock.Receive(clientData);
  27.             string clientDataInString = Encoding.ASCII.GetString(clientData, 0, receivedBytesLen);
  28.             Console.WriteLine("Received Data {0}", clientDataInString);
  29.             while(clientSock.Connected)
  30.             {
  31.                 try
  32.                 {
  33.                     string clientStr = "Client Data Received";
  34.                     byte[] sendData = new byte[1024];
  35.                     sendData = Encoding.ASCII.GetBytes(clientStr);
  36.                     clientSock.Send(sendData);
  37.                 }
  38.                 catch
  39.                 {
  40.                 }
  41.                 sock.Close();
  42.             }
  43.             clientSock.Close();
  44.             Console.WriteLine(">>>  Exit!");
  45.             Console.ReadLine();
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement