Thread: Binding

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    34

    Binding

    hi guys i am trying to bind a name to a socket,

    here is the bind chunk :

    Code:
    /* Bind the socket */
        if (bind(sockfd, (struct sockaddr *)&my_addr, 
    	    sizeof(struct sockaddr)) == -1) {
            perror("bind");
            exit(1);
        }
    I get this error:

    Code:
    bind: Bad file descriptor
    I do not know what the source of the problem could be.
    sockfd is decaled like so:
    Code:
    /* listen on sock_fd, new connection on new_fd*/
    int sockfd, new_fd;
    thank you in advance

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You need to call socket() and set sockfd equal to the return value of the socket() function before you can bind() it.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    34
    wow that was fast,

    thanx again!
    itsme

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    34
    Another question,

    i am using a wrapper function to do the socket(),

    Code:
    int Socket(int family, int type, int protocol)
    {
        int n;
    
        if ( (n = socket(family, type, protocol)) < 0) {
    	perror("socket error");
    	exit(1);
        }
        return(n);
    }
    in the main body i do this:

    Code:
    /* Establish Socket */
    sockfd = Socket(AF_INET, SOCK_STREAM, 0);
    bind gives me error:

    Code:
    bind: Address already in use
    is it because i have 'n' in the function body set to the socket and then i try to set it to something else: 'sockfd' ?


    thank you in advance

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Try using setsockopt() to set the option SO_REUSEADDR after calling socket() and before calling bind():
    Code:
    {
      int opt = 1;
    
      setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
    }

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    34
    thanks again itsme,

    one more thing,

    my server is supposed to send http requests,

    do u know how that works?

    my idea is that i have a tcp connection with the host and i just write "GET /index.html HTTP/1.0" as a string

    does a regular browser do the same as well?

    and to check if a file exists i will use the return value of fopen ( is there a better way? )


    thank you so much

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Hmm, I'm confused here. You're bind()ing a socket which is generally only done when you want to be the server. If that's the case then the remote side, the side that connects to your server, would be the one sending the HTTP request. Then your server would handle the request and send the page.

    If you just want to connect to port 80 of a webserver with your program then you don't need bind(). You only need to use socket() and connect().

    Here's the URL for the HTTP RFC: http://www.faqs.org/rfcs/rfc2616.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boost::shared_ptr and dynamic binding
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 03:50 PM
  2. inet_aton()... Noob needs help with sockets :|
    By Maz in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2005, 04:33 PM
  3. dynamic/static binding
    By faze in forum C++ Programming
    Replies: 7
    Last Post: 07-10-2005, 12:26 PM
  4. dynamic binding
    By freethenet in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-26-2004, 03:31 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM