尝试与 TcpClient 连接时出现 SocketException
SocketException when attempting to connect with TcpClient
当我尝试创建一个新的 TcpClient
时,我得到一个 SocketException
,这是我的代码:
public void TcpOpenConnection()
{
// The next line is where the exception is occurring.
tcpClient = new TcpClient(ipAddress, port);
connected = true;
}
我在cmd中用netstat -a
检查了端口是否打开,我什至做了另一个函数来检查端口是否打开:
public static bool PortCheck(int port)
{
bool portOpen = false;
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpConnInfo = ipGlobalProperties.GetActiveTcpConnections();
foreach (var tcpConn in tcpConnInfo)
{
if (tcpConn.LocalEndPoint.Port == port)
{
portOpen = true;
break;
}
}
return portOpen;
}
其中 returns 正确。我得到的异常是 SocketException
,它表示我尝试连接的机器正在主动拒绝连接。这里可能是什么问题?我也尝试过其他端口,但没有成功。
如果您需要更多信息,请询问,我很乐意提供更多。
The exception that I am getting is a SocketException
and it is saying the machine I am trying to connect to is actively refusing the connection.
这可能表明目标主机未在端口上侦听,这可能是由多种原因引起的:
- 服务器网络的路由器未正确进行端口转发
- 路由器的防火墙/服务器的防火墙阻止了连接
- 服务器和客户端使用的端口不同
- 服务器配置错误
列表还在继续......但本质上,这个错误意味着服务器不允许连接。
如果端口打开并且您尝试连接。你得到 SocketException 因为没有任何东西可以获取客户端连接。
因此您需要在此端口上托管一个 Tcplistner。
static void StartServer()
{
int port = 150;
TcpListener listner = new TcpListener(IPAddress.Any, port);
listner.Start();
// This line waits the client connection.
TcpClient remote_client = listner.AcceptTcpClient();
// do something with remote_client.
}
您可以连接到。
static void StartClient()
{
int port = 150;
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpClient client = new TcpClient();
client.Connect(ip, port);
// Do something with client.
}
当我尝试创建一个新的 TcpClient
时,我得到一个 SocketException
,这是我的代码:
public void TcpOpenConnection()
{
// The next line is where the exception is occurring.
tcpClient = new TcpClient(ipAddress, port);
connected = true;
}
我在cmd中用netstat -a
检查了端口是否打开,我什至做了另一个函数来检查端口是否打开:
public static bool PortCheck(int port)
{
bool portOpen = false;
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpConnInfo = ipGlobalProperties.GetActiveTcpConnections();
foreach (var tcpConn in tcpConnInfo)
{
if (tcpConn.LocalEndPoint.Port == port)
{
portOpen = true;
break;
}
}
return portOpen;
}
其中 returns 正确。我得到的异常是 SocketException
,它表示我尝试连接的机器正在主动拒绝连接。这里可能是什么问题?我也尝试过其他端口,但没有成功。
如果您需要更多信息,请询问,我很乐意提供更多。
The exception that I am getting is a
SocketException
and it is saying the machine I am trying to connect to is actively refusing the connection.
这可能表明目标主机未在端口上侦听,这可能是由多种原因引起的:
- 服务器网络的路由器未正确进行端口转发
- 路由器的防火墙/服务器的防火墙阻止了连接
- 服务器和客户端使用的端口不同
- 服务器配置错误
列表还在继续......但本质上,这个错误意味着服务器不允许连接。
如果端口打开并且您尝试连接。你得到 SocketException 因为没有任何东西可以获取客户端连接。
因此您需要在此端口上托管一个 Tcplistner。
static void StartServer()
{
int port = 150;
TcpListener listner = new TcpListener(IPAddress.Any, port);
listner.Start();
// This line waits the client connection.
TcpClient remote_client = listner.AcceptTcpClient();
// do something with remote_client.
}
您可以连接到。
static void StartClient()
{
int port = 150;
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpClient client = new TcpClient();
client.Connect(ip, port);
// Do something with client.
}