0

I am new to socket programming and trying to figure out how to do a simple connection between client and server.

I found a website which explains about it. http://www.tutorialspoint.com/unix_sockets/socket_client_example.htm

I tried executing the codes in my ubuntu terminal ( gcc -o client client.c)

However it prints out the following error messages

client.c In function 'main' :
client.c:32:12: warning: assignment makes pointer from integer without a cast[enabled by defaulted]
client.c:40:25: error : dereferencing pointer to incomplete type
client.c:40:23: error : dereferencing pointer to incomplete type
client.c 46:5: warning: passing argument 2 of 'connect' from incompatible pointer type [enabled by default]

In file included from client c:1:0:
/usr/include/x86_64-linux-gnu/sys/socket.h:138:12: note:expected 'const struct sockaddr *' but argument is of type 'stuct sockaddr_in'

client.c code from the website

#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];

    if (argc < 3) {
        fprintf(stderr,"usage %s hostname port\n", argv[0]);
        exit(0);
    }
    portno = atoi(argv[2]);
    /* Create a socket point */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
    {
        perror("ERROR opening socket");
       exit(1);
    }

   //warning: assignment makes pointer from integer without a cast[enabled by defaulted]
   server = gethostbyname(argv[1]);

   if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
   }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;

    //dereferencing pointer to incomplete type
    bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
    serv_addr.sin_port = htons(portno);

    /* Now connect to the server */
    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
    {
         perror("ERROR connecting");
         exit(1);
    }   
    /* Now ask for a message from the user, this message
    * will be read by server
    */
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    /* Send message to the server */
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
    {
         perror("ERROR writing to socket");
         exit(1);
    }
    /* Now read server response */
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
   if (n < 0) 
    {
         perror("ERROR reading from socket");
         exit(1);
    }
    printf("%s\n",buffer);
    return 0;
}

I am kind of lost and need help. thanks in advance

7
  • dereferencing pointer to incomplete type, means you a missing some header file that you need to include. Commented Nov 14, 2013 at 5:03
  • maybe you would like to share what header files have I actually missed out? Commented Nov 14, 2013 at 5:10
  • see what type you use on the line specified. Commented Nov 14, 2013 at 5:12
  • What can help is if you comment in your code and say "here is where <Warning_Text> appears", or highlight the line number in your code. Commented Nov 14, 2013 at 5:18
  • 2
    You're missing at least the following include files, and the author of that code should be throttled for posting it as-is. <stdlib.h> , <string.h>, <unistd.h>, and <netdb.h> Commented Nov 14, 2013 at 5:20

1 Answer 1

2

Not really a full answer, but my advice is to compile with more warnings turned on (e.g. with -Wall):

$ gcc -Wall -o client client.c
client.c: In function `main':
client.c:16: warning: implicit declaration of function `exit'
client.c:18: warning: implicit declaration of function `atoi'
client.c:26: warning: implicit declaration of function `gethostbyname'
client.c:26: warning: assignment makes pointer from integer without a cast
client.c:32: warning: implicit declaration of function `bzero'
client.c:34: warning: implicit declaration of function `bcopy'
client.c:34: error: dereferencing pointer to incomplete type
client.c:34: error: dereferencing pointer to incomplete type
client.c:38: warning: passing arg 2 of `connect' from incompatible pointer type
client.c:50: warning: implicit declaration of function `write'
client.c:50: warning: implicit declaration of function `strlen'
client.c:58: warning: implicit declaration of function `read'
$ 

This makes a lot of the problems much clearer.

For all the implicit declaration of function warnings, just look up the relevant manpage to find what header you need:

$ man -s3 exit | grep '#include'
       #include <stdlib.h>
$ man -s2 write | grep '#include'
       #include <unistd.h>
$ man -s3 gethostbyname | grep '#include'
       #include <netdb.h>
       #include <sys/socket.h>       /* for AF_INET */
$ 

In general, these types of calls will be in section 2 or 3 of the manpages, but you might have to hunt around a bit.

$ man man | grep calls
       2   System calls (functions provided by the kernel)
       3   Library calls (functions within program libraries)
$
Sign up to request clarification or add additional context in comments.

1 Comment

I suggest compiling with -Wall as well as -Wextra. You can look into pedantic too, but I think that only works with -ansi.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.