| xHarbour Reference Documentation > Function Reference |
![]() |
![]() |
![]() |
Creates a server side socket.
INetServer( <nPort> , ;
[<pRawSocket>] , ;
[<cIPAddress>] , ;
[<nMaxConnections>]) --> pServerSocket
The function returns a server side socket configured for listening to connection requests from remote client processes.
Before INetServer() can be called, the sockets system must be initialized with INetInit(). INetServer() returns a socket configured for the server side of an internet communication. Unlike a client side socket, a server side socket cannot establish a connection to a remote process, but must wait until a connection request comes in. INetServer() configures a socket for this very purpose. The returned socket must then be passed to function INetAccept() which listens on the server socket until a remote process requests a connection. The return value of INetAccept() is a socket that can be used to exchange data between the current process (server) and a remote process (client).
The following examples outline the essentials how to use sockets functions for establishing a communication between a local server and remote client processes using the TCP/IP protocol.
| See also: | INetAccept(), INetConnect(), INetRecv(), INetSend(), INetSetTimeout() |
| Category: | Internet functions , Sockets functions , xHarbour extensions |
| Source: | vm\INet.c |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
Server side
// This example is the server side of a TCP/IP communication between
// processes. The server socket is created with INetServer() and then
// configured with a timeout value. As a result, INetAccept() returns
// every half second when there is no connection request. This allows
// for querying the keyboard two times a second and check if the user
// pressed a key to terminate the server process.
//
// Note: the example must be built as multi-threading application
// e.g. Xbuild -mt TestServer
//
PROCEDURE Main
LOCAL pServer, pClient
CLS
INetInit()
// listen on port 1800
pServer := INetServer( 1800 )
// stop listening after .5 seconds
INetSetTimeout( pServer, 500 )
? "Server up and running", pServer
? "Press any key to quit"
DO WHILE Inkey(.1) == 0
// wait for incoming connection requests
pClient := INetAccept( pServer )
IF INetErrorCode( pServer ) == 0
// process client request in separate thread
StartThread( @ServeClient(), pClient )
ELSE
// display some kind of "I am still alive" message
?? "."
ENDIF
ENDDO
// make sure second thread has ended
WaitForThreads()
// close socket and cleanup memory
INetClose( pServer )
INetCleanup()
RETURN
PROCEDURE ServeClient( pSocket )
LOCAL cBuffer, nBytes, cData := ""
// display IP address of client
? "Serving:", INetAddress( pSocket )
// wait until data containing CRLF has arrived
// (this is blocking the thread)
cData := INetRecvLine( pSocket, @nBytes )
? "Bytes received:", nBytes
? "Data received:", cData
cData := "Server reports: " + ;
LTrim(Str( nBytes )) + ;
" bytes " + INetCRLF()
// report to client process
INetSend( pSocket, cData )
RETURN
Client side
// This example is the client side of a TCP/IP communication between
// processes. The server IP address defaults to "127.0.0.1", which is
// the IP address of the local station. The user can enter data in a
// single GET. This data is transferred to the server process, and the
// response of the server process is displayed in the client process.
//
// Note: the example can be built as a single threaded application
// e.g. Xbuild TestClient
//
#include "Inkey.ch"
PROCEDURE Main( cServerIPAddress )
LOCAL pSocket, cData, nBytes
IF Empty( cServerIPAddress )
// IP address of the local station
cServerIPAddress := "127.0.0.1"
ENDIF
INetInit()
DO WHILE Lastkey() <> K_ESC
CLS
// let the user enter some data
cData := Space(80)
@ 2,2 SAY "Enter a string:" GET cData PICTURE "@S30"
READ
IF Lastkey() == K_ESC
EXIT
ENDIF
cData := Trim( cData )
nBytes := Len ( cData )
// connect to server on port 1800
pSocket := INetConnect( cServerIPAddress, 1800 )
IF INetErrorCode( pSocket ) <> 0
? "Socket error:", INetErrorDesc( pSocket )
INetCleanUp()
QUIT
ENDIF
// Send data and append "new line" characters
// This is what the "example server" expects
INetSend( pSocket, cData + INetCRLF() )
CLS
? "sent:", LTrim(Str(nBytes)), "bytes"
// Receive the server's response
cData := ""
DO WHILE nBytes > 0
cBuffer := Space( 100 )
nBytes := INetRecv( pSocket, @cBuffer )
cData += Left( cBuffer, nBytes )
ENDDO
// disconnect from server
INetClose( pSocket )
? "received:", cData
WAIT
ENDDO
INetCleanUp()
RETURN
http://www.xHarbour.com