Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a TCP Client Connection with SSL

Tags:

c#

.net

tcp

ssl

I'm trying to create a TCP connection and send/read data that uses SSL, but I haven't been able to successfully accomplish this.

What I'd like to do is something like this:

    TcpClient _tcpClient = new TcpClient("host", 110);

    BinaryReader reader = 
       new BinaryReader(new System.Net.Security.SslStream(_tcpClient.GetStream(), true));

    Console.WriteLine(reader.ReadString());

I haven't had any luck with it though. An exception is thrown when creating the BinaryReader.

Does anyone know of a simple example that does this? I'm not interested in writing the server side of this, just the client.

like image 442
Mel Green Avatar asked Oct 31 '08 01:10

Mel Green


2 Answers

BinaryReader reads primitive data types as binary values in a specific encoding, is that what your server sends?
If not use StreamReader:

TcpClient _tcpClient = new TcpClient("host", 110);

StreamReader reader = 
   new StreamReader(new System.Net.Security.SslStream(_tcpClient.GetStream(), true));

Console.WriteLine(reader.ReadToEnd());
like image 120
Ovidiu Pacurar Avatar answered Oct 17 '22 08:10

Ovidiu Pacurar


I'm not entirely sure if this will work for your application but I would recommend taking a look at stunnel:
http://www.stunnel.org

I've used it for wrapping existing TCP connections in the past.

like image 41
toddk Avatar answered Oct 17 '22 08:10

toddk