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 » [SOLVED] A problem with TcpSocket::GetLine()
[SOLVED] A problem with TcpSocket::GetLine() [message #41354] Tue, 03 December 2013 16:33 Go to next message
Oblivion is currently offline  Oblivion
Messages: 875
Registered: August 2007
Experienced Contributor
Hello,

I am trying to use the TcpSocket::GetLine() method, and I have encountered a problem.

When I try to connect to a server and read the input (here, a single line, server "hello" message, ending with \n") with the GetLine() method, below code always encounter a timeount error and returns immediately. But when I use, say, Get(512), it works as expected. Why is GetLine() method not working here, and what am I doing wrong, any ideas?

Thanks in advance.

#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	TcpSocket socket;
	
	if(!socket.Connect("pop.gmail.com", 995)) {
		Cout() << "Socket error encountered: " << socket.GetErrorDesc() << "\r\n";
		Exit(1);
	}
	if(!socket.StartSSL()) {
		if(socket.IsOpen()) socket.Close();
		Cout() << "Couldn't start SSL session.\r\n";
		Exit(1);
	}

	// 5 secs.
	socket.Timeout(5000);
	
	// Get() method is working as expected
	// String server_hello = socket.Get(512);
	
	// GetLine() method always fails with a timeout error.
	// No matter if timeout value witle the GlobalTimeout() or the Timeout() method is set.
	String server_hello = socket.GetLine();
	if(IsNull(server_hello))
		Cout() 	<< "An error occured: "	<< socket.GetErrorDesc() << "\r\n";
	else
	 	Cout() 	<< server_hello <<	"\r\n";

	if(socket.IsOpen())
		socket.Close();
}


Regards.


[Updated on: Tue, 17 December 2013 15:38]

Report message to a moderator

Re: A problem with TcpSocket::GetLine() [message #41355 is a reply to message #41354] Tue, 03 December 2013 17:03 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
First: I suggest you create a server on your own machine.
There is an example of a server and client in the SDK.

Second: End your String with \0

Then post with what you are getting.

I use a socket as client in C/C++ to communicate with a java server. Trying to jump on the web until you get the bugs out with the client and server on your own machine is like trying to run before learning to walk.
Re: A problem with TcpSocket::GetLine() [message #41356 is a reply to message #41355] Tue, 03 December 2013 18:12 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 875
Registered: August 2007
Experienced Contributor
nlneilson wrote on Tue, 03 December 2013 18:03

First: I suggest you create a server on your own machine.
There is an example of a server and client in the SDK.

Second: End your String with \0

Then post with what you are getting.

I use a socket as client in C/C++ to communicate with a java server. Trying to jump on the web until you get the bugs out with the client and server on your own machine is like trying to run before learning to walk.


Hello nineilson, thank you for your reply.

I already examined server & client example, I am afraid it was not very helpful for my case.

The code snippet I posted above was actually the simplest test case I could come up with, to show the actual behaviour.
(by the way, this is tested under Linux)

Just to clarify the situtation in detail: I wrote a POP3 class, which actually works as expected and my intention was to upload it to the Upp bazaar. But then I decided to simplfy the code a bit. Because;

1) A standard POP3 server produces two types of well defined and predictable responses which can be read by a client. A single line respone (terminates with a "\r\n") and a multi-line response (terminates with a "\r\n.\r\n").

3) So, to increase the code clarity I decided to use the TcpSocket::GetLine() method to read single-line server responses (such as the pop3 server "hello" messages).

In theory, this should work (or I am getting something wrong?). But in practice, as with the above test case, it simply and immediately fails with a timeout error and increasing the timeout value with GlobalTimeout() or Timeout() does not help at all -- It does not read anything. Now, the thing is, as I've mentioned on my previous post, TcpSocket::Get() method works where TcpSocket::GetLine() reads nothing.

Any other ideas?

Regards.


[Updated on: Tue, 03 December 2013 18:24]

Report message to a moderator

Re: A problem with TcpSocket::GetLine() [message #41377 is a reply to message #41356] Sun, 08 December 2013 22:16 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
I have not worked with POP3 so cannot give any suggestions there.

The client - server examples that comes with U++ are very basic.
Unless you understand them and can debug through them if you have problems I don't know what to suggest.

Your 'error' seems to be the web address server is not accepting your client.
Re: A problem with TcpSocket::GetLine() [message #41420 is a reply to message #41377] Sun, 15 December 2013 20:08 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13252
Registered: November 2005
Ultimate Member
Bug identified & fixed. Should work now.

Mirek

P.S.: POP3 package would be handy.. Smile
Re: A problem with TcpSocket::GetLine() [message #41422 is a reply to message #41420] Mon, 16 December 2013 01:26 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 875
Registered: August 2007
Experienced Contributor
Quote:


Bug identified & fixed. Should work now.

Mirek

P.S.: POP3 package would be handy..



Thanks Mirek! I've updated the U++ source, and now it works.

I will upload the POP3 class before the new year. But before that, I will clean up the code and write the api documentation. It is a straightforward POP3 implementation meant to accompany SMTP.

However, I have another question and problem regarding TcpSocket::GetLine():

GetLine() also fails if there are any multibyte characters in the socket buffer. In SocketClient and SocketServer examples, just Put("İŞĞişğ\n") and you'll see that it fails. Is this intended, or a bug?

Quote:


#include <Core/Core.h>

using namespace Upp;

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');
// The following line cannot be read by SocketServer.

s.Put("testing TcpSocket:GetLine(): İŞĞişğ\n");

s.Timeout(5000);
return s.GetLine();
}

// Start reference/SocketServer before starting this program

CONSOLE_APP_MAIN
{
Cout() << Request("time") << '\n';
Cout() << Request("33") << '\n';
}




Regards.


Re: A problem with TcpSocket::GetLine() [message #41429 is a reply to message #41422] Mon, 16 December 2013 14:07 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13252
Registered: November 2005
Ultimate Member
Those characters got a kind of scrambled, could you please zip it and post the whole package?
Re: A problem with TcpSocket::GetLine() [message #41431 is a reply to message #41429] Mon, 16 December 2013 15:26 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 875
Registered: August 2007
Experienced Contributor
Hello Mirek, I am uploading a modified version of the SocketClient example. It contains Turkish letters, which the GetLine() in SocketServer refuses to read.

By the way, changing "if(c < 0)" to "if(c == -1)" (in Sockep.cpp, line 837) seem to fix this problem, since Peek() returns -1 on both error and timeout. But I haven't examined the internals of TcpSocket class in detail, so I am not sure if it counts as a fix or a workaround.

Regards.


[Updated on: Mon, 16 December 2013 15:27]

Report message to a moderator

Re: A problem with TcpSocket::GetLine() [message #41440 is a reply to message #41431] Tue, 17 December 2013 14:06 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13252
Registered: November 2005
Ultimate Member
Oblivion wrote on Mon, 16 December 2013 09:26


By the way, changing "if(c < 0)" to "if(c == -1)" (in Sockep.cpp, line 837) seem to fix this problem


Ah! Damn stupid classical problem (char is signed). Please try now, should be fixed.
Re: A problem with TcpSocket::GetLine() [message #41441 is a reply to message #41440] Tue, 17 December 2013 15:37 Go to previous message
Oblivion is currently offline  Oblivion
Messages: 875
Registered: August 2007
Experienced Contributor
Thanks Mirek!
The bug is fixed. Everything works as expected.

Regards.


Previous Topic: COM servers
Next Topic: GuiMT compilation error on ubuntu12.04
Goto Forum:
  


Current Time: Sun Apr 11 09:34:20 CEST 2021

Total time taken to generate the page: 0.01589 seconds