Reputation: 109
I'm facing a strange error. Following code is always throwing exception (System.ArgumentException "Stream was not writable
"):
TcpClient socket = new TcpClient();
socket.Connect("serverIp", serverPort);
//wrap networkstream with ssl
SslStream sslStream = new SslStream(socket.GetStream(),false);
stream.ReadTimeout = 5000;
stream.WriteTimeout = 5000;
//following line throws System.ArgumentException "Stream was not writable"
var writer = new BinaryWriter(sslStream);
writer.Write(abMessage, nStart, nLength);
writer.Flush();
'sslStream
' object canWrite
and canRead
properties are always 'false
'.
Obviously, I have Googled, and searched in many forums, but, I always found that same code which is not working for me.
How can I fix this exception?
Upvotes: 2
Views: 1426
Reputation: 642
Once you create a SslStream
(on top of a TcpClient
) you should handshake the ssl connection. For this, you will use some of the methods available in the SslStream
type:
AuthenticateAsClient
(several overloads)AuthenticateAsServer
(several overloads)(or they async variants BeginAuthenticateAs...
)
Without using this handshaking methods the SslStream
is not really setup and you won't be able to read or write from/to the network stream.
Which methods to use (AuthenticateAsClient
or AuthenticateAsServer
) is specific to your implementation and the desired usage of the SSL Stream.
Upvotes: 4