w我在linux上运行完全没有问题的.netSocket连接程序,始终是无法分配连接Cannot assign requested address
但是在一台1h0.5g的超垃圾服务器上竟然运行成功了,我把我的服务器重装系统,保证除了.NET的sdk下没有运行其他任何程序(宝塔都没下),但依然错误。
网上搜索的结果几乎都是说要释放端口,可我的端口已经空得不能再空了。
我枚举从32767端口试到60000,没一个成功(安全组确认已开放)
请问下需要怎么做?
服务端配置:centos7.8,.net6.0
程序配置:':Visual Studio C# 控制台,.netSocket
服务端代码:
Console.WriteLine("[服务器]启动");
IPAddress ip = IPAddress.Parse("我写得服务器外网地址");
IPEndPoint point = new IPEndPoint(ip, int.Parse(32790));
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Dictionary<string, Socket> dic = new Dictionary<string, Socket>();
try
{
socket.Bind(point);
socket.Listen(10);
Thread thread = new Thread(AcceptInfo);
thread.IsBackground = false;
thread.Start(socket);
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
void AcceptInfo(object o)
{
Socket socket = o as Socket;
ShowMsg("开始检测!");
while (true)
{
try
{
Socket tSocket = socket.Accept();
string point = tSocket.RemoteEndPoint.ToString();
ShowMsg(point + "连接成功!");
dic.Add(point, tSocket);
Send(point, point);
Thread th = new Thread(ReceiveMsg);
th.IsBackground = true;
th.Start(tSocket);
}
catch (Exception ex) { break; }
}
}
void ShowMsg(string msg)
{
Console.WriteLine(msg);//这是对服务器情况汇报的方式
}
void ReceiveMsg(object o)
{
Socket client = o as Socket;
while (true)
{
try
{
byte[] buffer = new byte[1024 * 1024];
int n = client.Receive(buffer);
string words = Encoding.UTF8.GetString(buffer, 0, n);
ShowMsg(client.RemoteEndPoint.ToString() + " : " + words);
}
catch (Exception ex) { break; }
}
}
void Send(string ip, string str)//发送消息直接调用这个函数,方法:Send("IP:端口",内容);
{
try
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
dic[ip].Send(buffer);
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
}