reference: SocketClient and SocketServer changed to use Core:TcpSocket

git-svn-id: svn://ultimatepp.org/upp/trunk@5382 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-09-22 08:12:58 +00:00
parent a0f7972d4b
commit ec179e638b
2 changed files with 49 additions and 47 deletions

View file

@ -1,21 +1,23 @@
#include <Web/Web.h>
using namespace Upp;
String Request(const String& r)
{
Socket s;
if(!ClientSocket(s, CommandLine().GetCount() ? CommandLine()[0] : "127.0.0.1", 3214)) {
Cout() << "Unable to connect to server!\n";
SetExitCode(1);
return Null;
}
s.Write(r + '\n');
return s.ReadUntil('\n');
}
CONSOLE_APP_MAIN
{
Cout() << Request("time") << '\n';
Cout() << Request("33") << '\n';
}
#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');
return s.GetLine();
}
// Start reference/SocketServer before starting this program
CONSOLE_APP_MAIN
{
Cout() << Request("time") << '\n';
Cout() << Request("33") << '\n';
}

View file

@ -1,26 +1,26 @@
#include <Web/Web.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
Socket server;
if(!ServerSocket(server, 3214)) {
Cout() << "Unable to initialize server socket!\n";
SetExitCode(1);
return;
}
Cout() << "Waiting for requests..\n";
for(;;) {
Socket s;
if(server.Accept(s)) {
String w = s.ReadUntil('\n');
Cout() << "Request: " << w << " from: " << s.GetPeerAddr() << '\n';
if(w == "time")
s.Write(AsString(GetSysTime()));
else
s.Write(AsString(3 * atoi(~w)));
s.Write("\n");
}
}
}
#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";
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");
}
}
}