客户端断开连接后,如何让 TCP 服务器等待新的客户端连接?
How to make a TCP Server wait for a new client connection after a client disconnects?
我在 C# 中有一个 TCP 服务器。但问题是我希望它继续接收消息。当连接断开时,它应该等待一个新的连接然后接受它。
这是我目前的情况:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
public class SimpleTcpSrvr
{
public static void Main()
{
int recv;
byte[] data = new byte[1024];
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 5001);
Socket SOC = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SOC.Bind(remoteEP);
SOC.Listen(25);
Console.WriteLine("GETTING THE ENGINE WARMMED UP. . .");
Socket client = SOC.Accept();
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("REACHING THE FAR LIMITS OF THE ENGINE!!!");
data = new byte[1024];
try
{
while (true)
{
recv = client.Receive(data);
string RECEIVED_COMMAND = Encoding.ASCII.GetString(data, 0, recv);
if (RECEIVED_COMMAND == "CONNECT")
{
string TIME = DateTime.Now.ToString("h:mm:ss tt");
string TARGET_HOST_NAME = Dns.GetHostName();
string TARGET_IP = Dns.GetHostByName(TARGET_HOST_NAME).AddressList[0].ToString();
data = Encoding.ASCII.GetBytes(string.Join(Environment.NewLine, " " + "YOU HAVE SUCCESSFULLY CONNECTED", " TO " + TARGET_IP + " AT " + TIME));
client.Send(data, data.Length, SocketFlags.None);
}
else if (RECEIVED_COMMAND == "GET_INFORMATION")
{
OperatingSystem OS_INFO = System.Environment.OSVersion;
string USERNAME = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string CHROME = null;
if (Directory.Exists(@"C:\Program Files (x86)\Google\Chrome"))
{
CHROME = "GOOGLE CHROME IS AVAILABLE.";
}
else if (!Directory.Exists(@"C:\Program Files (x86)\Google\Chrome"))
{
CHROME = "GOOGLE CHROME IS UNAVAILABLE.";
}
data = Encoding.ASCII.GetBytes(string.Join(Environment.NewLine, " " + OS_INFO.VersionString, " " + OS_INFO.Platform, " " + USERNAME, " " + CHROME));
client.Send(data, data.Length, SocketFlags.None);
}
}
}
catch (System.Net.Sockets.SocketException)
{
}
Console.WriteLine("THE ENGINE HAS SUCCESSFULLY REACHED ITS LIMITS, AND NOW ON ITS FULL POWER ;)");
client.Close();
SOC.Close();
Console.ReadLine();
}
}
这可能与 TCP server with multiple Clients
重复
为了能够接受多个客户端(或通信会话),您必须生成线程并在完成后关闭它们,或者提供一些类似的机制来侦听传入请求并满足它们。
我在 C# 中有一个 TCP 服务器。但问题是我希望它继续接收消息。当连接断开时,它应该等待一个新的连接然后接受它。
这是我目前的情况:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
public class SimpleTcpSrvr
{
public static void Main()
{
int recv;
byte[] data = new byte[1024];
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 5001);
Socket SOC = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SOC.Bind(remoteEP);
SOC.Listen(25);
Console.WriteLine("GETTING THE ENGINE WARMMED UP. . .");
Socket client = SOC.Accept();
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("REACHING THE FAR LIMITS OF THE ENGINE!!!");
data = new byte[1024];
try
{
while (true)
{
recv = client.Receive(data);
string RECEIVED_COMMAND = Encoding.ASCII.GetString(data, 0, recv);
if (RECEIVED_COMMAND == "CONNECT")
{
string TIME = DateTime.Now.ToString("h:mm:ss tt");
string TARGET_HOST_NAME = Dns.GetHostName();
string TARGET_IP = Dns.GetHostByName(TARGET_HOST_NAME).AddressList[0].ToString();
data = Encoding.ASCII.GetBytes(string.Join(Environment.NewLine, " " + "YOU HAVE SUCCESSFULLY CONNECTED", " TO " + TARGET_IP + " AT " + TIME));
client.Send(data, data.Length, SocketFlags.None);
}
else if (RECEIVED_COMMAND == "GET_INFORMATION")
{
OperatingSystem OS_INFO = System.Environment.OSVersion;
string USERNAME = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string CHROME = null;
if (Directory.Exists(@"C:\Program Files (x86)\Google\Chrome"))
{
CHROME = "GOOGLE CHROME IS AVAILABLE.";
}
else if (!Directory.Exists(@"C:\Program Files (x86)\Google\Chrome"))
{
CHROME = "GOOGLE CHROME IS UNAVAILABLE.";
}
data = Encoding.ASCII.GetBytes(string.Join(Environment.NewLine, " " + OS_INFO.VersionString, " " + OS_INFO.Platform, " " + USERNAME, " " + CHROME));
client.Send(data, data.Length, SocketFlags.None);
}
}
}
catch (System.Net.Sockets.SocketException)
{
}
Console.WriteLine("THE ENGINE HAS SUCCESSFULLY REACHED ITS LIMITS, AND NOW ON ITS FULL POWER ;)");
client.Close();
SOC.Close();
Console.ReadLine();
}
}
这可能与 TCP server with multiple Clients
重复为了能够接受多个客户端(或通信会话),您必须生成线程并在完成后关闭它们,或者提供一些类似的机制来侦听传入请求并满足它们。