Exemple #1
0
void NewClient(int ClientSocket, int id)
{
	printf("Connection %d added...\n", id);
	AddActiveSocket(&ClientSocket, id);
	ReceiveThread(&ClientSocket, id);

	printf("Connection %d dead...\n", id);
}
void NewClient(SOCKET ClientSocket)
{
    AddActiveSocket(&ClientSocket);
    int iResult;
    int iSendResult;
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;
    static int thcount = 1;
    int thisthread = thcount;
    thcount++;
    printf("thread %d started\n", thisthread);

    // Receive until the peer shuts down the connection
    while (1)
    {
        iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
        if (iResult > 0)
        {
            printf("Bytes received: %d\n", iResult);

            if (iResult < DEFAULT_BUFLEN)
                recvbuf[iResult] = '\0';
            else
                recvbuf[DEFAULT_BUFLEN - 1] = '\0';

            SendAll(recvbuf);
        }
        else if (iResult == 0)
        {
            printf("Connection closing... th=%d\n", thisthread);
            DeleteSocket(&ClientSocket);
            closesocket(ClientSocket);
            return;
        }
        else
        {
            printf("recv failed with error: %d\n", WSAGetLastError());
            DeleteSocket(&ClientSocket);
            closesocket(ClientSocket);
            return;
        }
    }

    DeleteSocket(&ClientSocket);
    printf("thread completed\n");
}