Below are the codes of simple application server and client. Server waits for a connection from client, reads messages from client, and sends them back. Client connects to the server, reads data from keyboard, sends them to the server, and prints what server returns.
Server source:
// Stand for connection and simply make echo. #define DEFPORT 3000 function main( cport ) local nPort, nH, nT := seconds() + 60 local nCon clear screen nport := iif( cport != nil, val(cport), DEFPORT ) if (nH := tcpListen( nport, 10 )) == -1 ? "tcpsrv: Error listen on port ", nport ? return( 1 ) endif ? "wait client connection" do while( .t. ) if (nCon := tcpAccept( nH, 50 )) != -1 ? "start",nH,nCon start( @conEcho(), nCon ) endif sleep(1) enddo tcpClose( nH ) ? return( 0 ) static function ConEcho( nH ) // Make echo. local cBuf := space(5), nL, cTxt ? "task, param : ", nH do while( .t. ) if( (nL := tcpRead( nH, @cBuf, len(cBuf), 6000 )) > 0 ) if( nL == 1 .and. upper( substr(cbuf,1,1)) == "Q" ) exit endif ? "tcpsrv, read : ", nL cTxt := "tcpsrv:" + substr(cBuf, 1, nL) tcpWrite( nH, cTxt ) endif enddo tcpClose( nH ) return( 0 )
Client source:
#define BUFLEN 1024
#define DEFPORT 3000
#define DEFHOST "localhost"
#define nTimeOut 600
function main( cport, chost )
local nH, nPort , nL, cbuf := space( BUFLEN )
local nK, cEnv
clear screen
nPort := iif(valtype(cport)=='C', val( cport), DEFPORT )
if( chost == nil ); chost := DEFHOST; endif
? "Connecting to " + getHostByName( cHost )
if( (nH := tcpConnect( cHost, nPort, nTimeOut )) == -1 )
? "Error connecting to " + cHost + " on port "
?? nPort
?
return( 1 )
endif
nK := 0; cEnv := ""
do while ( nK != 27 )
if( (nL := tcpRead( nH, @cbuf, BUFLEN, 60 )) > 0 )
? "Received ("+alltrim(str(nL))+"):"+substr(cbuf,1,nL)
?
endif
if( (nK := inkey( 0.1 )) == 0 )
loop; endif
if( nK == 13 )
nL := tcpWrite( nH, cEnv )
? "Send: ",nl, + cEnv
if upper(cEnv)=="Q"
exit
endif
cEnv := ""
else
cEnv += chr(nK)
endif
enddo
? tcpClose( nH )
?
return( 0 )