all groups > dotnet distributed apps > october 2004 >
You're in the

dotnet distributed apps

group:

SecureSocket over a proxy server


SecureSocket over a proxy server Romain A
10/29/2004 1:01:03 AM
dotnet distributed apps: I am trying to move an existing piece of code which use
System.Net.Sockets.Socket (.NET 1.1) into something which
would use a securesocket.

I have managed to use the SecureSocket made by
http://www.mentalis.org/soft/projects/ssocket/
when making direct connection to a server, however I do
not seem capable of connecting through a proxy server.

Has anyone any exerience in the matter.

I have posted the code which I have modified from the
example given in MSDN for the Socket class and the example
provided by Mentalis.

The following code always thrown an exception: "No
connection could be made because the target machine
actively refused it" from the Mentalis class.

I am thinking the problem could be with the actual
mentalis class, Would you know of any other 3rd party I
could use ?

Any other ideas welcome.

Regards

Romain

static SecurityOptions options = new
SecurityOptions(
SecureProtocol.Ssl3 |
SecureProtocol.Tls1, // use SSL3 or TLS1

null, // do not use
client authentication

ConnectionEnd.Client, // this is
the client side

CredentialVerification.None, // do not
check the certificate -- this should not be used in a real-
life application :-)

null, // not used
with automatic certificate verification
"www.freessl.com",
// this is the common name of the Microsoft web server

SecurityFlags.Default, // use the
default security flags

SslAlgorithms.ALL, // only use
secure ciphers
null);

private static SecureSocket connectSocket(string
server, int port)
{

SecureSocket s = null;
IPHostEntry iphe = null;

try
{
// Get host related information.
iphe = Dns.Resolve(server);

// Loop through the AddressList to obtain
the supported AddressFamily. This is to avoid
// an exception to be thrown if the host
IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress ipad in iphe.AddressList)
{
IPEndPoint ipe = new IPEndPoint(ipad,
port);

SecureSocket tmpS =
//new Socket(ipe.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
new SecureSocket
(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp, options);

tmpS.Connect(ipe);

if(tmpS.Connected)
{
s = tmpS;
break;
}
else
continue;
}
}

catch(SocketException e)
{
Console.WriteLine("SocketException
caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " +
e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " +
e.Message);
}
return s;
}

// This method requests the home page content for
the passed server.
// It displays the number of bytes received and
the page content.
private static string socketSendReceive(string
server, int port)
{
//Set up variables and String to write to the
server.
Encoding ASCII = Encoding.ASCII;
string Get = "GET
https://www.freessl.com/test/freessl.html HTTP/1.0
\r\nHost: www.freessl.com\r\nConnection: Close\r\nUser-
Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
\r\n\r\n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[512];
String strRetPage = null;

// Create a socket connection with the
specified server and port.
SecureSocket s = connectSocket(server, port);

if (s == null)
return ("Connection failed : 'Connected'
is false !");

try
{
// Send request to the server.
s.Send(ByteGet, ByteGet.Length, 0);

// Receive the server home page content.
Int32 bytes = s.Receive(RecvBytes,
RecvBytes.Length, 0);

// Read the first 256 bytes.
strRetPage = "HTML page on " + server
+ ":\r\n";
strRetPage = strRetPage + ASCII.GetString
(RecvBytes, 0, bytes);

while (bytes > 0)
{
bytes = s.Receive(RecvBytes,
RecvBytes.Length, 0);
strRetPage = strRetPage +
ASCII.GetString(RecvBytes, 0, bytes);
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException
caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " +
e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " +
e.Message);
}
return strRetPage;
}

public static void Main(string[] args)
{
string host;
int port = 443;

if (args.Length == 0)
// If no server name is passed as argument
to this program, use the current
// host name as default.
host = Dns.GetHostName();
else
host = args[0];

host = "dsproxy";
//host = "www.freessl.com";

string result = socketSendReceive(host, port);

Console.WriteLine(result);
Console.ReadLine();

}

Re: SecureSocket over a proxy server Feroze [msft]
11/10/2004 12:40:48 PM
Are you trying to access a website using SSL? If so, why not just use
HttpWebRequest? Why are you using Sockets here?

--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------

[quoted text, click to view]
AddThis Social Bookmark Button