|
|
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 Go to next message](theme/default/images/down.png) |
Oblivion
Messages: 1169 Registered: August 2007
|
Senior 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.
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Tue, 17 December 2013 15:38] Report message to a moderator
|
|
|
|
Re: A problem with TcpSocket::GetLine() [message #41356 is a reply to message #41355] |
Tue, 03 December 2013 18:12 ![Go to previous message Go to previous message](theme/default/images/up.png) ![Go to next message Go to next message](theme/default/images/down.png) |
Oblivion
Messages: 1169 Registered: August 2007
|
Senior 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.
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Tue, 03 December 2013 18:24] Report message to a moderator
|
|
|
|
|
Re: A problem with TcpSocket::GetLine() [message #41422 is a reply to message #41420] |
Mon, 16 December 2013 01:26 ![Go to previous message Go to previous message](theme/default/images/up.png) ![Go to next message Go to next message](theme/default/images/down.png) |
Oblivion
Messages: 1169 Registered: August 2007
|
Senior 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.
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
|
|
|
|
|
|
|
Goto Forum:
Current Time: Mon Jan 13 21:59:29 CET 2025
Total time taken to generate the page: 0.01634 seconds
|
|
|