简体   繁体   English

非连接套接字上不允许操作 - C#4.0

[英]Operation not allowed on non-connected Sockets - C# 4.0

It keeps having an error "Operation not allowed on non-connected sockets" on the line 它一直出现错误“在非连接套接字上不允许操作”

var ServerStream = Connect2Server.GetStream();

And I'm not really sure why 而且我不确定为什么

Below is rest of the code for that 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);

You gotta Connect() before you can get the NetworkStream. 你需要Connect()才能获得NetworkStream。

The documentation is usually pretty good for this kinda stuff. 文档通常对这种东西很好。 Under Exceptions in the help for GetStream , you'll see: GetStream帮助中的例外情况下,您将看到:

InvalidOperationException: The TcpClient is not connected to a remote host. InvalidOperationException:TcpClient未连接到远程主机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM