У меня TCP сервер в C#. Но проблема в том, что я хочу, чтобы он продолжал получать сообщения. И когда соединение разрывается, он должен дождаться нового подключения, а затем принять его.
Вот что у меня есть на данный момент:
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();
}
}