Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Accept Connection on Socket

See more Socket/SSL/TLS Examples

Demonstrates how to create a TCP/IP socket, listen on a port, accept an incoming connection, and send a "Hello World" message to the client.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oListenSocket
LOCAL oConnectedSocket

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oListenSocket := CreateObject("Chilkat.Socket")

//  Bind to a port and listen for incoming connections:
//  This example will listen at port 5555 and allows for a backlog
//  of 25 pending connection requests.
nSuccess := oListenSocket:BindAndListen(5555, 25)
IF (nSuccess == 0)
    ? oListenSocket:LastErrorText
    oListenSocket:destroy()
    RETURN
ENDIF

//  Get the next incoming connection
//  Wait a maximum of 20 seconds (20000 millisec)
oConnectedSocket := CreateObject("Chilkat.Socket")
nSuccess := oListenSocket:AcceptNext(20000, oConnectedSocket)
IF (nSuccess == 0)
    ? oListenSocket:LastErrorText
    oListenSocket:destroy()
    oConnectedSocket:destroy()
    RETURN
ENDIF

//  Set maximum timeouts for reading an writing (in millisec)
oConnectedSocket:MaxReadIdleMs := 10000
oConnectedSocket:MaxSendIdleMs := 10000

//  Send a "Hello World!" message to the client:
nSuccess := oConnectedSocket:SendString("Hello World!")
IF (nSuccess != 1)
    ? oConnectedSocket:LastErrorText
    oListenSocket:destroy()
    oConnectedSocket:destroy()
    RETURN
ENDIF

//  Close the connection with the client.
//  Wait a max of 20 seconds (20000 millsec)
nSuccess := oConnectedSocket:Close(20000)

? "success!"

oListenSocket:destroy()
oConnectedSocket:destroy()