TcpListener serverSocket = new TcpListener(ip, port);
TcpClient clientSocket =new TcpClient(); //default tcpclient socket;
serverSocket.Start();
counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
LAN_Quiz.ClientHandle.HandleClient hc = new LAN_Quiz.ClientHandle.HandleClient();
hc.ClientHandlingStarted(clientSocket, counter.ToString());
//Get the client ip address
string clientIPAddress = "Your Ip Address is: " + IPAddress.Parse(((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString()));
NetworkStream stream = client.GetStream();
..
...
}
在Windows窗体应用程序中,我遇到了在使用
时无法加载窗体的问题clientSocket = serverSocket.AcceptTcpClient();
和休息代码
但它可以接受客户请求; 现在如何解决这个问题,并且plz给我提供有关Timer使用的信息
答案 0 :(得分:0)
public partial class MyForm : Form
{
private Thread _serverThread;
public Form()
{
_serverThread = new Thread(ServerThread).Start();
}
private void ServerThread(object state)
{
//add the socket code here.
}
}
那会有用。但是,我不推荐它,因为它打破了单一责任原则(谷歌)。将所有套接字代码放在一个单独的类(包括线程代码)中,并使用主窗体中的该类。