How can I write accept function in my library in socket programming?
Asked 07 September, 2021
Viewed 1.2K times
  • 54
Votes

I am trying to write the code with my own library. I did this for the other functions such as 'bind' but when I write accept function the code did not work.

This is the code in the library:

int bindASocket(int* TheTcpSocket, struct sockaddr_in* TheTcpAddress);
int AcceptASocket(int* TheTCPSocket,struct sockaddr_in* TheTcpAddress);

these are function codes:

int AcceptASocket(int* TcpSocket, struct sockaddr_in* TcpAddress)
{
    int a=0;
    a = accept(*TcpSocket, (struct sockaddr*)TcpAddress, sizeof(struct sockaddr));

    if (TcpSocket == NULL)
    {
        printf("Accept - The socket was null....returning
");
        return -5;
    }
    if (TcpAddress == NULL)
    {
        printf("Accept - Address was null....returning
");
        return -6;
    }

    if (a == SOCKET_ERROR)
    {
        printf("Accept failed with error code : %d", WSAGetLastError());
        return -7;
    }
    puts("Connection accepted.");
}

This is the main code:

a = AcceptASocket(&sck,&client_bilgileri);

Socket changes while the accept function is getting created so problem could be about the socket but I could not understand the solution. When I wrote new_socket instead of sck I could not solve that too.

0 Answer