Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to previous messageGo to previous message
Oblivion is currently offline  Oblivion
Messages: 1094
Registered: August 2007
Senior Contributor
Hello Xemuth,

Your server code accepts only a single connection and then exits. You need to loop. Smile

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


[Updated on: Mon, 17 May 2021 22:40]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Stopping ReadStdIn() function
Next Topic: How to configure for SDL2 project?
Goto Forum:
  


Current Time: Wed May 15 10:03:23 CEST 2024

Total time taken to generate the page: 0.02161 seconds