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
my TCP client/server don't work correctly [message #57046] Mon, 17 May 2021 20:52 Go to next message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
Following SocketServer / SocketClient example :

Server :
#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	TcpSocket server;
	if(!server.Listen(3214, 5)) {
		Cout() << "Unable to initialize server socket!\n";
		SetExitCode(1);
		return;
	}
	Cout() << "Waiting for requests..\n";
	TcpSocket s;
	if(s.Accept(server)) {
		while(!s.IsError()){
			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");
		}
	}
	Cout() << "Stopping server..\n";
}


Client :
#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	TcpSocket s;
	if(!s.Connect(CommandLine().GetCount() ? CommandLine()[0] : "127.0.0.1", 3214)) {
		Cout() << "Unable to connect to server!\n";
		SetExitCode(1);
	}
	s.GlobalTimeout(1000);
	s.Put("time" + '\n');
	Cout() << s.GetLine() <<EOL;
	s.Put("hello" + '\n');
	Cout() << s.GetLine() <<EOL;
}


Here is what I get in my console :
https://i.imgur.com/QQTUsqG.png


What I'm doing wrong ? the SocketServer / SocketClient work fine but it only use the socket once to send and receive data once before closing it

[Updated on: Mon, 17 May 2021 21:03]

Report message to a moderator

Re: my TCP client/server don't work correctly [message #57047 is a reply to message #57046] Mon, 17 May 2021 21:03 Go to previous messageGo to next message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
No Message Body
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 next message
Oblivion is currently offline  Oblivion
Messages: 1143
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

Re: my TCP client/server don't work correctly [message #57049 is a reply to message #57048] Mon, 17 May 2021 23:29 Go to previous messageGo to next message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
Hello again Oblivion !

Only having one socket and send multiple data across this one is what I aim for. That why I dont do a for in my example.

Probleme is, when I connect to the server. Neither my first nor my second data is received by server. Instead I receive a Null string from server (probably when I destroy the client socket) (this behavior can be seen in the console screen)
Re: my TCP client/server don't work correctly [message #57050 is a reply to message #57049] Mon, 17 May 2021 23:53 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1143
Registered: August 2007
Senior Contributor
Probleme is, when I connect to the server. Neither my first nor my second data is received by server. Instead I receive a Null string from server (probably when I destroy the client socket) (this behavior can be seen in the console screen)



Then, the reason seems to be:


//	s.Put("time" + '\n'); // << ------------ This won't work... Clang: warning: adding 'char' to a string does not append to the string [-Wstring-plus-int] (not the same thing what SocketClient does. It appends it to a Upp::String, using the relevant operator.)

        s.Put("time\n");            // << ------------ This works as expected.
	s.Put(String("time") + '\n'); // also works
	Cout() << s.GetLine() <<EOL;
	s.Put("hello\n");
	Cout() << s.GetLine() <<EOL;




Best regards,
Oblivion


[Updated on: Tue, 18 May 2021 00:03]

Report message to a moderator

Re: my TCP client/server don't work correctly [message #57051 is a reply to message #57050] Tue, 18 May 2021 08:42 Go to previous message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
Indeed it make sense...
will give a try after work.
Thanks again Oblivion

Edit : It worked fine, here is the code I come up with (not perfect and Many thing need to be added but it might help) :
if(!server.Listen(port, 1)) {
		LOG("Unable to initialize server socket on port " + AsString(port));
		return;
	}
	client.GlobalTimeout(1000);
	LOG("Waiting for webServer...");
	while(!Thread::IsShutdownThreads()){
		
		if(client.Accept(server)){
			LOG("WebServer connected");
			int emptyData = 0;
			while(!client.IsError()){
				Upp::String data = client.GetLine();
				if(!client.IsTimeout()){
					Upp::String sendingCmd = "";
					LOG("Receiving : " + data + " from: " + client.GetPeerAddr());
					sendingCmd = ProcessCommandNetwork(data);
					LOG("Sending: " + sendingCmd + " To: " + client.GetPeerAddr());
					sendingCmd += '\n';
					sendingCmd.Shrink();
					client.Put(sendingCmd);
				}else{
					client.ClearError();
				}
			}
			LOG("WebServer error: " + client.GetErrorDesc());
			client.Close();
			client.Clear();
			LOG("WebServer disconnected");
		}
	}
	LOG("Connection with webServer ended");

[Updated on: Wed, 19 May 2021 08:49]

Report message to a moderator

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


Current Time: Sat Dec 14 13:12:29 CET 2024

Total time taken to generate the page: 0.03533 seconds