reference: Socket examples

git-svn-id: svn://ultimatepp.org/upp/trunk@1694 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2009-11-15 12:33:13 +00:00
parent 2da7a48780
commit f45e57ac95
6 changed files with 81 additions and 0 deletions

View file

@ -0,0 +1,21 @@
#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';
}

View file

@ -0,0 +1,12 @@
description "Example of using client Socket - its counterpart is SocketServer\377";
uses
Core,
Web;
file
SocketClient.cpp;
mainconfig
"" = "";

View file

@ -0,0 +1,5 @@
#ifndef _SocketClient_icpp_init_stub
#define _SocketClient_icpp_init_stub
#include "Core/init"
#include "Web/init"
#endif

View file

@ -0,0 +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 << '\n';
if(w == "time")
s.Write(AsString(GetSysTime()));
else
s.Write(AsString(3 * atoi(~w)));
s.Write("\n");
}
}
}

View file

@ -0,0 +1,12 @@
description "Example of using server Socket - its counterpart is SocketClient\377";
uses
Core,
Web;
file
SocketServer.cpp;
mainconfig
"" = "";

View file

@ -0,0 +1,5 @@
#ifndef _SocketServer_icpp_init_stub
#define _SocketServer_icpp_init_stub
#include "Core/init"
#include "Web/init"
#endif