1
任何人都可以帮助我在C#.net WindowsService中记录端口活动。还有以下代码的任何问题。套接字编程对我来说是全新的。我甚至不能记录它。C#.net Windows服务 - 如何在fil中记录TCP端口活动
public void Start()
{
try
{
// Read server IPAddress and port details from the config
string serverIP, serverPort;
serverIP = ConfigurationManager.AppSettings["ServerIP"];
serverPort = ConfigurationManager.AppSettings["ServerPort"];
// Initialize server IPAddress and port number
IPAddress serverIPAddress = IPAddress.Parse(serverIP);
int serverPortNumber = int.Parse(serverPort);
this.tcpListnr = new TcpListener(serverIPAddress, serverPortNumber);
this.tcpListnr.Start();
// Start listener thread
this.tcpListnrThread = new Thread(this.ThreadProc);
this.tcpListnrThread.Start();
}
catch (Exception ex)
{
if (Er != null)
this.Er.LogError(ex,"Error in Start ");
}
}
private void ThreadProc()
{
try
{
// Listen infinitly
while (true)
{
// Waiting for a connection
TcpClient tcpClient = this.tcpListnr.AcceptTcpClient();
// Connected
if (this.tcpListnrThread.IsAlive)
{
try
{
Thread readDataThread = new Thread(this.ReadDataThreadProc);// Read data in a separate thread
readDataThread.Start(tcpClient);
}
catch(Exception ex)
{
this.Er.LogError(ex,"Thread Abort");
}
}
}
}
catch (Exception ex)
{
if (Er != null)
this.Er.LogError(ex,"Error in ThreadProc - ");
}
}
我是StackOverflow的新手,请原谅我的问题。