我想从此代码创建Windows服务。任何人都可以帮助创建Windows服务。我自己尝试了很多次,但我成功了50%。在我的代码中,我想在与客户端应用程序连接后执行3个功能。
public static class Program
{
public static TcpClient client;
private static TcpListener listener;
private static string ipString;
static void Main(string[] args)
{
IPAddress[] localIp = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress address in localIp)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
{
ipString = address.ToString();
}
}
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipString), 1234);
listener = new TcpListener(ep);
listener.Start();
client = listener.AcceptTcpClient();
while (client.Connected)
{
try
{
const int bytesize = 1024 * 1024;
byte[] buffer = new byte[bytesize];
string x = client.GetStream().Read(buffer, 0, bytesize).ToString();
var data = ASCIIEncoding.ASCII.GetString(buffer);
if (data.ToUpper().Contains("SLP2"))
{
Sleep();
}
else if (data.ToUpper().Contains("SHTD3"))
{
Shutdown();
}
else if (data.ToUpper().Contains("TSC1"))
{
var bitmap = SaveScreenshot();
var stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Bmp);
sendData(stream.ToArray(), client.GetStream());
}
}
catch (Exception exc)
{
client.Dispose();
client.Close();
}
}
}
}