Results 1 to 3 of 3

Thread: C Programming Sockets.

Hybrid View

  1. #1
    Junior Member
    Join Date
    Feb 2004
    Posts
    72

    C Programming Sockets.

    I cerated a simple server.c program in class. it worked fine.

    When compiling i had to do gcc server.c -o server -lsocket. It worked fine on the Sun Microsystems in the class.

    When i got home i tried to compile the same program on Ubuntu. When compiling i get the following error:

    gcc server.c -o server
    server.c: In function ‘main’:
    server.c:11: error: storage size of ‘servaddr’ isn’t known
    server.c:11: error: storage size of ‘cliaddr’ isn’t known
    server.c:15: warning: incompatible implicit declaration of built-in function ‘bzero’
    server.c:17: error: ‘INADDR_ANY’ undeclared (first use in this function)
    server.c:17: error: (Each undeclared identifier is reported only once
    server.c:17: error: for each function it appears in.)

    Any help would be appreciated (I also tried to do gcc server.c -o server -lsocket, with the same errors.

  2. #2
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re:C Programming Sockets.

    I would like to know what kind of source code you're using, since on Linux any socket using program that includes netinet/in.h and sys/socket.h will be able to compile without any linker flags.
    Considder these two small programs:

    Listener:
    Code:
    /*
    ** listener.c -- a datagram sockets "server" demo
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <sys/wait.h>
    
    #define MYPORT 4950    /* the port users will be connecting to */
    
    #define MAXBUFLEN 100
    
    main()
    {
        int sockfd;
        struct sockaddr_in my_addr;    /* my address information */
        struct sockaddr_in their_addr; /* connector's address information */
        int addr_len, numbytes;
        char buf[MAXBUFLEN];
    
        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }
    
        my_addr.sin_family = AF_INET;         /* host byte order */
        my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
        my_addr.sin_addr.s_addr = INADDR_ANY; /* automatically fill with my IP */
        bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */
    
        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
            perror("bind");
            exit(1);
        }
    
        addr_len = sizeof(struct sockaddr);
        if ((numbytes=recvfrom(sockfd,buf,MAXBUFLEN,0,(struct sockaddr *)&their_addr, &addr_len)) == -1) {
            perror("recvfrom");
            exit(1);
        }
    
        printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
        printf("packet is %d bytes long\n",numbytes);
        buf[numbytes] = '\0';
        printf("packet contains \"%s\"\n",buf);
    
        close(sockfd);
    }
    talker:
    Code:
    /*
    ** talker.c -- a datagram "client" demo
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <sys/socket.h>
    #include <sys/wait.h>
    
    #define MYPORT 4950    /* the port users will be connecting to */
    
    int main(int argc, char *argv[])
    {
        int sockfd;
        struct sockaddr_in their_addr; /* connector's address information */
        struct hostent *he;
        int numbytes;
    
        if (argc != 3) {
            fprintf(stderr,"usage: talker hostname message\n");
            exit(1);
        }
    
        if ((he=gethostbyname(argv[1])) == NULL) {  /* get the host info */
            perror("gethostbyname");
            exit(1);
        }
    
        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }
    
        their_addr.sin_family = AF_INET;         /* host byte order */
        their_addr.sin_port = htons(MYPORT);     /* short, network byte order */
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        bzero(&(their_addr.sin_zero), 8);        /* zero the rest of the struct */
    
        if ((numbytes=sendto(sockfd,argv[2],strlen(argv[2]),0,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
            perror("recvfrom");
            exit(1);
        }
        printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
        close(sockfd);
    
        return 0;
    }
    Which can easily be compiled with
    ~ > gcc -o program file.c

  3. #3
    Junior Member
    Join Date
    Feb 2004
    Posts
    72

    Re:C Programming Sockets.

    Thanx for that. I just needed to add #include <netinet/in.h>

    Is now working great.

    Thanx again

Similar Threads

  1. Programming
    By imported_n00b in forum Announcements and Suggestions
    Replies: 12
    Last Post: 09-17-2004, 06:35 PM
  2. Programming
    By Asbenson in forum General Chat
    Replies: 34
    Last Post: 06-19-2004, 04:54 AM
  3. SB128 programming
    By Henkalex in forum Programming
    Replies: 3
    Last Post: 10-21-2002, 02:52 PM
  4. Learning programming
    By trickster in forum Programming
    Replies: 2
    Last Post: 09-19-2002, 10:40 AM
  5. Programming challenges
    By ndogg in forum Programming
    Replies: 21
    Last Post: 03-01-2002, 09:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •