Sample code for 30+ languages & platforms
DataFlex

Bind and Listen for Incoming Connections

See more Socket/SSL/TLS Examples

Demonstrates Socket.BindAndListen, which binds a TCP listener to a port and places it into listening mode, then accepts an incoming connection.

Background. The backlog argument bounds the queue of pending connections. When ClientIpAddress is empty, the listener binds to all interfaces. After listening, call AcceptNext to accept connections.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSocket
    Variant vConnectedSock
    Handle hoConnectedSock
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatSocket)) To hoSocket
    If (Not(IsComObjectCreated(hoSocket))) Begin
        Send CreateComObject of hoSocket
    End

    //  Bind a TCP listener to a port and place it into listening mode.  The 2nd argument is the backlog,
    //  the maximum number of pending connections allowed to queue.
    Get ComBindAndListen Of hoSocket 5000 25 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  After the listener is established, accept the next incoming connection into a new Socket.
    Get Create (RefClass(cComChilkatSocket)) To hoConnectedSock
    If (Not(IsComObjectCreated(hoConnectedSock))) Begin
        Send CreateComObject of hoConnectedSock
    End
    Get pvComObject of hoConnectedSock to vConnectedSock
    Get ComAcceptNext Of hoSocket 20000 vConnectedSock To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Accepted a connection."


End_Procedure