TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
emad ali
NA
11
2.5k
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**
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Net.Sockets;
using
System.Net;
using
System.Threading;
namespace
socks5.TCP
{
public
class
TcpServer
{
private
TcpListener p;
private
bool
accept =
false
;
public
int
PacketSize{
get
;
set
;}
public
event
EventHandler<ClientEventArgs> onClientConnected =
delegate
{ };
public
event
EventHandler<ClientEventArgs> onClientDisconnected =
delegate
{ };
//public event EventHandler<DataEventArgs> onDataReceived = delegate { };
//public event EventHandler<DataEventArgs> onDataSent = delegate { };
public
TcpServer(IPAddress ip,
int
port)
{
p =
new
TcpListener(ip, port);
}
private
ManualResetEvent Task =
new
ManualResetEvent(
false
);
private
void
AcceptConnections()
{
while
(accept)
{
try
{
Task.Reset();
p.BeginAcceptSocket(
new
AsyncCallback(AcceptClient), p);
Task.WaitOne();
}
catch
{
//error, most likely server shutdown.
}
}
}
void
AcceptClient(IAsyncResult res)
{
try
{
TcpListener px = (TcpListener)res.AsyncState;
Socket x = px.EndAcceptSocket(res);
Task.Set();
Client f =
new
Client(x, PacketSize);
//f.onClientDisconnected += onClientDisconnected;
//f.onDataReceived += onDataReceived;
//f.onDataSent += onDataSent;
onClientConnected(
this
,
new
ClientEventArgs(f));
}
catch
(Exception ex)
{
Console.WriteLine(ex.ToString());
//server stopped or client errored?
}
}
public
void
Start()
{
if
(!accept)
{
accept =
true
;
p.Start(10000);
new
Thread(
new
ThreadStart(AcceptConnections)).Start();
}
}
public
void
Stop()
{
if
(accept)
{
accept =
false
;
p.Stop();
Task.Set();
}
}
}
}
**Socks5Server**
using
System;
using
System.Collections.Generic;
using
System.Text;
using
socks5.TCP;
using
System.Net;
using
System.Threading;
using
socks5.Plugin;
using
socks5.Socks;
namespace
socks5
{
public
class
Socks5Server
{
public
int
Timeout {
get
;
set
; }
public
int
PacketSize {
get
;
set
; }
public
bool
LoadPluginsFromDisk {
get
;
set
; }
public
IPAddress OutboundIPAddress {
get
;
set
; }
private
TcpServer _server;
private
Thread NetworkStats;
public
List<SocksClient> Clients =
new
List<SocksClient>();
public
Stats Stats;
private
bool
started;
public
Socks5Server(IPAddress ip,
int
port)
{
Timeout = 5000;
PacketSize = 4096;
LoadPluginsFromDisk =
false
;
Stats =
new
Stats();
OutboundIPAddress = IPAddress.Any;
_server =
new
TcpServer(ip, port);
_server.onClientConnected += _server_onClientConnected;
}
public
void
Start()
{
if
(started)
return
;
Plugin.PluginLoader.LoadPluginsFromDisk = LoadPluginsFromDisk;
PluginLoader.LoadPlugins();
_server.PacketSize = PacketSize;
_server.Start();
started =
true
;
//start thread.
NetworkStats =
new
Thread(
new
ThreadStart(
delegate
()
{
while
(started)
{
if
(
this
.Clients.Contains(
null
))
this
.Clients.Remove(
null
);
Stats.ResetClients(
this
.Clients.Count);
Thread.Sleep(1000);
}
}));
NetworkStats.Start();
}
public
void
Stop()
{
if
(!started)
return
;
_server.Stop();
for
(
int
i = 0; i < Clients.Count; i++)
{
Clients[i].Client.Disconnect();
}
Clients.Clear();
started =
false
;
}
void
_server_onClientConnected(
object
sender, ClientEventArgs e)
{
//Console.WriteLine("Client connected.");
//call plugins related to ClientConnectedHandler.
foreach
(ClientConnectedHandler cch
in
PluginLoader.LoadPlugin(
typeof
(ClientConnectedHandler)))
{
try
{
if
(!cch.OnConnect(e.Client, (IPEndPoint)e.Client.Sock.RemoteEndPoint))
{
e.Client.Disconnect();
return
;
}
}
catch
{
}
}
SocksClient client =
new
SocksClient(e.Client);
e.Client.onDataReceived += Client_onDataReceived;
e.Client.onDataSent += Client_onDataSent;
client.onClientDisconnected += client_onClientDisconnected;
Clients.Add(client);
client.Begin(
this
.OutboundIPAddress,
this
.PacketSize,
this
.Timeout);
}
void
client_onClientDisconnected(
object
sender, SocksClientEventArgs e)
{
e.Client.onClientDisconnected -= client_onClientDisconnected;
e.Client.Client.onDataReceived -= Client_onDataReceived;
e.Client.Client.onDataSent -= Client_onDataSent;
this
.Clients.Remove(e.Client);
foreach
(ClientDisconnectedHandler cdh
in
PluginLoader.LoadPlugin(
typeof
(ClientDisconnectedHandler)))
{
try
{
cdh.OnDisconnected(sender, e);
}
catch
{
}
}
}
//All stats data is "Server" bandwidth stats, meaning clientside totals not counted.
void
Client_onDataSent(
object
sender, DataEventArgs e)
{
//Technically we are sending data from the remote server to the client, so it's being "received"
this
.Stats.AddBytes(e.Count, ByteType.Received);
this
.Stats.AddPacket(PacketType.Received);
}
void
Client_onDataReceived(
object
sender, DataEventArgs e)
{
//Technically we are receiving data from the client and sending it to the remote server, so it's being "sent"
this
.Stats.AddBytes(e.Count, ByteType.Sent);
this
.Stats.AddPacket(PacketType.Sent);
}
}
}
Attachment:
Socks5-master.rar
Reply
Answers (
0
)
Show pdf document in axAcroPDF?
how to add date picker