不允许在未连接的套接字上进行操作 - C# 4.0
它一直有错误“在非连接的套接字上不允许操作”
var ServerStream = Connect2Server.GetStream();
,我不太确定为什么
下面是该函数的其余代码
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在获得 NetworkStream 之前,您必须先 Connect()。
对于这类东西,文档通常非常好。在 GetStream,你会看到:
You gotta Connect() before you can get the NetworkStream.
The documentation is usually pretty good for this kinda stuff. Under Exceptions in the help for GetStream, you'll see: