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   | 
		 
		
			
				
				
				  | 
					
						  
						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 : 
  
 
 
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  
 |  
	| 
		
	 | 
 
 
 |  
  
 
Goto Forum:
 
 Current Time: Tue Nov 04 03:00:37 CET 2025 
 Total time taken to generate the page: 0.06323 seconds 
 |