Operation not allowed for unbound sockets - C # 4.0

It continues to have the error "Operation not allowed for unconnected sockets" on the line

var ServerStream = Connect2Server.GetStream(); 

And I'm not quite sure why

Below is the rest of the code for this function

 var buffersize = 0; var Convert2Tet = new ASCIIEncoding(); var Connect2Server = new TcpClient(); var ServerEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8801); var ServerStream = Connect2Server.GetStream(); Console.WriteLine("Connecting to Server"); Connect2Server.Connect(ServerEndPoint); var WelcomeMessage = new byte[4096]; ServerStream.Read(WelcomeMessage, 0, 4096); Console.Write(Convert2Tet.GetChars(WelcomeMessage)); var UserCredentials = Console.ReadLine(); buffersize = Convert2Tet.GetByteCount(UserCredentials); var Credentials = new byte[buffersize]; Credentials = Convert2Tet.GetBytes(UserCredentials); ServerStream.Write(Credentials, 0, buffersize); 
+4
source share
1 answer

You need to connect () before you can get NetworkStream.

The documentation is usually good for this kind. In the Exclusions section of GetStream help, you'll see:

InvalidOperationException: TcpClient is not connected to the remote host.

+9
source

All Articles