我有一个典型的服务器和一个使用客户端连接到我的IP /端口的朋友,他一直收到异常:“连接尝试失败,因为连接方在一段时间后没有正确响应,或已建立的连接失败,因为已连接的主机无法响应{MY_IP}:{MY_PORT}“ - 您无需知道我的IP。
但是,客户端和服务器在环回地址(127.0.0.1)上运行良好。我也没有任何防火墙,也没有Windows防火墙处于活动状态。
服务器:
static void Main(string[] args)
{
Console.Title = "Socket Server";
Console.WriteLine("Listening for messages...");
Socket serverSock = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Any;
IPEndPoint serverEP = new IPEndPoint(serverIP, 33367);
SocketPermission perm = new SocketPermission(NetworkAccess.Accept, TransportType.Tcp, "98.112.235.18", 33367);
serverSock.Bind(serverEP);
serverSock.Listen(10);
while (true)
{
Socket connection = serverSock.Accept();
Byte[] serverBuffer = new Byte[8];
String message = String.Empty;
while (connection.Available > 0)
{
int bytes = connection.Receive(
serverBuffer,
serverBuffer.Length,
0);
message += Encoding.UTF8.GetString(
serverBuffer,
0,
bytes);
}
Console.WriteLine(message);
connection.Close();
}
}
客户端:
static void Main(string[] args)
{
// Design the client a bit
Console.Title = "Socket Client";
Console.Write("Enter the IP of the server: ");
IPAddress clientIP = IPAddress.Parse(Console.ReadLine());
String message = String.Empty;
while (true)
{
Console.Write("Enter the message to send: ");
// The messsage to send
message = Console.ReadLine();
IPEndPoint clientEP = new IPEndPoint(clientIP, 33367);
// Setup the socket
Socket clientSock = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
// Attempt to establish a connection to the server
Console.Write("Establishing connection to the server... ");
try
{
clientSock.Connect(clientEP);
// Send the message
clientSock.Send(Encoding.UTF8.GetBytes(message));
clientSock.Shutdown(SocketShutdown.Both);
clientSock.Close();
Console.Write("Message sent successfully.\n\n");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
答案 0 :(得分:0)
在客户端看起来这是一个错误的防火墙。
答案 1 :(得分:0)
while (connection.Available > 0)
{
int bytes = connection.Receive(
serverBuffer,
serverBuffer.Length,
0);
message += Encoding.UTF8.GetString(
serverBuffer,
0,
bytes);
}
我不知道原因(可能是时间)但可用连接数始终为0.因此服务器不会收到客户端发送的消息。删除while循环,您的程序将正常运行。我尝试了它并且它有效。
答案 2 :(得分:0)
您需要添加权限以允许套接字使用Assert()方法在特定端点进行连接。以下服务器代码可能有效,
static void Main(string[] args)
{
Console.Title = "Socket Server";
Console.WriteLine("Listening for messages...");
Socket serverSock = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Any;
IPEndPoint serverEP = new IPEndPoint(serverIP, 33367);
SocketPermission perm = new SocketPermission(NetworkAccess.Accept, TransportType.Tcp, "98.112.235.18", 33367);
perm.Assert();
serverSock.Bind(serverEP);
serverSock.Listen(10);
while (true)
{
Socket connection = serverSock.Accept();
Byte[] serverBuffer = new Byte[8];
String message = String.Empty;
while (connection.Available > 0)
{
int bytes = connection.Receive(
serverBuffer,
serverBuffer.Length,
0);
message += Encoding.UTF8.GetString(
serverBuffer,
0,
bytes);
}
Console.WriteLine(message);
connection.Close();
}
}