| |

VerySource

 Forgot password?
 Register
Search
View: 732|Reply: 3

tcp communication, why is there no response when AcceptTcpClient () is executed?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-16 09:20:02
| Show all posts |Read mode
I just learned TCP communication, but the following code is always not responding. Why?
TcpListener tcplistener = new TcpListener (IPAddress.Parse ("127.0.0.1"), 13);
tcplistener.Start ();
byte [] btServerReceive = new byte [256];
string strServerReceive = string.Empty;

while (true)
{
    TcpClient tcp = tcplistener.AcceptTcpClient ();
    NetworkStream ns = tcp.GetStream ();
    int intReceiveLength = ns.Read (btServerReceive, 0, btServerReceive.Length);
    strServerReceive = UnicodeEncoding.Unicode.GetString (btServerReceive, 0, intReceiveLength);
    txtServerResult.AppendText ("ServerReceive:" + strServerReceive + "");
    ns.Write (btServerReceive, 0, btServerReceive.Length);
    txtServerResult.AppendText ("ServerSend:" + strServerReceive + "");
    tcp.Close ();
}
Reply

Use magic Report

0

Threads

14

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-10 12:15:02
| Show all posts
It is recommended to check MSDN first

AcceptTcpClient () is blocked until a client executes Connect. It is normal for you to execute this piece of code until AcceptTcpClient () does not respond.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-20 00:15:01
| Show all posts
There is no response until the client sends a message, so it is best to put it in a thread
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-25 19:45:01
| Show all posts
Look, I don’t think I understand what code you are
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list