emad ali

emad ali

  • NA
  • 11
  • 2.3k

how i can send data using TcpListener?

May 9 2018 1:11 PM
I want to be able to send data by using TcpListener how can i achieve this form this socks5 proxy project that listen to many connections using TcpListener.
i want to be able send data using only TcpListener from this 2 codes samples i am able to listen to many connections but i am not able to send data any help?
the link to whole project https://github.com/ThrDev/Socks5
**TcpServer**
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Net.Sockets;  
  5. using System.Net;  
  6. using System.Threading;  
  7. namespace socks5.TCP  
  8. {  
  9. public class TcpServer  
  10. {  
  11. private TcpListener p;  
  12. private bool accept = false;  
  13. public int PacketSize{get;set;}  
  14.   
  15. public event EventHandler<ClientEventArgs> onClientConnected = delegate { };  
  16. public event EventHandler<ClientEventArgs> onClientDisconnected = delegate { };  
  17. //public event EventHandler<DataEventArgs> onDataReceived = delegate { };  
  18. //public event EventHandler<DataEventArgs> onDataSent = delegate { };  
  19. public TcpServer(IPAddress ip, int port)  
  20. {  
  21. p = new TcpListener(ip, port);  
  22. }  
  23. private ManualResetEvent Task = new ManualResetEvent(false);  
  24. private void AcceptConnections()  
  25. {  
  26. while(accept)  
  27. {  
  28. try  
  29. {  
  30. Task.Reset();  
  31. p.BeginAcceptSocket(new AsyncCallback(AcceptClient), p);  
  32. Task.WaitOne();  
  33. }  
  34. catch { //error, most likely server shutdown.  
  35. }  
  36. }  
  37. }  
  38. void AcceptClient(IAsyncResult res)  
  39. {  
  40. try  
  41. {  
  42. TcpListener px = (TcpListener)res.AsyncState;  
  43. Socket x = px.EndAcceptSocket(res);  
  44. Task.Set();  
  45. Client f = new Client(x, PacketSize);  
  46. //f.onClientDisconnected += onClientDisconnected;  
  47. //f.onDataReceived += onDataReceived;  
  48. //f.onDataSent += onDataSent;  
  49. onClientConnected(thisnew ClientEventArgs(f));  
  50. }  
  51. catch(Exception ex)  
  52. {  
  53. Console.WriteLine(ex.ToString());  
  54. //server stopped or client errored?  
  55. }  
  56. }  
  57. public void Start()  
  58. {  
  59. if (!accept)  
  60. {  
  61. accept = true;  
  62. p.Start(10000);  
  63. new Thread(new ThreadStart(AcceptConnections)).Start();  
  64. }  
  65. }  
  66. public void Stop()  
  67. {  
  68. if (accept)  
  69. {  
  70. accept = false;  
  71. p.Stop();  
  72. Task.Set();  
  73. }  
  74. }  
  75. }  
  76. }  
**Socks5Server**
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using socks5.TCP;  
  5. using System.Net;  
  6. using System.Threading;  
  7. using socks5.Plugin;  
  8. using socks5.Socks;  
  9. namespace socks5  
  10. {  
  11. public class Socks5Server  
  12. {  
  13. public int Timeout { getset; }  
  14. public int PacketSize { getset; }  
  15. public bool LoadPluginsFromDisk { getset; }  
  16. public IPAddress OutboundIPAddress { getset; }  
  17. private TcpServer _server;  
  18. private Thread NetworkStats;  
  19. public List<SocksClient> Clients = new List<SocksClient>();  
  20. public Stats Stats;  
  21. private bool started;  
  22. public Socks5Server(IPAddress ip, int port)  
  23. {  
  24. Timeout = 5000;  
  25. PacketSize = 4096;  
  26. LoadPluginsFromDisk = false;  
  27. Stats = new Stats();  
  28. OutboundIPAddress = IPAddress.Any;  
  29. _server = new TcpServer(ip, port);  
  30. _server.onClientConnected += _server_onClientConnected;  
  31. }  
  32. public void Start()  
  33. {  
  34. if (started) return;  
  35. Plugin.PluginLoader.LoadPluginsFromDisk = LoadPluginsFromDisk;  
  36. PluginLoader.LoadPlugins();  
  37. _server.PacketSize = PacketSize;  
  38. _server.Start();  
  39. started = true;  
  40. //start thread.  
  41. NetworkStats = new Thread(new ThreadStart(delegate()  
  42. {  
  43. while (started)  
  44. {  
  45. if (this.Clients.Contains(null))  
  46. this.Clients.Remove(null);  
  47. Stats.ResetClients(this.Clients.Count);  
  48. Thread.Sleep(1000);  
  49. }  
  50. }));  
  51. NetworkStats.Start();  
  52. }  
  53. public void Stop()  
  54. {  
  55. if (!started) return;  
  56. _server.Stop();  
  57. for (int i = 0; i < Clients.Count; i++)  
  58. {  
  59. Clients[i].Client.Disconnect();  
  60. }  
  61. Clients.Clear();  
  62. started = false;  
  63. }  
  64. void _server_onClientConnected(object sender, ClientEventArgs e)  
  65. {  
  66. //Console.WriteLine("Client connected.");  
  67. //call plugins related to ClientConnectedHandler.  
  68. foreach (ClientConnectedHandler cch in PluginLoader.LoadPlugin(typeof(ClientConnectedHandler)))  
  69. {  
  70. try  
  71. {  
  72. if (!cch.OnConnect(e.Client, (IPEndPoint)e.Client.Sock.RemoteEndPoint))  
  73. {  
  74. e.Client.Disconnect();  
  75. return;  
  76. }  
  77. }  
  78. catch  
  79. {  
  80. }  
  81. }  
  82. SocksClient client = new SocksClient(e.Client);  
  83. e.Client.onDataReceived += Client_onDataReceived;  
  84. e.Client.onDataSent += Client_onDataSent;  
  85. client.onClientDisconnected += client_onClientDisconnected;  
  86. Clients.Add(client);  
  87. client.Begin(this.OutboundIPAddress, this.PacketSize, this.Timeout);  
  88. }  
  89. void client_onClientDisconnected(object sender, SocksClientEventArgs e)  
  90. {  
  91. e.Client.onClientDisconnected -= client_onClientDisconnected;  
  92. e.Client.Client.onDataReceived -= Client_onDataReceived;  
  93. e.Client.Client.onDataSent -= Client_onDataSent;  
  94. this.Clients.Remove(e.Client);  
  95. foreach (ClientDisconnectedHandler cdh in PluginLoader.LoadPlugin(typeof(ClientDisconnectedHandler)))  
  96. {  
  97. try  
  98. {  
  99. cdh.OnDisconnected(sender, e);  
  100. }  
  101. catch  
  102. {  
  103. }  
  104. }  
  105. }  
  106. //All stats data is "Server" bandwidth stats, meaning clientside totals not counted.  
  107. void Client_onDataSent(object sender, DataEventArgs e)  
  108. {  
  109. //Technically we are sending data from the remote server to the client, so it's being "received"  
  110. this.Stats.AddBytes(e.Count, ByteType.Received);  
  111. this.Stats.AddPacket(PacketType.Received);  
  112. }  
  113. void Client_onDataReceived(object sender, DataEventArgs e)  
  114. {  
  115. //Technically we are receiving data from the client and sending it to the remote server, so it's being "sent"  
  116. this.Stats.AddBytes(e.Count, ByteType.Sent);  
  117. this.Stats.AddPacket(PacketType.Sent);  
  118. }  
  119. }  
  120. }

Attachment: Socks5-master.rar