Sockets...bind() fails.. - VxWorks

This is a discussion on Sockets...bind() fails.. - VxWorks ; Hi following is the code snippet, The socket is crerated , ip can be read and sSrv_addr is populated properly but bind just fails. I can only know tht it fails and gives ERROR but how can i know wht ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Sockets...bind() fails..

  1. Sockets...bind() fails..

    Hi following is the code snippet,

    The socket is crerated , ip can be read and sSrv_addr is populated
    properly but bind just fails. I can only know tht it fails and gives
    ERROR but how can i know wht is wrong in Socket/Address/Len which are
    provided as parameters.


    /* Get a handle for a new TCP socket */
    if ( ( lSocket = platformSocket( PF_INET, SOCK_STREAM, 0 ) ) ==
    INVALID_SOCKET )
    {
    return;
    }

    /* Configure the socket to listen to any potential client coming
    on the Ethernet/IP TCP port */
    memset( ( char * )&sSrv_addr, 0, sizeof( sSrv_addr ) );
    sSrv_addr.sin_family = AF_INET;
    sSrv_addr.sin_port = htons( ENCAP_SERVER_PORT );
    sSrv_addr.sin_len = sizeof(struct sockaddr);

    platformGetInternetAddress(sSrv_addr.sin_addr) =
    htonl(lClaimedHostIPAddr);
    /* IPAddress is correctly read*/


    /* The socket may be bound to an address which is already in use
    */
    status = platformSetSockOpt
    ( lSocket, SOL_SOCKET, SO_REUSEADDR, ( char * )
    &lFlagTrue, sizeof( lFlagTrue ) );
    if ( status == SOCKET_ERROR )
    {
    return
    }

    /* Keepalives are being sent*/
    status = platformSetSockOpt
    ( lSocket, SOL_SOCKET, SO_KEEPALIVE, ( char * )
    &lFlagTrue, sizeof( lFlagTrue ) );
    if ( status == SOCKET_ERROR )
    return;

    /* Get a size buffer at least large enough for a full packet, and
    also
    keep it a power of 2. Keep it at least 4k in size */
    lSize_buf = MAX_PACKET_SIZE;
    status = platformSetSockBufSize
    ( lSocket, SOL_SOCKET, SO_SNDBUF, ( char * )
    &lSize_buf, sizeof( lSize_buf ) );
    if ( status == SOCKET_ERROR )
    {
    return;
    }

    lSize_buf = MAX_PACKET_SIZE;
    status = platformSetSockBufSize
    ( lSocket, SOL_SOCKET, SO_RCVBUF, ( char * )
    &lSize_buf, sizeof( lSize_buf ) );
    if ( status == SOCKET_ERROR )
    {
    return;
    }

    /* Bind the server to Ethernet/IP TCP port */
    if ( platformBind( lSocket, ( struct sockaddr * ) & sSrv_addr,
    sizeof( sSrv_addr ) ) == SOCKET_ERROR )
    {
    return; /* Bind Fails here */
    }


    Regards,
    Asad

  2. Re: Sockets...bind() fails..

    On Jan 2, 9:12 pm, "asadullah.k...@in.abb.com"
    wrote:
    > Hi following is the code snippet,
    >
    > The socket is crerated , ip can be read and sSrv_addr is populated
    > properly but bind just fails. I can only know tht it fails and gives
    > ERROR but how can i know wht is wrong in Socket/Address/Len which are
    > provided as parameters.
    >
    > /* Get a handle for a new TCP socket */
    > if ( ( lSocket = platformSocket( PF_INET, SOCK_STREAM, 0 ) ) ==
    > INVALID_SOCKET )
    > {
    > return;
    > }
    >
    > /* Configure the socket to listen to any potential client coming
    > on the Ethernet/IP TCP port */
    > memset( ( char * )&sSrv_addr, 0, sizeof( sSrv_addr ) );
    > sSrv_addr.sin_family = AF_INET;
    > sSrv_addr.sin_port = htons( ENCAP_SERVER_PORT );
    > sSrv_addr.sin_len = sizeof(struct sockaddr);
    >
    > platformGetInternetAddress(sSrv_addr.sin_addr) =
    > htonl(lClaimedHostIPAddr);
    > /* IPAddress is correctly read*/
    >
    > /* The socket may be bound to an address which is already in use
    > */
    > status = platformSetSockOpt
    > ( lSocket, SOL_SOCKET, SO_REUSEADDR, ( char * )
    > &lFlagTrue, sizeof( lFlagTrue ) );
    > if ( status == SOCKET_ERROR )
    > {
    > return
    > }
    >
    > /* Keepalives are being sent*/
    > status = platformSetSockOpt
    > ( lSocket, SOL_SOCKET, SO_KEEPALIVE, ( char * )
    > &lFlagTrue, sizeof( lFlagTrue ) );
    > if ( status == SOCKET_ERROR )
    > return;
    >
    > /* Get a size buffer at least large enough for a full packet, and
    > also
    > keep it a power of 2. Keep it at least 4k in size */
    > lSize_buf = MAX_PACKET_SIZE;
    > status = platformSetSockBufSize
    > ( lSocket, SOL_SOCKET, SO_SNDBUF, ( char * )
    > &lSize_buf, sizeof( lSize_buf ) );
    > if ( status == SOCKET_ERROR )
    > {
    > return;
    > }
    >
    > lSize_buf = MAX_PACKET_SIZE;
    > status = platformSetSockBufSize
    > ( lSocket, SOL_SOCKET, SO_RCVBUF, ( char * )
    > &lSize_buf, sizeof( lSize_buf ) );
    > if ( status == SOCKET_ERROR )
    > {
    > return;
    > }
    >
    > /* Bind the server to Ethernet/IP TCP port */
    > if ( platformBind( lSocket, ( struct sockaddr * ) & sSrv_addr,
    > sizeof( sSrv_addr ) ) == SOCKET_ERROR )
    > {
    > return; /* Bind Fails here */
    > }
    >
    > Regards,
    > Asad




    It seems ok,did you check the port is free?? Probably it is binded to
    this server address already.

  3. Re: Sockets...bind() fails..

    Check what errno is, that should point you in the right direction.

    Carl

    Nabendu wrote:
    > On Jan 2, 9:12 pm, "asadullah.k...@in.abb.com"
    > wrote:
    > > Hi following is the code snippet,
    > >
    > > The socket is crerated , ip can be read and sSrv_addr is populated
    > > properly but bind just fails. I can only know tht it fails and gives
    > > ERROR but how can i know wht is wrong in Socket/Address/Len which are
    > > provided as parameters.
    > >
    > > /* Get a handle for a new TCP socket */
    > > if ( ( lSocket = platformSocket( PF_INET, SOCK_STREAM, 0 ) ) ==
    > > INVALID_SOCKET )
    > > {
    > > return;
    > > }
    > >
    > > /* Configure the socket to listen to any potential client coming
    > > on the Ethernet/IP TCP port */
    > > memset( ( char * )&sSrv_addr, 0, sizeof( sSrv_addr ) );
    > > sSrv_addr.sin_family = AF_INET;
    > > sSrv_addr.sin_port = htons( ENCAP_SERVER_PORT );
    > > sSrv_addr.sin_len = sizeof(struct sockaddr);
    > >
    > > platformGetInternetAddress(sSrv_addr.sin_addr) =
    > > htonl(lClaimedHostIPAddr);
    > > /* IPAddress is correctly read*/
    > >
    > > /* The socket may be bound to an address which is already in use
    > > */
    > > status = platformSetSockOpt
    > > ( lSocket, SOL_SOCKET, SO_REUSEADDR, ( char * )
    > > &lFlagTrue, sizeof( lFlagTrue ) );
    > > if ( status == SOCKET_ERROR )
    > > {
    > > return
    > > }
    > >
    > > /* Keepalives are being sent*/
    > > status = platformSetSockOpt
    > > ( lSocket, SOL_SOCKET, SO_KEEPALIVE, ( char * )
    > > &lFlagTrue, sizeof( lFlagTrue ) );
    > > if ( status == SOCKET_ERROR )
    > > return;
    > >
    > > /* Get a size buffer at least large enough for a full packet, and
    > > also
    > > keep it a power of 2. Keep it at least 4k in size */
    > > lSize_buf = MAX_PACKET_SIZE;
    > > status = platformSetSockBufSize
    > > ( lSocket, SOL_SOCKET, SO_SNDBUF, ( char * )
    > > &lSize_buf, sizeof( lSize_buf ) );
    > > if ( status == SOCKET_ERROR )
    > > {
    > > return;
    > > }
    > >
    > > lSize_buf = MAX_PACKET_SIZE;
    > > status = platformSetSockBufSize
    > > ( lSocket, SOL_SOCKET, SO_RCVBUF, ( char * )
    > > &lSize_buf, sizeof( lSize_buf ) );
    > > if ( status == SOCKET_ERROR )
    > > {
    > > return;
    > > }
    > >
    > > /* Bind the server to Ethernet/IP TCP port */
    > > if ( platformBind( lSocket, ( struct sockaddr * ) & sSrv_addr,
    > > sizeof( sSrv_addr ) ) == SOCKET_ERROR )
    > > {
    > > return; /* Bind Fails here */
    > > }
    > >
    > > Regards,
    > > Asad

    >
    >
    >
    > It seems ok,did you check the port is free?? Probably it is binded to
    > this server address already.


  4. Re: Sockets...bind() fails..

    On Jan 3, 1:23*am, Ruzzer wrote:
    > Check what errno is, that should point you in the right direction.
    >
    > Carl
    >
    >
    >
    > Nabendu wrote:
    > > On Jan 2, 9:12 pm, "asadullah.k...@in.abb.com"
    > > wrote:
    > > > Hi following is the code snippet,

    >
    > > > The socket is crerated , ip can be read and *sSrv_addr is populated
    > > > properly but bind just fails. I can only know tht it fails and gives
    > > > ERROR but how can i know wht is wrong in Socket/Address/Len which are
    > > > provided as parameters.

    >
    > > > */* Get a handle for a new TCP socket */
    > > > * * if ( ( lSocket = platformSocket( PF_INET, SOCK_STREAM, 0 ) )==
    > > > INVALID_SOCKET )
    > > > * * {
    > > > * * * * return;
    > > > * * }

    >
    > > > * * /* Configure the socket to listen to any potential client coming
    > > > on the Ethernet/IP TCP port */
    > > > * * memset( ( char * )&sSrv_addr, 0, sizeof( sSrv_addr ) );
    > > > * * * * sSrv_addr.sin_family = AF_INET;
    > > > * * sSrv_addr.sin_port = htons( ENCAP_SERVER_PORT );
    > > > * * * * sSrv_addr.sin_len = sizeof(struct sockaddr);

    >
    > > > * * * * platformGetInternetAddress(sSrv_addr.sin_addr) =
    > > > htonl(lClaimedHostIPAddr);
    > > > * * */* IPAddress is correctly read*/

    >
    > > > * * /* The socket may be bound to an address which is already in use
    > > > */
    > > > * * status = platformSetSockOpt
    > > > * * * * * * * * * ( lSocket, SOL_SOCKET, SO_REUSEADDR, ( char * )
    > > > &lFlagTrue, sizeof( lFlagTrue ) );
    > > > * * if ( status == SOCKET_ERROR )
    > > > * * {
    > > > * * *return
    > > > * * *}

    >
    > > > * * /* Keepalives are being sent*/
    > > > * * status = platformSetSockOpt
    > > > * * * * * * * * *( lSocket, SOL_SOCKET, SO_KEEPALIVE, ( char * )
    > > > &lFlagTrue, sizeof( lFlagTrue ) );
    > > > * * if ( status == SOCKET_ERROR )
    > > > * * * * return;

    >
    > > > * * /* Get a size buffer at least large enough for a full packet, and
    > > > also
    > > > * * * *keep it a power of 2. Keep it at least 4k in size */
    > > > * * lSize_buf = MAX_PACKET_SIZE;
    > > > * * status = platformSetSockBufSize
    > > > * * * * * * * * *( lSocket, SOL_SOCKET, SO_SNDBUF, (char * )
    > > > &lSize_buf, sizeof( lSize_buf ) );
    > > > * * if ( status == SOCKET_ERROR )
    > > > * * {
    > > > * * *return;
    > > > * * }

    >
    > > > * * lSize_buf = MAX_PACKET_SIZE;
    > > > * * status = platformSetSockBufSize
    > > > * * * * * * * * *( lSocket, SOL_SOCKET, SO_RCVBUF, (char * )
    > > > &lSize_buf, sizeof( lSize_buf ) );
    > > > * * if ( status == SOCKET_ERROR )
    > > > * * {
    > > > * * * return;
    > > > * * }

    >
    > > > * * /* Bind the server to Ethernet/IP TCP port */
    > > > * * if ( platformBind( lSocket, ( struct sockaddr * ) & sSrv_addr,
    > > > sizeof( sSrv_addr ) ) == SOCKET_ERROR )
    > > > * * {
    > > > * * * * *return; /* Bind Fails here */
    > > > * * }

    >
    > > > Regards,
    > > > Asad

    >
    > > It seems ok,did you check the port is free?? Probably it is binded to
    > > this server address already.- Hide quoted text -

    >
    > - Show quoted text -


    Hi Carl,

    There is no specific error no. all i get is -1 for ERROR when bind
    fails.
    I want to know how to check whether the server adress is already
    binded?

    Regards,
    Asad

  5. Re: Sockets...bind() fails..

    "asadullah.khan@in.abb.com" wrote:

    >Hi Carl,
    >
    >There is no specific error no. all i get is -1 for ERROR when bind
    >fails.


    You're referring to bind()'s return value but Carl suggested the error
    number. See the documentation for functions such as perror() and
    strerror() and the global variable errno.

    >I want to know how to check whether the server adress is already
    >binded?



    --
    ================================================== ======================
    Michael Kesti | "And like, one and one don't make
    | two, one and one make one."
    mrkesti at hotmail dot com | - The Who, Bargain

  6. Re: Sockets...bind() fails..

    On Jan 3, 10:03*am, "Michael R. Kesti"
    wrote:
    > "asadullah.k...@in.abb.com" wrote:
    > >Hi Carl,

    >
    > >There is no specific error no. all i get is -1 for ERROR when bind
    > >fails.

    >
    > You're referring to bind()'s return value but Carl suggested the error
    > number. *See the documentation for functions such as perror() and
    > strerror() and the global variable errno.
    >
    > >I want to know how to check whether the server adress is already
    > >binded?

    >
    > --
    > ================================================== ======================
    > * * * * * Michael Kesti * * * * * *| *"And like, one and one don't make
    > * * * * * * * * * * * * * * * * * *| * two, one and one make one."
    > * * mrkesti at hotmail dot com * * | * * * * *- The Who,Bargain


    Hi Micheal and Carl,
    Thx for specific details. I am new to socket programing and VXWorks so
    u i hope can understand .

    Coming back to my problem i got the error no as 49 ie EADDRNOTAVAIL.
    I tried to see the n/w interfaces here is the list:

    lo (unit number 0):
    Flags: (0x8069) UP LOOPBACK MULTICAST ARP RUNNING
    Type: SOFTWARE_LOOPBACK
    Internet address: 127.0.0.1
    Netmask 0xff000000 Subnetmask 0xff000000
    Metric is 0
    Maximum Transfer Unit size is 32768
    0 packets received; 0 packets sent
    0 multicast packets received
    0 multicast packets sent
    0 input errors; 0 output errors
    0 collisions; 0 dropped
    motfec (unit number 0):
    Flags: (0x8063) UP BROADCAST MULTICAST ARP RUNNING
    Type: ETHERNET_CSMACD
    Internet address: 172.16.85.33
    Broadcast address: 172.16.87.255
    Netmask 0xffff0000 Subnetmask 0xfffffc00
    Ethernet address is 00:00:23:0d:00:62
    Metric is 0
    Maximum Transfer Unit size is 1500
    0 octets received
    0 octets sent
    0 packets received
    1 packets sent
    0 unicast packets received
    0 unicast packets sent
    0 non-unicast packets received
    1 non-unicast packets sent
    0 input discards
    0 input unknown protocols
    0 input errors
    0 output errors
    0 collisions; 0 dropped
    cpm (unit number 1):
    Flags: (0x8063) UP BROADCAST MULTICAST ARP RUNNING
    Type: ETHERNET_CSMACD
    Internet address: 172.16.82.33
    Broadcast address: 172.16.83.255
    Netmask 0xffff0000 Subnetmask 0xfffffc00
    Ethernet address is 00:00:23:0d:00:63
    Metric is 0
    Maximum Transfer Unit size is 1500
    0 octets received
    0 octets sent
    0 packets received
    1 packets sent
    0 unicast packets received
    1 unicast packets sent
    0 non-unicast packets received
    0 non-unicast packets sent
    0 input discards
    0 input unknown protocols
    0 input errors
    0 output errors
    0 collisions; 0 dropped

    This is still the same even after calling:
    sts = ifAddrSet
    (
    "cpm", /* name of interface to configure, i.e. ei0 */
    "10.140.57.02" /* Internet address to assign to interface */
    );

    at the start up... please let me know ur comments.

    Regards,
    Asad

  7. Re: Sockets...bind() fails..

    Hi,

    run ifShow command and see the interface name. I believe it should
    show "cpm1" as the interface name (1 unit number). So in ifAddrSet you
    should use cpm1 instead of cpm.

    Best Regards
    VKG | Ritsoft Technologies

  8. Re: Sockets...bind() fails..

    On Jan 3, 4:09*pm, VKG Ritsoft Technologies
    wrote:
    > Hi,
    >
    > run ifShow command and see the interface name. I believe it should
    > show "cpm1" as the interface name (1 unit number). So in ifAddrSet you
    > should use cpm1 instead of cpm.
    >
    > Best Regards
    > VKG | Ritsoft Technologies


    Thanks All

    ... I got this sometime back... i was seeing HostTabel Entries and
    using "cpm"

    regards,
    Asad


+ Reply to Thread