8

Below is my code for Linux. I am implementing a client/server application and below is the server .cpp file.

int main()
{
 int serverFd, clientFd, serverLen, clientLen;
struct sockaddr_un serverAddress;/* Server address */
struct sockaddr_un clientAddress; /* Client address */
struct sockaddr* serverSockAddrPtr; /* Ptr to server address */
struct sockaddr* clientSockAddrPtr; /* Ptr to client address */

/* Ignore death-of-child signals to prevent zombies */
signal (SIGCHLD, SIG_IGN);

serverSockAddrPtr = (struct sockaddr*) &serverAddress;
serverLen = sizeof (serverAddress);

clientSockAddrPtr = (struct sockaddr*) &clientAddress;
clientLen = sizeof (clientAddress);

/* Create a socket, bidirectional, default protocol */
serverFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
serverAddress.sun_family = AF_LOCAL; /* Set domain type */
strcpy (serverAddress.sun_path, "css"); /* Set name */
unlink ("css"); /* Remove file if it already exists */
bind (serverFd, serverSockAddrPtr, serverLen); /* Create file */
listen (serverFd, 5); /* Maximum pending connection length */   

readData();

while (1) /* Loop forever */
  {
    /* Accept a client connection */
    clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

    if (fork () == 0) /* Create child to send recipe */
      {
        printf ("");
    printf ("\nRunner server program. . .\n\n");
    printf ("Country Directory Server Started!\n");

        close (clientFd); /* Close the socket */
        exit (/* EXIT_SUCCESS */ 0); /* Terminate */
      }
    else
      close (clientFd); /* Close the client descriptor */
  }

}

When i tried to compile it displays an error message which shows.

 server.cpp:237:67: error: invalid conversion from ‘int*’ to ‘socklen_t*’
server.cpp:237:67: error:   initializing argument 3 of ‘int accept(int, sockaddr*, socklen_t*)’

It points to this line

clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

I do not actually know how to solve this problem. Thanks in advance to those who helped! :)

alk
  • 69,737
  • 10
  • 105
  • 255
Andres
  • 275
  • 2
  • 6
  • 14

2 Answers2

19

Define clientLen as socklen_t:

socklen_t clientLen;

instead of

int clientLen;
Marko Kevac
  • 2,902
  • 30
  • 47
  • This is the obvious right answer, but I'm going to throw a tangential comment in here. I had the same problem because I started from an example that I grabbed off the web. I didn't really know what I was doing and I was reluctant to deviate from the example. – David Dec 28 '18 at 21:48
3

Change,

clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

to

clientFd = accept (serverFd, clientSockAddrPtr,(socklen_t*)&clientLen);
Baris Demiray
  • 1,539
  • 24
  • 35
  • 1
    You might want to read [How Do I Write A Good Answer](http://stackoverflow.com/help/how-to-answer). The *why* is as important as the *how*. ;) – Markus W Mahlberg Sep 15 '15 at 09:28