bind() and SIGSEGV when usign UDP

Discussion in 'C++' started by USUN_TO, Jul 30, 2005.

  1. USUN_TO

    USUN_TO Guest

    Hi, i got problem

    when i bind in this way:

    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    local_addr.sin_port = htons(CLIENT_PORT);

    i can easly bind port without any SIGSEGV (segmentation fault) but MSDN
    wrote:

    "If an application does not care what local address is assigned, specify
    the manifest constant value ADDR_ANY for the sa_data member of the name
    parameter. This allows the underlying service provider to use any
    appropriate network address, potentially simplifying application
    programming in the presence of multihomed hosts (that is, hosts that
    have more than one network interface and address)."

    so how write this? What is diference betwean ADDR_ANY and INADDR_ANY?

    Thiese lines are wrong cause makes SIGSEGV:

    local_addr.sin_addr.s_addr = htonl(ADDR_ANY);
    local_addr.sin_addr.s_addr = htons(ADDR_ANY);
    local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    local_addr.sin_addr.s_addr = htons(INADDR_ANY);
    local_addr.sin_addr.s_addr = INADDR_ANY;
    local_addr.sin_addr.s_addr = ADDR_ANY;

    Plz help me...
    USUN_TO, Jul 30, 2005
    #1
    1. Advertising

  2. USUN_TO

    red floyd Guest

    USUN_TO wrote:
    > [redacted]


    You're OT here. Ask in comp.unix.programmer.
    red floyd, Jul 30, 2005
    #2
    1. Advertising

  3. USUN_TO wrote:
    > Hi, i got problem
    >
    > when i bind in this way:
    >
    > local_addr.sin_family = AF_INET;
    > local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    > local_addr.sin_port = htons(CLIENT_PORT);
    >
    > i can easly bind port without any SIGSEGV (segmentation fault) but MSDN
    > wrote:
    >
    > "If an application does not care what local address is assigned, specify
    > the manifest constant value ADDR_ANY for the sa_data member of the name
    > parameter. This allows the underlying service provider to use any
    > appropriate network address, potentially simplifying application
    > programming in the presence of multihomed hosts (that is, hosts that
    > have more than one network interface and address)."
    >
    > so how write this? What is diference betwean ADDR_ANY and INADDR_ANY?
    >
    > Thiese lines are wrong cause makes SIGSEGV:
    >
    > local_addr.sin_addr.s_addr = htonl(ADDR_ANY);
    > local_addr.sin_addr.s_addr = htons(ADDR_ANY);
    > local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    > local_addr.sin_addr.s_addr = htons(INADDR_ANY);
    > local_addr.sin_addr.s_addr = INADDR_ANY;
    > local_addr.sin_addr.s_addr = ADDR_ANY;
    >
    > Plz help me...


    Your posting appears to be from a Windows machine.
    If you're asking about the bind() found in the Windows
    Winsock lib ("Windows Sockets 2"), then you might get help
    in a Windows newsgroup. If you're asking about the socket
    bind() in Unix or Linux networking, then you'll have to
    ask for help in a newsgroup for the appropriate OS.

    Regards,
    Larry
    Larry I Smith, Jul 30, 2005
    #3
  4. USUN_TO wrote:
    > Hi, i got problem
    >
    > when i bind in this way:
    >
    > local_addr.sin_family = AF_INET;
    > local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    > local_addr.sin_port = htons(CLIENT_PORT);
    >
    > i can easly bind port without any SIGSEGV (segmentation fault) but MSDN
    > wrote:
    >
    > "If an application does not care what local address is assigned, specify
    > the manifest constant value ADDR_ANY for the sa_data member of the name
    > parameter. This allows the underlying service provider to use any
    > appropriate network address, potentially simplifying application
    > programming in the presence of multihomed hosts (that is, hosts that
    > have more than one network interface and address)."
    >
    > so how write this? What is diference betwean ADDR_ANY and INADDR_ANY?
    >
    > Thiese lines are wrong cause makes SIGSEGV:
    >
    > local_addr.sin_addr.s_addr = htonl(ADDR_ANY);
    > local_addr.sin_addr.s_addr = htons(ADDR_ANY);
    > local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    > local_addr.sin_addr.s_addr = htons(INADDR_ANY);
    > local_addr.sin_addr.s_addr = INADDR_ANY;
    > local_addr.sin_addr.s_addr = ADDR_ANY;
    >
    > Plz help me...



    Below is a function extracted from one of my older C++
    libs for Windows. Ignore the calls to IsOpen() and Close(),
    they reside elsewhere, but the code may still help you
    find your problem. Remember to add '#include <winsock.h>'
    in your code.


    int
    ServerIpSocket::BindToPort(int port, int queueSize)
    {
    SOCKADDR_IN serverSockAddr;
    int status;

    // if already associated with a port, close
    if (IsOpen())
    Close();

    /* MANDATORY - zero the sockaddr_in structure */
    memset (&serverSockAddr, 0, sizeof (serverSockAddr));

    /* specify the port portion of the address */
    serverSockAddr.sin_port = htons ((u_short)port);

    /* specify the address family as Internet */
    serverSockAddr.sin_family = AF_INET;

    /* specify that the address does not matter */
    serverSockAddr.sin_addr.s_addr = htonl (INADDR_ANY);

    /* create a socket */
    theSocket = socket (AF_INET, SOCK_STREAM, 0);
    if (theSocket == INVALID_SOCKET)
    {
    cerr << "ERROR: socket unsuccessful"
    << endl;

    return (-1);
    }

    /* associate the socket with the address */
    status = bind (theSocket,
    (LPSOCKADDR) & serverSockAddr,
    sizeof (serverSockAddr));
    if (status == SOCKET_ERROR)
    {
    cerr << "ERROR: bind unsuccessful" << endl;

    // close theSocket opened by the above socket() call
    Close();

    return (-1);
    }

    // allow the socket to take connections.
    // specify a waiting connection queue size
    status = listen (theSocket, queueSize);
    if (status == SOCKET_ERROR)
    {
    cerr << "ERROR: listen unsuccessful" << endl;

    // close theSocket opened by the above socket() call
    Close();

    return (-1);
    }

    // NOTE: For this socket class, we are NEVER connected to a client.
    // ConnectToClient() will create/return a NEW socket to handle
    // each client connection. Therefore we NEVER set "isConn" to
    // non-zero for this class (ie: IsConnected() always returns
    // zero)

    return 0;
    }

    Regards,
    Larry
    Larry I Smith, Jul 30, 2005
    #4
  5. USUN_TO

    USUN_TO Guest

    Larry I Smith napisał(a):
    > USUN_TO wrote:
    >


    Thx a lot Larry but the same code under linux works fine and the same
    (of course changing to WinSock spec. ) when i bind UDP i got SIGSEGV
    (segmentation fault) but when i continue program works fine (not always
    stable - so i tracking errors)
    USUN_TO, Jul 31, 2005
    #5
  6. USUN_TO wrote:
    > Larry I Smith napisał(a):
    >> USUN_TO wrote:
    >>

    >
    > Thx a lot Larry but the same code under linux works fine and the same
    > (of course changing to WinSock spec. ) when i bind UDP i got SIGSEGV
    > (segmentation fault) but when i continue program works fine (not always
    > stable - so i tracking errors)


    Is the return value of bind() WSAEACCES?
    If it is, then (from the MSDN ref for Winsock), this might be
    the problem:

    <quote>
    Attempt to connect datagram socket to broadcast address failed
    because setsockopt() option SO_BROADCAST is not enabled.
    </quote>

    To use datagrams you have to enable SO_BROADCAST by calling
    setsocketopt() before doing the bind().
    See the MSDN Winsock docs for setsocketopt() for details.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/setsockopt_2.asp

    Regards,
    Larry
    Larry I Smith, Jul 31, 2005
    #6
  7. USUN_TO

    red floyd Guest

    OT: bind() and SIGSEGV when usign UDP

    USUN_TO wrote:
    > Larry I Smith napisał(a):
    >
    >> USUN_TO wrote:
    >>

    >
    > Thx a lot Larry but the same code under linux works fine and the same
    > (of course changing to WinSock spec. ) when i bind UDP i got SIGSEGV
    > (segmentation fault) but when i continue program works fine (not always
    > stable - so i tracking errors)


    This is OT, but...

    The other thing under Winsock is to make damn sure you call WSAStartup.
    red floyd, Jul 31, 2005
    #7
  8. USUN_TO

    USUN_TO Guest

    Re: OT: bind() and SIGSEGV when usign UDP

    > The other thing under Winsock is to make damn sure you call WSAStartup.

    I use Bcc32 5.5 and Newes MiniGW (from Dev-Cpp 4.9.9.2 with update) and
    got thesame SIGSEGV :

    http://republika.pl/aao_auth/UDP.rar

    there is the source

    and plz use GDB as debugger
    USUN_TO, Jul 31, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry Camel

    Usign an OCX in an ASP .NET page

    Jerry Camel, Sep 14, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    6,427
    ankit
    Oct 17, 2008
  2. Neil Daniell
    Replies:
    0
    Views:
    548
    Neil Daniell
    Jan 4, 2005
  3. Replies:
    0
    Views:
    770
  4. George Ter-Saakov

    usign Skype from .NET

    George Ter-Saakov, Apr 24, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    561
    Juan T. Llibre
    Apr 25, 2008
  5. LC
    Replies:
    9
    Views:
    594
    Mike Treseler
    Nov 12, 2009
Loading...

Share This Page