| TCPACCEPT() | Accept a connection on a socket. |
| TCPCLOSE() | Close connection. |
| TCPCONNECT() | Connect with server. |
| TCPLISTEN() | Listen port. |
| TCPREAD() | Read to buffer data from connection. |
| TCPWRITE() | Commit data from buffer to connection. |
TCPACCEPT(<nSock>[, <nTimeout>]) --> <nNewSocket>
Returns numeric value <nNewSocket> handler of new socket or -1 if error occurced.
TCPACCEPT() extracts the first connection request on the queue of pending connections, creates a new connected socket with mostly the same properties as <nSock>, and allocates a new file descriptor <nNewSocket> for the socket, which is returned.
FERROR() and FERRORSTR()kept the error description.
#define DEFPORT 3000
local nH, nPort, nCon
nPort := DEFPORT
if (nH := TCPLISTEN( nPort, 10 )) == -1
? "tcpsrv: Error listen on port ", nPort
?
return( 1 )
endif
//? "wait client connection"
do while( .t. )
? "wait client connection",time()
if (nCon := TCPACCEPT( nH, 5000 )) != -1
? "start",nH,nCon
start( {|nC| /* reading data from connection */ TCPREAD(nC....)}, nCon )
endif
sleep(0.01)
enddo
TCPCLOSE( nH )
TCPCLOSE(<nConnection>) --> TRUE || FALSE
Returns TRUE if connection was successfully closed.
TCPCLOSE() closes opened connections and returns TRUE if operation was successfully.
nH := TCPLISTEN(3000) If nH > 0 .... TCPCLOSE(nH) endif
TCPCONNECT(<sHostName>[, <nPort>][, <nTimeout>]) --> <nConnection>
Returns the connection number or -1.
TCPCONNECT() makes connect to server <sHostName> and creates socket to port <nPort>. Returns connection number <nConnection> or -1 on error. FERROR() and FERRORSTR()kept the error description.
con := TCPCONNECT("http://www.itk.ru")
if con > -1
? "connection: ", con
....
TCPCLOSE(con)
else
? FERROR()
endif
TCPLISTEN(<nPort>[, <nBackLog>]) --> <nErrNo>
Returns the numeric value <nErrNo>: on zero - operation was successfully, <nErrNo> < 0 - error code.
TCPLISTEN() creates socket and stands for incomming connections.
#define DEFPORT 3000
local nH, nPort, nCon
nPort := DEFPORT
if (nH := TCPLISTEN( nPort, 10 )) == -1
? "tcpsrv: Error listen on port ", nPort
?
return( 1 )
endif
//? "wait client connection"
do while( .t. )
? "wait client connection",time()
if (nCon := TCPACCEPT( nH, 5000 )) != -1
? "start",nH,nCon
start( {|nC| /* reading data from connection */ TCPREAD(nC....)}, nCon )
endif
sleep(0.01)
enddo
TCPCLOSE( nH )
TCPREAD(<nConnection>, @<sBuffer>, <nLen>[, <nTimeout>]) --> <nLenBuf>
Returns numeric value - is the realy number of bytes received, or -1 if an error occurred.
TCPREAD() reads from connection <nConnection> some data (no more of <nLen> bytes) and writes it to buffer <sBuffer>. FERROR() and FERRORSTR()kept the error description.
con := TCPCONNECT("http://www.itk.ru")
if con > -1
? "connection: ", con
buf := ""
tr := TCPREAD(con, @buf, 1024)
if tr > -1
? buf
else
? "Error:", FERROR(), ":", FERRORSTR()
endif
TCPCLOSE(con)
else
? FERROR(), FERRORSTR()
endif
TCPWRITE(<nConnection>, <sBuffer>, <nLen>[, <nTimeout>]) --> <nLenBuf>
Returns numeric value - is the realy number of bytes commited, or -1 if an error occurred.
TCPWRITE() trys write some data <sBuffer> (with length <nLen> bytes)to connection <nConnection>. FERROR() and FERRORSTR()kept the error description.
local cBuf := space(5), nL, cTxt, nH, nPort nPort := DEFPORT if (nH := TCPLISTEN( nPort, 10 )) == -1 ? "tcpsrv: Error listen on port ", nPort ? return( 1 ) endif do while( .t. ) if( (nL := TCPREAD( nH, @cBuf, len(cBuf), 60000 )) > 0 ) ? "tcpsrv, read : ", nL if( nL == 1 .and. upper( substr(cbuf,1,1)) == "Q" ) exit endif cTxt := "tcpsrv:" + substr(cBuf, 1, nL) TCPWRITE( nH, cTxt ) elseif ferror() == 32 ? "Error:",ferrorstr()+":","closed task:",nh exit endif //? "tcpsrv, read : ", nL,nh,time() enddo TCPCLOSE( nH )