简体   繁体   中英

C#.net socket programming issue

I'm learning socket programming and whenever I start my server, it gets frozen.

Here is my code:

        IPAddress ipAdd = IPAddress.Parse(textBox1.Text);
        TcpListener tcpListen = new TcpListener(ipAdd, Convert.ToInt32( textBox2.Text));
        tcpListen.Start();

        Socket s = tcpListen.AcceptSocket();

        //Get the data
        byte[] b = new byte[100];
        int k = s.Receive(b);
        richTextBox1.AppendText(k.ToString());
        listiplbl.Text = textBox1.Text;
        listportlbl.Text = textBox2.Text;
        srvrstatuslbl.Text = "Running";
        actconslbl.Text = "1";

Also, I'm not quite sure if with this code I'm able to connect multiple clients. My program is simple. I'm planning on installing a client on each PC and it supposed to send me the computer's name and its ip. And if I query an ip through my server, the client should reply with the same details also. Please use simple terms or explanations. It is the first time for me to use sockets and I'm trying to understand.

Winsocket's can either be created as blocking or non blocking sockets. The reason why your server is blocking is because you're using a blocking socket. To unblock it, just send some data to it from a client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM