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 » U++ Library support » U++ MT-multithreading and servers » Socket client out of buffer error and reusing sockets
Socket client out of buffer error and reusing sockets [message #40199] Tue, 02 July 2013 11:03 Go to next message
koldo is currently offline  koldo
Messages: 3372
Registered: August 2008
Senior Veteran
Hello all

I am a completely newbie in sockets.

When doing a client based in SocketClient demo and running it in a for() loop a WSAENOBUFS is get:

String Request(const String& r)
{
	TcpSocket s;
	if(!s.Connect(CommandLine().GetCount() ? CommandLine()[0] : "127.0.0.1", 3214)) {
		Cout() << "Unable to connect to server!\n";
		SetExitCode(1);
		return Null;
	}
	s.Put(r + '\n');
	return s.GetLine();
}

CONSOLE_APP_MAIN
{
	for (int i = 0; i < 50000; ++i)
		Cout() << Request("time") << '\n';
}


However if TcpSocket is reused, the program runs perfectly:

String Request(TcpSocket &s, const String& r)		// Added TcpSocket &s
{
	if(!s.Connect(CommandLine().GetCount() ? CommandLine()[0] : "127.0.0.1", 3214)) {
		Cout() << "Unable to connect to server!\n";
		SetExitCode(1);
		return Null;
	}
	s.Put(r + '\n');
	return s.GetLine();
}

CONSOLE_APP_MAIN
{
	TcpSocket s;						// Added TcpSocket s;
	for (int i = 0; i < 50000; ++i)
		Cout() << Request(s, "time") << '\n';		// Added s
}


Is the conclusion that TcpSocket has to be reused, or TcpSocket is not properly destructed, or maybe both?


Best regards
Iñaki
Re: Socket client out of buffer error and reusing sockets [message #40200 is a reply to message #40199] Tue, 02 July 2013 18:55 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
koldo wrote on Tue, 02 July 2013 02:03

Is the conclusion that TcpSocket has to be reused,



I think that is your answer. Each transmission from the client to the server or the response is a different packet.

I have a buffer set at 3000 and seldom have over run that.
One important thing is end the packet with \0.
Then only the data up to and including the \0 is included in the packet. Your packets can be several sentences including \n as that is just another character and has no significance to the packet but the data can be parsed at the \n.

The same socket is reused.

edit: GetLine() is a misnomer just think of that as GetData().
'String' has \0 appended and can include several \n.
If you want to add several 'String' or 'lines' to send through a socket just remove all the \0 and then append that at the end of the data.

[Updated on: Tue, 02 July 2013 19:27]

Report message to a moderator

Re: Socket client out of buffer error and reusing sockets [message #40207 is a reply to message #40200] Wed, 03 July 2013 08:36 Go to previous message
koldo is currently offline  koldo
Messages: 3372
Registered: August 2008
Senior Veteran
Thank you!

Best regards
Iñaki
Previous Topic: Using Raw Values in arrays (with Skylark)
Next Topic: SSL sockets example
Goto Forum:
  


Current Time: Sun Jun 16 21:08:40 CEST 2024

Total time taken to generate the page: 0.02275 seconds