This is very probably caused by the HTTP proxy you are using.
I tried to connect to your server with our HTTP proxy more than 10 times in a row and it worked well.
You can try it for yourself using just plain ProxySocket
:
private static string WaitForWelcomeMessage(string host, int port, string proxyHost, int proxyPort)
{
// initialize proxy socket
var proxy = new Rebex.Net.Proxy(Rebex.Net.ProxyType.HttpConnect, proxyHost, proxyPort);
proxy.LogWriter = new Rebex.ConsoleLogWriter(Rebex.LogLevel.Verbose);
var socket = new Rebex.Net.ProxySocket(proxy);
socket.Timeout = 5000;
try
{
// connect to host using specified proxy
socket.Connect(host, port);
// read bytes until welcome message is received
int total = 0;
byte[] buffer = new byte[1024];
while (true)
{
int n = socket.Receive(buffer, total, buffer.Length - total);
if (n <= 0)
{
// log connection closure
proxy.LogWriter.Write(Rebex.LogLevel.Verbose, socket.GetType(), 0, "Socket", "Connection closed.");
return null;
}
// log received data
proxy.LogWriter.Write(Rebex.LogLevel.Verbose, socket.GetType(), 0, "Socket", "Received data:", buffer, total, n);
total += n;
// find new line in received data
int newLine = Array.IndexOf<byte>(buffer, (byte)'\n', 0, total);
if (newLine >= 0)
return Encoding.ASCII.GetString(buffer, 0, newLine).TrimEnd();
// this should never arise
if (total == buffer.Length)
{
proxy.LogWriter.Write(Rebex.LogLevel.Error, socket.GetType(), 0, "Socket", "No new line in 1KB.");
return null;
}
}
}
catch (Exception ex)
{
proxy.LogWriter.Write(Rebex.LogLevel.Error, socket.GetType(), 0, "Socket", ex.ToString());
return null;
}
finally
{
socket.Close();
}
}
Use the method like this:
string host = "xxxxx";
int port = 22;
string proxyHost = "xxxxx";
int proxyPort = 8080;
int TOTAL = 12;
int success = 0;
for (int i = 0; i < TOTAL; i++)
{
Console.WriteLine("Waiting for welcome message ...");
string message = WaitForWelcomeMessage(host, port, proxyHost, proxyPort);
if (message != null)
{
Console.WriteLine("Welcome message received: {0}", message);
success++;
}
else
{
Console.WriteLine("Welcome message not received.");
}
}
Console.WriteLine("Total attempts: {0}", TOTAL);
Console.WriteLine("Success attempts: {0}", success);
Console.WriteLine("Failed attempts: {0}", TOTAL - success);
You can use some network analyzer such as Wireshark to ensure the welcome message was not received by network socket in case of failure.
If network analyzer shows, that the welcom message was sent by network, but the ProxySocket
didn't receive it, it is a bug in the Rebex code. In this case, please send us the produced Verbose log (output of the program above) and network capture (created by e.g. Wireshark).
Alternatively, you can use Rebex.FileLogWriter
instead of Rebex.ConsoleLogWriter
for easier log creation.