public void Connect(object port) { try { IPEndPoint IpEndPoint = new IPEndPoint(IPAddress.Any, Convert.ToInt32(port)); Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { SendBufferSize = 50 * 1024, ReceiveBufferSize = 50 * 1024, ReceiveTimeout = -1, SendTimeout = -1, }; Server.Bind(IpEndPoint); Server.Listen(30); HandleLogs.Addmsg($"Listenning {port}", Color.Green); Server.BeginAccept(EndAccept, null); } catch (Exception ex) { MessageBox.Show(ex.Message); Environment.Exit(0); } }
public Clients(Socket socket) { if (Settings.Blocked.Contains(socket.RemoteEndPoint.ToString().Split(':')[0])) { Disconnected(); return; } else { HandleLogs.Addmsg($"Client {socket.RemoteEndPoint.ToString().Split(':')[0]} connected successfully", Color.Green); } ClientSocket = socket; ClientBuffer = new byte[1]; ClientBuffersize = 0; ClientBufferRecevied = false; ClientMS = new MemoryStream(); LV = null; SendSync = new object(); EndSendSync = new object(); BytesRecevied = 0; ClientSocket.BeginReceive(ClientBuffer, 0, ClientBuffer.Length, SocketFlags.None, ReadClientData, null); }
public async void ReadClientData(IAsyncResult ar) { try { if (!ClientSocket.Connected) { Disconnected(); return; } else { int Recevied = ClientSocket.EndReceive(ar); if (Recevied > 0) { if (ClientBufferRecevied == false) { await ClientMS.WriteAsync(ClientBuffer, 0, ClientBuffer.Length); ClientBuffersize = BitConverter.ToInt32(ClientMS.ToArray(), 0); ClientMS.Dispose(); ClientMS = new MemoryStream(); if (ClientBuffersize > 0) { ClientBuffer = new byte[ClientBuffersize]; Debug.WriteLine("/// Server Buffersize " + ClientBuffersize.ToString() + " Bytes ///"); ClientBufferRecevied = true; } } else { await ClientMS.WriteAsync(ClientBuffer, 0, Recevied); Settings.Received += Recevied; BytesRecevied += Recevied; if (ClientMS.Length == ClientBuffersize) { try { ThreadPool.QueueUserWorkItem(HandlePacket.Read, new object[] { Settings.aes256.Decrypt(ClientMS.ToArray()), this }); } catch (CryptographicException) { HandleLogs.Addmsg($"Client {ClientSocket.RemoteEndPoint.ToString().Split(':')[0]} tried to connect with wrong password, IP blocked", Color.Red); Settings.Blocked.Add(ClientSocket.RemoteEndPoint.ToString().Split(':')[0]); Disconnected(); return; } ClientBuffer = new byte[4]; ClientMS.Dispose(); ClientMS = new MemoryStream(); ClientBufferRecevied = false; } else { ClientBuffer = new byte[ClientBuffersize - ClientMS.Length]; } } ClientSocket.BeginReceive(ClientBuffer, 0, ClientBuffer.Length, SocketFlags.None, ReadClientData, null); } else { Disconnected(); return; } } } catch { Disconnected(); return; } }