开发者

Dereferencing error in client socket code

Till yday my code was running fine, but today I am getting client.c:61:25: error: dereferencing pointer to incomplete type client.c:63:16: error: dereferencing pointer to incomplete type

The lines have been written below again. Any kind of help would be appreciated.

               #include <stdio.h>
               #include <stdlib.h>
               #include <string.h>
               #include <math.h>
               #include <sys/types.h> 
               #include <sys/socket.h>
               #include <netinet/in.h>
               #define MAXPROFILES  2

              int main(int argc, char *argv[])
 开发者_StackOverflow         {
              int sockfd, portno, n;
              struct sockaddr_in serv_addr;
              struct hostent *server;
              unsigned char buf[1024];

              struct profile_t 
           {
              unsigned char length;
              unsigned char type;
              unsigned char *data;
           };

              typedef struct profile_datagram_t
          {
             unsigned char src[4];
             unsigned char dst[4];
             unsigned char ver;
             unsigned char n;
             struct profile_t profiles[MAXPROFILES];    
          } header;

             header outObj;

             if (argc < 3) {
             fprintf(stderr,"usage: %s hostname port\n", argv[0]);
             exit(0);
          }
             portno = atoi(argv[2]); //Convert ASCII to integer
             sockfd = socket(AF_INET, SOCK_STREAM, 0); // socket file descriptor


             if (sockfd < 0) 
             error("ERROR DETECTED !!! Problem in opening socket\n");

             server = gethostbyname(argv[1]);
             if (server == NULL) {
             fprintf(stderr,"ERROR DETECTED !!!, no such server found \n");
             exit(0);
           }

            bzero((char *) &serv_addr, sizeof(serv_addr)); //clear the memory for server address

            serv_addr.sin_family = AF_INET;    
            bcopy((char *)server->h_addr, 
                   (char *)&serv_addr.sin_addr.s_addr,
                                    server->h_length);

            serv_addr.sin_port = htons(portno);

            printf("Client 1 trying to connect with server host %s on port %d\n", argv[1], portno); 


            if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
            error("ERROR in connection");

            printf("SUCCESS !!! Connection established \n");

The error is in lines:

                          *bcopy((char *)server->h_addr, 
                                  (char *)&serv_addr.sin_addr.s_addr,
                                    server->h_length);*


You haven't included the declaration of gethostbyname:

#include <netdb.h>

compile with -Wall

warning: implicit declaration of function ‘gethostbyname’

About the error message: the compiler basically tells you that it doesn't know how to access the field of the struct because it never saw the definition of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜