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 » Problem using TcpSockets
Problem using TcpSockets [message #41919] Wed, 05 February 2014 21:02 Go to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
Hello!

I have been playing successfully with sockets based in SocketServer ans SocketClient demos.

However I have had problems with a simple program that sends plain text messages ended with a '\n with:

char *buf = "Message\n";
send(socket, buf, strlen(buf), 0);


Looking to SocketServer demo, it seems that s.Accept(server) blocks the program the second time. I mean:
- 1st s.Accept(server) passes just when demo program begins to send messages
- s.GetLine() works properly
- 2st s.Accept(server) blocks the program

However, if s.GetLine() is called in a loop, it takes all the messages properly.

Is there any kind of protocol embedded in TcpSocket used the way is used in SocketServer/Client demos, that does not match with plain send() calls?



Best regards
Iñaki
Re: Problem using TcpSockets [message #42011 is a reply to message #41919] Mon, 10 February 2014 20:16 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13252
Registered: November 2005
Ultimate Member
koldo wrote on Wed, 05 February 2014 15:02

Hello!

I have been playing successfully with sockets based in SocketServer ans SocketClient demos.

However I have had problems with a simple program that sends plain text messages ended with a '\n with:

char *buf = "Message\n";
send(socket, buf, strlen(buf), 0);


Looking to SocketServer demo, it seems that s.Accept(server) blocks the program the second time. I mean:
- 1st s.Accept(server) passes just when demo program begins to send messages
- s.GetLine() works properly
- 2st s.Accept(server) blocks the program

However, if s.GetLine() is called in a loop, it takes all the messages properly.

Is there any kind of protocol embedded in TcpSocket used the way is used in SocketServer/Client demos, that does not match with plain send() calls?




Just to be sure: Do you open a new connection to the server for second 'send'? (the whole testcase would be useful).

Mirek
Re: Problem using TcpSockets [message #42014 is a reply to message #42011] Tue, 11 February 2014 02:15 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
A testcase would be useful to see the problem.

"- 2st s.Accept(server) blocks the program"
I am curious how you do that.

Whenever the last return is called:
	return "y";
}
#endif

the connection is closed and then a new connection can be made.

If you are in a GetLine() loop then the connection is not broken so a new
	if(!s.Connect("127.0.0.1", 11811)) {
with the same address ("127.0.0.1" or whatever) will fail with an error saying that address is in use.

Servers are often set up to accept multiple connections so a different address could be opened. But a basic client with just one address can have only one connection for that address and cannot be opened more than once at the same time.

My thinking may be wrong but I think that is correct.
Re: Problem using TcpSockets [message #42016 is a reply to message #42014] Tue, 11 February 2014 09:14 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
Hello guys!

Lets put an example:

- In one side there is a client that just does socket "send()" sending strings ended with '\n'.

- In the other side, there is a server based on SocketServer demo.

This is similar to SocketServer, and just shows the first line Sad :
	for(;;) {
		TcpSocket s;
		if(s.Accept(server)) {
			Cout() << "Request from: " << s.GetPeerAddr() << '\n';
			Cout() << "Received: " << s.GetLine() << "\n";
		}
	}

This shows all the lines (although it does not stop the loop Rolling Eyes );
	for(;;) {
		TcpSocket s;
		if(s.Accept(server)) {
			Cout() << "Request from: " << s.GetPeerAddr() << '\n';
			while(true) 
				Cout() << "Received: " << s.GetLine() << "\n";
		}
	}





Best regards
Iñaki
Re: Problem using TcpSockets [message #42018 is a reply to message #42016] Tue, 11 February 2014 09:45 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13252
Registered: November 2005
Ultimate Member
If that is the case (first code not working, second does), then the problem is that you do not close 'send' socket.

I mean, the first example would only work if you close the connection, then establish a new one for next line.

Mirek
Re: Problem using TcpSockets [message #42028 is a reply to message #42018] Wed, 12 February 2014 10:13 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
I understand. This happened to me with an external program I wanted to connect to.

In addition, I have to do a socket client in plain C that I wanted to connect to an U++ server. Would I have to close the connection every time I send a data stream?


Best regards
Iñaki

[Updated on: Wed, 12 February 2014 10:13]

Report message to a moderator

Re: Problem using TcpSockets [message #42029 is a reply to message #42028] Wed, 12 February 2014 10:50 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13252
Registered: November 2005
Ultimate Member
Actually, I think you get the whole socket design wrong. I suggest some reading on 'accept'.

Anyway, if client sends a multitude of text in separate 'send's, then yours second source is right. You can replace '(true)' with something else like (!socket.IsEof()).
Re: Problem using TcpSockets [message #42059 is a reply to message #41919] Fri, 14 February 2014 11:41 Go to previous message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
If you have a considerable amount of data to send you can have many sentences that end with \n which is just another character.

The end is \0
Previous Topic: GuiMT compilation error on ubuntu12.04
Next Topic: [SOLVED] Unable to compile MT applications (weird assembler error with GCC on Linux i386 arch)
Goto Forum:
  


Current Time: Sun Apr 11 09:48:55 CEST 2021

Total time taken to generate the page: 0.01016 seconds