Sample code for 30+ languages & platforms
C

Accept the Next Incoming Connection

See more Socket/SSL/TLS Examples

Demonstrates Socket.AcceptNext, which waits for and accepts the next incoming connection on a listening socket, delivering it as a new connected Socket.

Background. A maxWaitMs of 0 waits indefinitely; a positive value waits up to that many milliseconds. For a TLS listener, acceptance includes the server-side TLS handshake.

Chilkat C Downloads

C
#include <C_CkSocket.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSocket socket;
    HCkSocket connectedSock;

    success = FALSE;

    socket = CkSocket_Create();

    success = CkSocket_BindAndListen(socket,5000,25);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(socket));
        CkSocket_Dispose(socket);
        return;
    }

    //  Wait for and accept the next incoming connection on the listening socket.  maxWaitMs = 0 waits
    //  indefinitely; a positive value waits up to that many milliseconds.  For a TLS listener, acceptance
    //  includes the server-side TLS handshake.
    connectedSock = CkSocket_Create();
    success = CkSocket_AcceptNext(socket,20000,connectedSock);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(socket));
        CkSocket_Dispose(socket);
        CkSocket_Dispose(connectedSock);
        return;
    }

    printf("Accepted a connection from %s\n",CkSocket_remoteIpAddress(connectedSock));


    CkSocket_Dispose(socket);
    CkSocket_Dispose(connectedSock);

    }