开发者

How to Solve a form loading error of Client-Server

开发者 https://www.devze.com 2023-02-21 06:11 出处:网络
TcpListenerserverSocket = new TcpListener(ip, port); TcpClientclientSocket =new TcpClient();//default tcpclient socket;
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());


    //G开发者_如何学Pythonet the client ip address

    string clientIPAddress = "Your Ip Address is: " + IPAddress.Parse(((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString()));

    NetworkStream stream = client.GetStream();

    ..
    ... 
}       

In Windows Form Application here ,I have the problem of why form did not load when I used the

clientSocket = serverSocket.AcceptTcpClient(); 

and rest code

but it can accept the client request; Now how to solve this problem and also plz give me information about the Timer uses


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.
    }
}

That would work. However, I do not recommend it since it breaks the Single Responsibility Principle (google). Put all socket code in a separate class (including the thread code) and use that class from your main form.

0

精彩评论

暂无评论...
验证码 换一张
取 消