不允许在未连接的套接字上进行操作 - C# 4.0

发布于 2024-10-01 22:48:53 字数 846 浏览 3 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天冷不及心凉 2024-10-08 22:48:53

在获得 NetworkStream 之前,您必须先 Connect()。

对于这类东西,文档通常非常好。在 GetStream,你会看到:

InvalidOperationException:
TcpClient 未连接到远程
主机。

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:

InvalidOperationException: The
TcpClient is not connected to a remote
host.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文