From: <dar...@us...> - 2008-10-06 19:22:00
|
Revision: 3459 http://oorexx.svn.sourceforge.net/oorexx/?rev=3459&view=rev Author: darthapache Date: 2008-10-06 19:19:19 +0000 (Mon, 06 Oct 2008) Log Message: ----------- proposed socket sample programs Added Paths: ----------- sandbox/tdf/socket samples/ sandbox/tdf/socket samples/readme.txt sandbox/tdf/socket samples/scclient.rex sandbox/tdf/socket samples/scserver.rex sandbox/tdf/socket samples/sfclient.rex sandbox/tdf/socket samples/sfserver.rex Added: sandbox/tdf/socket samples/readme.txt =================================================================== --- sandbox/tdf/socket samples/readme.txt (rev 0) +++ sandbox/tdf/socket samples/readme.txt 2008-10-06 19:19:19 UTC (rev 3459) @@ -0,0 +1,6 @@ +These are simple socket server and client programs that give an example of +how to send and recieve data using a socket client and server. There are four +programs: sfclient.rex, sfserver.rex, scclient.rex, and scserver.rex +The two programs starting with sf use the socket functions, the two programs +starting with sc use the socket class. The clients and servers are interchangable, +the function based client can be used the the class based server, etc. \ No newline at end of file Property changes on: sandbox/tdf/socket samples/readme.txt ___________________________________________________________________ Added: svn:eol-style + native Added: sandbox/tdf/socket samples/scclient.rex =================================================================== --- sandbox/tdf/socket samples/scclient.rex (rev 0) +++ sandbox/tdf/socket samples/scclient.rex 2008-10-06 19:19:19 UTC (rev 3459) @@ -0,0 +1,48 @@ +/* scclient.rex */ +/* David Ruggles 3:02:26 PM Monday, October 06, 2008 */ +/* This program uses the socket class + to create a basic socket client */ + +/* instaniate an instance of the socket class */ + sock = .socket~new() + +/* instaniate an instance of the inetaddress class + with the host information of the server we will + contact: localhost and port 726578 + we use the "gethostid" class method of the socket + class to determine the localhost address */ + host = .inetaddress~new(.socket~gethostid(), '726578') + +/* connect to the server */ + if sock~connect(host) < 0 then do + say 'Connect Failed' + exit + end + + say 'type "X" to exit' + do forever + call charout , 'Send To Server: ' + parse pull message + if message~upper() = 'X' then leave + /* send message to server */ + if sock~send(message) < 0 then do + say 'Send Failed' + leave + end + /* get message from server */ + ret = sock~recv(1024) + if ret = .nil then do + if sock~errno() < 0 then + say 'Recv Failed' + else + say 'Socket Closed' + leave + end + say 'Server Responded:' ret + end + +/* close the socket connection */ + if sock~close() < 0 then + say 'SockClose Failed' + +::requires 'c:\dev\oorexx development\main\trunk\extensions\rxsock\socket.cls' \ No newline at end of file Property changes on: sandbox/tdf/socket samples/scclient.rex ___________________________________________________________________ Added: svn:executable + * Added: svn:eol-style + native Added: sandbox/tdf/socket samples/scserver.rex =================================================================== --- sandbox/tdf/socket samples/scserver.rex (rev 0) +++ sandbox/tdf/socket samples/scserver.rex 2008-10-06 19:19:19 UTC (rev 3459) @@ -0,0 +1,70 @@ +/* scserver.rex */ +/* David Ruggles 3:07:47 PM Monday, October 06, 2008 */ +/* This program uses the socket class + to create a basic socket echo server */ + +srv = .myserver~new() +srv~listen() + +::requires 'c:\dev\oorexx development\main\trunk\extensions\rxsock\socket.cls' + +::class myserver + +::method init + expose sock + +/* instaniate an instance of the socket class */ + sock = .socket~new() + +::method monitor unguarded + expose sock + + say 'Press Ctrl-C To Shutdown' + do forever + if sysgetkey('noecho')~c2x() = '03' then leave + end + +/* close the socket */ + if sock~close() < 0 then + say 'SockClose Failed' + +::method listen + expose sock + +/* instaniate an instance of the inetaddress class + with the host information of the server we will + contact: localhost and port 726578 + we use the "gethostid" class method of the socket + class to determine the localhost address */ + host = .inetaddress~new(.socket~gethostid(), '726578') + +/* bind to the host information */ + if sock~bind(host) < 0 then do + say 'Bind Failed' + exit + end + + if sock~listen(256) < 0 then do + say 'Listen Failed' + exit + end + + self~start('monitor') -- this will allow the server to be shutdown cleanly + + do forever + csock = sock~accept() -- prepare to accept a new client + if .nil = csock then leave -- if the socket is closed (by monitor) sockaccept will return .nil + /* this will spawn a thread to handle the new client and then return to accept the next client */ + self~start('respond', csock) + end + +::method respond unguarded + use arg sock + + do forever + /* get data from the client */ + data = sock~recv(1024) + if data = .nil then leave + /* echo that data back to the client */ + sock~send('Echo:' data) + end Property changes on: sandbox/tdf/socket samples/scserver.rex ___________________________________________________________________ Added: svn:executable + * Added: svn:eol-style + native Added: sandbox/tdf/socket samples/sfclient.rex =================================================================== --- sandbox/tdf/socket samples/sfclient.rex (rev 0) +++ sandbox/tdf/socket samples/sfclient.rex 2008-10-06 19:19:19 UTC (rev 3459) @@ -0,0 +1,50 @@ +/* sfclient.rex */ +/* David Ruggles 2:03:26 PM Monday, October 06, 2008 */ +/* This program uses the socket function package + to create a basic socket client */ + +/* load socket function package if it's not already loaded */ + if rxfuncquery('SockDropFuncs') then do + call rxfuncadd 'SockLoadFuncs', 'rxsock', 'SockLoadFuncs' + call SockLoadFuncs 'bypasscopyright' + end + +/* create a socket */ + socket = socksocket('AF_INET', 'SOCK_STREAM', 'IPPROTO_TCP') + +/* specify the host we will connect to */ + host.!family = 'AF_INET' -- Protocol family (only AF_INET is supported) + host.!addr = SockGetHostId() -- IP address (use the sockgethostid function to get address of the localhost) + host.!port = '726578' -- Port number + +/* connect to the server */ + if sockconnect(socket, 'host.!') < 0 then do + say 'SockConnect Failed' + exit + end + + say 'type "X" to exit' + do forever + call charout , 'Send To Server: ' + parse pull message + if message~upper() = 'X' then leave + /* send message to server */ + if socksend(socket, message) < 0 then do + say 'SockSend Failed' + leave + end + /* get message from server */ + ret = sockrecv(socket, 'data', 1024) + if ret < 1 then do + if ret < 0 then + say 'SockRecv Failed' + else + say 'Socket Closed' + leave + end + say 'Server Responded:' data + end + +/* close the socket connection */ + if sockclose(socket) < 0 then + say 'SockClose Failed' \ No newline at end of file Property changes on: sandbox/tdf/socket samples/sfclient.rex ___________________________________________________________________ Added: svn:executable + * Added: svn:eol-style + native Added: sandbox/tdf/socket samples/sfserver.rex =================================================================== --- sandbox/tdf/socket samples/sfserver.rex (rev 0) +++ sandbox/tdf/socket samples/sfserver.rex 2008-10-06 19:19:19 UTC (rev 3459) @@ -0,0 +1,72 @@ +/* sfserver.rex */ +/* David Ruggles 2:03:37 PM Monday, October 06, 2008 */ +/* This program uses the socket function package + to create a basic socket echo server */ + + srv = .myserver~new() + srv~listen() + +::class myserver + +::method init + expose socket + +/* load socket function package if it's not already loaded */ + if rxfuncquery('SockDropFuncs') then do + call rxfuncadd 'SockLoadFuncs', 'rxsock', 'SockLoadFuncs' + call SockLoadFuncs 'bypasscopyright' + end + +/* create a socket */ + socket = socksocket('AF_INET', 'SOCK_STREAM', '0') + +::method monitor unguarded + expose socket + + say 'Press Ctrl-C To Shutdown' + do forever + if sysgetkey('noecho')~c2x() = '03' then leave + end + +/* close the socket */ + if sockclose(socket) < 0 then + say 'SockClose Failed' + +::method listen + expose socket + +/* specify the host we will run as */ + host.!family = 'AF_INET' -- Protocol family (only AF_INET is supported) + host.!addr = SockGetHostId() -- IP address (use the sockgethostid function to get address of the localhost) + host.!port = '726578' -- Port number + +/* bind to the host information */ + if sockbind(socket, 'host.!') < 0 then do + say 'SockBind Failed' + exit + end + +/* start listening for new connections */ + if socklisten(socket, 256) < 0 then do + say 'SockListen Failed' + exit + end + + self~start('monitor') -- this will allow the server to be shutdown cleanly + + do forever + clientsocket = sockaccept(socket) -- prepare to accept a new client + if clientsocket = '-1' then leave -- if the socket is closed (by monitor) sockaccept will fail + /* this will spawn a thread to handle the new client and then return to accept the next client */ + self~start('respond', clientsocket) + end + +::method respond unguarded + use arg socket + + do forever + /* get data from the client */ + if sockrecv(socket, 'data', 1024) < 1 then leave + /* echo that data back to the client */ + call socksend socket, 'Echo:' data + end Property changes on: sandbox/tdf/socket samples/sfserver.rex ___________________________________________________________________ Added: svn:executable + * Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |