Thread: bind() system call

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    bind() system call

    hey ive copied an example from a tutorial
    and i wanted to know why this is working
    but putting an error on bind? (the error
    is "invalid argument")
    Code:
        #include <string.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
    
        #define MYPORT 3490    // the port users will be connecting to
    
        #define BACKLOG 2     // how many pending connections queue will hold
    
        int main()
        {
            int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
            struct sockaddr_in my_addr;    // my address information
            struct sockaddr_in their_addr; // connector's address information
            int sin_size;
    
            sockfd = socket(AF_INET, SOCK_STREAM, 0); // do some error checking!
    
            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; // auto-fill with my IP
            memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
    
            // don't forget your error checking for these calls:
            bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
    	if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
    		perror("bind");
    
            listen(sockfd, BACKLOG);
    	if(listen(sockfd, BACKLOG) == -1)
    		perror("listen");
    
            sin_size = sizeof(struct sockaddr_in);
            new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    	if(accept(sockfd, (struct sockaddr *)&their_addr, &sin_size) == -1)
    		perror("accept");
    	return 0;
    }
    how would i rewrite this to see who is loggin on, when someone connects?
    thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why have you doubled up all the system calls? You have two calls to bind(), two to listen() etc etc
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    oh right thanks! fixed that..the error is gone.

    but what have i to add if i want to see when someones logges on?

    cya

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read the rest of Beej's guide

    printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i will

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Replies: 6
    Last Post: 07-21-2008, 03:10 AM
  3. Inline asm
    By brietje698 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2007, 02:54 PM
  4. nanosleep() -system call does some confusing things
    By jtk in forum Linux Programming
    Replies: 5
    Last Post: 08-30-2007, 04:15 AM
  5. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM