gpt4 book ai didi

c# - 每个套接字地址(协议(protocol)/网络地址/端口)通常只允许使用一次

转载 作者:行者123 更新时间:2023-12-03 12:06:55 28 4
gpt4 key购买 nike

我正在做一个使用套接字编程的项目,但是当多次使用同一个套接字时,即使在关闭 listener() 之后也是如此。每次我使用套接字时:当我运行应用程序时,它会给出错误:

Only one usage of each socket address (protocol/network address/port) is normally permitted



我真的很担心。有人可以帮帮我吗?

这是我正在使用的代码,我正在正确关闭所有连接
        TcpClient tcpClient;
TcpListener tcpListener;
byte[] ipaddress = new byte[4];
string ip = ConfigurationManager.AppSettings["IP"].ToString();
string[] ips = ip.Split('.');
ipaddress[0] = (byte)Convert.ToInt32(ips[0]);
ipaddress[1] = (byte)Convert.ToInt32(ips[1]);
ipaddress[2] = (byte)Convert.ToInt32(ips[2]);
ipaddress[3] = (byte)Convert.ToInt32(ips[3]);
int portNumber = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
tcpListener = new TcpListener(new IPAddress(ipaddress), portNumber);
tcpListener.Start();
tcpClient = new TcpClient();
tcpClient.NoDelay = true;
try
{
tcpClient.ReceiveTimeout = 1;
tcpClient = tcpListener.AcceptTcpClient();
}
catch (Exception ex)
{
tcpClient.Close();
tcpListener.Stop();
}
NetworkStream networkStream = tcpClient.GetStream();
byte[] bytes = new byte[networkStream.Length];
try
{
networkStream.ReadTimeout = 2000;
networkStream.Read(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
tcpClient.Close();
tcpListener.Stop();
}
string returndata = Encoding.Default.GetString(bytes);
tcpClient.Close();
tcpListener.Stop();

return returndata;

最佳答案

您确定要选择一个独特的端口吗?

您是否尝试过只使用该端口一次?

在重新打开套接字之前,请确保您真正处理了连接对象等。没有更多的实现细节很难知道。

关于c# - 每个套接字地址(协议(protocol)/网络地址/端口)通常只允许使用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2245838/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com