bind

This is a discussion on bind within the Linux Programming forums, part of the Platform Specific Boards category; why is the bind command always return -1 in this program Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> ...

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    bind

    why is the bind command always return -1 in this program
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <sys/types.h>
                                                                                                                  
    int main(void) {
            int sv; /* Socket. */
            struct sockaddr_in info;
            int sinfo;
            const unsigned char ipnum[] = { 123, 2, 3, 4 };
                                                                                                                  
            if((sv = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                    fprintf(stderr, "Couldn't make sockets.\n");
                    return -1;
            }
            memset(&info, 0, sizeof info);
            info.sin_family = AF_INET;
            info.sin_port = htons(9000);
            memcpy(&info.sin_addr.s_addr, &ipnum, 4);
            sinfo = sizeof info;
            if((bind(sv, (struct sockaddr *)&info, sinfo)) == -1) {
                    fprintf(stderr, "Couldn't bind ipv4 to socket.\n");
                    close(sv);
                    return -1;
            }
            system("netstat --unix -p | grep \"test\"");
            close(sv);
            return 0;
    }

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    yeah, i just did that and got ip out of range or something, so I changed to something that started with 127 and it worked. thanx.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Try:

    info.sin_addr.s_addr = inet_addr("127.0.0.1");
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I'm sorry but I have to post this here =(

    but Sebastiani, your quote is soooo awsome!

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bind() fails with WSAEACCES
    By pheres in forum Tech Board
    Replies: 2
    Last Post: 02-24-2009, 12:58 PM
  2. Can I bind a UDP socket to a port, but send to any other port?
    By trillianjedi in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-25-2009, 03:27 PM
  3. What does bind() do, exactly?
    By Hunter2 in forum Networking/Device Communication
    Replies: 9
    Last Post: 07-07-2005, 12:12 PM
  4. Bind();
    By SirCrono6 in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-26-2005, 07:01 PM
  5. bind() error
    By PutoAmo in forum C Programming
    Replies: 5
    Last Post: 05-23-2002, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21