Home » Community » Newbie corner » my TCP client/server don't work correctly
Re: my TCP client/server don't work correctly [message #57048 is a reply to message #57046] |
Mon, 17 May 2021 21:10   |
Oblivion
Messages: 1214 Registered: August 2007
|
Senior Contributor |
|
|
Hello Xemuth,
Your server code accepts only a single connection and then exits. You need to loop. 
SocketServer example:
for(;;) { <<----------
TcpSocket s;
if(s.Accept(server)) {
String w = s.GetLine();
Cout() << "Request: " << w << " from: " << s.GetPeerAddr() << '\n';
if(w == "time")
s.Put(AsString(GetSysTime()));
else
s.Put(AsString(3 * atoi(~w)));
s.Put("\n");
}
}
It you need to handle multiple incoming connections at the same time, you either fork (expensive) or use an array of nonblocking sockets and/or MT, once accepting them, loop over them asynchronously until the connection is closed.
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Mon, 17 May 2021 22:40] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Tue Jul 15 14:17:20 CEST 2025
Total time taken to generate the page: 0.03398 seconds
|