我正在开发一个应该使用此类收集所有传入TCP / IP流的Windows服务。它从服务OnStart()void开始。
但是我遇到过,当消息来自第二个发件人时,整个通信都会停止工作。然后,在重新启动之前,该服务不会对单个连接作出反应。
public class TCPIP
{
public static Receiver rc = new Receiver();
public class Receiver
{
public delegate void ReceivedEventHandler(string ReceivedText, string Sender);
public event ReceivedEventHandler Received;
public void Start()
{
System.Threading.Thread th = new System.Threading.Thread(internals);
th.Start();
}
private void internals()
{
TcpListener _listener = new TcpListener(1994);
_listener.Start();
while (true)
{
TcpClient rcclient = _listener.AcceptTcpClient();
StreamReader reader = new StreamReader(rcclient.GetStream());
string msg = "";
while (reader.Peek() > -1)
{
msg += Convert.ToChar(reader.Read()).ToString();
}
Received(msg, rcclient.Client.RemoteEndPoint.ToString().Split(Convert.ToChar(":"))[0]);
// Cleanup
rcclient.Close();
reader.Close();
}
}
有人可以帮助我改进这门课程来回答来自多个端点的连接,并且不会在一个之后被占用吗?
非常感谢你。