2

I am trying to learn a bit of socket programming, I am also pretty new to c and having some trouble creating my server.

I am having trouble with pointers and the error I get when I try to run the code is

warning: passing 'int *' to parameter of type 'socklen_t *' (aka 'unsigned int *') converts between pointers to integer types with different sign [-Wpointer-sign]`

My server code is as follows, can anyone help with the error I am getting? Any other tips or comments are welcome as well

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main( int argc, char *argv[] )
{
   int sockfd, newsockfd, portno, clilen;
   char buffer[256];
   struct sockaddr_in serv_addr, cli_addr;
   int  n;

   /* First call to socket() function */
   sockfd = socket(AF_INET, SOCK_STREAM, 0);

   if (sockfd < 0)
      {
      perror("ERROR opening socket");
      exit(1);
      }

   /* Initialize socket structure */
   bzero((char *) &serv_addr, sizeof(serv_addr));
   portno = 5001;

   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = INADDR_ANY;
   serv_addr.sin_port = htons(portno);

   /* Now bind the host address using bind() call.*/
   if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
      {
      perror("ERROR on binding");
      exit(1);
      }

   /* Now start listening for the clients, here process will
   * go in sleep mode and will wait for the incoming connection
   */

   listen(sockfd,5);
   clilen = sizeof(cli_addr);

   /* Accept actual connection from the client */
   newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
   if (newsockfd < 0)
      {
      perror("ERROR on accept");
      exit(1);
      }

   /* If connection is established then start communicating */
   bzero(buffer,256);
   n = read( newsockfd,buffer,255 );

   if (n < 0)
      {
      perror("ERROR reading from socket");
      exit(1);
      }

   printf("Here is the message: %s\n",buffer);

   /* Write a response to the client */
   n = write(newsockfd,"I got your message",18);

   if (n < 0)
      {
      perror("ERROR writing to socket");
      exit(1);
      }

   return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Hayes121
  • 283
  • 7
  • 25
  • 1
    What line is the error, and could you possibly post shorter code that exhibits the problem? – Almo Feb 16 '15 at 21:39
  • Error is on line 55, i will try shorten down the code a little – Hayes121 Feb 16 '15 at 21:41
  • Easy: look at the documentation for the function on the line where the error occurs. Actually, Clang will draw an arrow tomthe exact parameter. (You ought to have indicated that in the post, BTW). – JDługosz Feb 16 '15 at 21:45
  • It seems that you have problem with `accept()` call and variable `clilen` which is defined as `int`. Change it to unsigned integer. `accept()` expects `socklen_t *` as last argument. And `socklen_t` is unsigned int type. – Anto Jurković Feb 16 '15 at 21:54

1 Answers1

5

Well, Actually you could mention to the the exact place of the error so we could look faster.

Anyway the error is in the accept call where you passed an int * instead of socklen_t *. in

 newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);

So in this line:

int sockfd, newsockfd, portno, clilen;

change the declaration of clilen to

socklen_t clilen;

and it'll work.

user207421
  • 305,947
  • 44
  • 307
  • 483
madz
  • 1,803
  • 18
  • 45