+1 vote
by (240 points)

Hello,

I am currently using TcpClient from System.Net.Sockets namespace for sending and receiving small packets to a server.

I would to know if exists some class in Rebex library that I can use instead TcpClient to connect through a proxy or firewall because System.Net.Sockets.TcpClient does not offer that option.

Kind regards,
Javier Gutiérrez

2 Answers

0 votes
by (58.9k points)
selected by
 
Best answer

Hello,

thank you for your additional information in the comment.

Actually, it is possible to bind the System.Net.Sockets.TcpClient to our ProxySocket class. Then you will be able to work with the TcpClient, but the underlying connection will be handled by the Rebex.Net.ProxySocket that supports SOCKS4/SOCKS5 and HTTP CONNECT proxy servers. This setup will allow you to specify the proxy server, port (authenticating is also possible) and at the same time use the familiar System.Net.TcpClient that you are already using.

Please try this code:

// create a proxy socket
Proxy proxy = new Proxy();

// set the proxy server parameters
proxy.ProxyType = ProxyType.Socks5; // choose the type of Proxy you need
proxy.Host = "your-proxy-example.com";
proxy.Port = 8080;

// create an instance of Rebex.Net.Proxysocket
ProxySocket proxySocket = new ProxySocket(proxy);

// connect to your server through the proxy
proxySocket.Connect("192.168.62.78", 8051);

TcpClient client = new TcpClient();

// bind Rebex ProxySocket to the .NET TcpClient
client.Client = proxySocket.Socket;

// now you can work with the TcpClient:
var stream = client.GetStream();

var data = ...;
stream.Write(data, 0, data.Length);

Please give it a try and let me know whether you are able to connect through the proxy now.

by (240 points)
It works like a charm!.

Thank you very much.
by (58.9k points)
Great! Thanks for letting us know :-)
0 votes
by (58.9k points)

Hello,

Rebex components include ProxySocket object in the Rebex.Networking.dll. ProxySocket is a client-side HTTP, Socks4 and Socks5 proxy object that is used by our client objects (e.g. POP3, IMAP SFTP, FTP, etc). It can be used stand-alone as well, and other protocols can be run over it.

Not knowing what protocol you are using (?) I wrote a sample code that can be used to connect to an HTTP server through a proxy to demonstrate its capabilities:

// create a proxy socket
Proxy proxy = new Proxy(ProxyType.Socks5, "proxy-example.org", 1080);

// specify other parameters if needed, e.g. username and password
proxy.UserName = "proxy-user";
proxy.Password = "proxy-password";

// create an instance of Rebex.Net.Proxysocket
ProxySocket proxySocket = new ProxySocket(proxy);

// connect to server through the proxy
proxySocket.Connect("http-server01", 80);

// send a request
byte[] request = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nHost: server01\r\n\r\n");
proxySocket.Send(request);

// receive response (needs to be actually implemented)
byte[] response = new byte[4096];
proxySocket.Receive(response);
...

However, to actually make this usable, you would have to provide a client object that can use the ProxySocket object as its transport layer. But given that you already are using System.Net.Sockets.TcpClient, this must be obvious.

Let me know whether the ProxySocket class solves your needs. In fact, you can purchase is as part of any Rebex networking-capable component. (The cheapest one being Rebex Time).

by (240 points)
Hello,

This is an example of what I am using to connect and sending data. I am using a custom protocol withing the server and the client.

var c = new System.Net.Sockets.TcpClient();
c.Connect("192.168.62.78", 8051);
var stream = c.GetStream();
var data = ...;
stream.Write(data, 0, data.Length);

I already have a "Rebex Total Pack" licence so this would not be a problem.

I really appreciate your help.

Kind regards,
Javier Gutiérrez
...