Socket Setup and program in C- help

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Re: Socket Setup and program in C- help

    In article <5d7d3bea.0307110528.24e72923@posting.google.com >,
    thumor <ttejas@yahoo.com> wrote:
    > if ( bind(server_fd,(struct sockaddr
    >*)&remote_addr,sizeof(struct sockaddr_in)) == -1)
    ....
    >I am getting error number 227 : EADDRNOTAVAIL on bind, when I call
    >function
    >OpenPort("MYPORTNAME") in my main().
    >
    When you call bind(), you're specifying the *local* address and port that
    the server should listen on. You're getting EADDRNOTAVAIL because the
    address in remote_addr is a remote address, not one of the server's local
    addresses.
    >If I replace the bind/accept/listen with a connect as follows; I get
    >error
    >number 238 (ETIMEDOUT).
    Connect() is for a client connecting to a server.

    If you're trying to configure it so that *only* this one address can
    connect to the server, I don't think sockets provides any way to do that.
    What you need to do is have your server call getpeername() when a
    connection comes in. If the remote address isn't the allowed client, it
    should then close the connection.

    --
    Barry Margolin, [email]barry.margolin@level3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  2. Similar Questions and Discussions

    1. best setup for school laptop program
      Hi, all. I'm posting here because I know the regulars in this ng are Sysadmin Gods (more flattery available Upon Request). We've got a small school...
    2. PDF and program won't open; "Error in Acrord32" "This program has performed an illegal operation and
      I recently had a browser hijacker. Fixed it with Norton and Lavasoft. Now, after downloading new Reader 6.0, I can't open anything. Only gives...
    3. Page setup/paper setup
      Hi I use Freehand MXa in MacOSX 13.2. I wonder, does Freehands own paper setup (in print dialog box) owerride all paper setup settings.(In...
    4. Socket.accept problem via Socket.for_fd($stdin.fileno)
      Hi, I am experiencing a rather infuriating problem with Socket.accept on Windows XP. The problem exists when I try to create a Socket from...
    5. Distinguishing between socket buffer full & socket disconnected
      I am using the IO::Select method can_write() to flow control the writing of a large amount of data to a socket, where the writer may well run ahead...
  3. #2

    Default Re: Socket Setup and program in C- help

    Barry Margolin <barry.margolin@level3.com> wrote in message news:<eXzPa.20$0z4.4@news.level3.com>...
    > In article <5d7d3bea.0307110528.24e72923@posting.google.com >,
    > thumor <ttejas@yahoo.com> wrote:
    > > if ( bind(server_fd,(struct sockaddr
    > >*)&remote_addr,sizeof(struct sockaddr_in)) == -1)
    > ...
    > >I am getting error number 227 : EADDRNOTAVAIL on bind, when I call
    > >function
    > >OpenPort("MYPORTNAME") in my main().
    > >
    >
    > When you call bind(), you're specifying the *local* address and port that
    > the server should listen on. You're getting EADDRNOTAVAIL because the
    > address in remote_addr is a remote address, not one of the server's local
    > addresses.
    >
    That was great help. I used following to correct that:

    /* Veriables defined */

    struct sockaddr_in local_addr;
    int addrlen;

    /* Initialize variables */
    memset( (char *)&local_addr, 0, sizeof(struct sockaddr_in));

    /* Placed this before bind */

    if ( getsockaddr(server_fd, &local_addr, &addrlen) == -1)
    {
    Log_message("Error (%d) on getsockaddr",errno);
    }
    else
    {
    Log_message("Got local address");
    }

    /* Changed bind as above to use &local_addr */


    -- That worked for bind. listen() went fine (no errors).

    Then it seems I never get past accept().
    Do I use local_addr or remote_addr for accept() ?
    Seems like nothing happens after listen().


    > >If I replace the bind/accept/listen with a connect as follows; I get
    > >error
    > >number 238 (ETIMEDOUT).
    >
    > Connect() is for a client connecting to a server.
    >
    > If you're trying to configure it so that *only* this one address can
    > connect to the server, I don't think sockets provides any way to do that.
    > What you need to do is have your server call getpeername() when a
    > connection comes in. If the remote address isn't the allowed client, it
    > should then close the connection.
    thumor Guest

  4. #3

    Default Re: Socket Setup and program in C- help

    In article <5d7d3bea.0307140757.43c3b92b@posting.google.com >,
    thumor <ttejas@yahoo.com> wrote:
    >Barry Margolin <barry.margolin@level3.com> wrote in message
    >news:<eXzPa.20$0z4.4@news.level3.com>...
    >> In article <5d7d3bea.0307110528.24e72923@posting.google.com >,
    >> thumor <ttejas@yahoo.com> wrote:
    >> > if ( bind(server_fd,(struct sockaddr
    >> >*)&remote_addr,sizeof(struct sockaddr_in)) == -1)
    >> ...
    >> >I am getting error number 227 : EADDRNOTAVAIL on bind, when I call
    >> >function
    >> >OpenPort("MYPORTNAME") in my main().
    >> >
    >>
    >> When you call bind(), you're specifying the *local* address and port that
    >> the server should listen on. You're getting EADDRNOTAVAIL because the
    >> address in remote_addr is a remote address, not one of the server's local
    >> addresses.
    >>
    >
    >That was great help. I used following to correct that:
    >
    >/* Veriables defined */
    >
    >struct sockaddr_in local_addr;
    >int addrlen;
    >
    >/* Initialize variables */
    >memset( (char *)&local_addr, 0, sizeof(struct sockaddr_in));
    >
    >/* Placed this before bind */
    >
    >if ( getsockaddr(server_fd, &local_addr, &addrlen) == -1)
    >{
    > Log_message("Error (%d) on getsockaddr",errno);
    >}
    >else
    >{
    > Log_message("Got local address");
    >}
    Why are you doing this? Calling bind() is what sets the socket's local
    address. What's the purpose of calling getsockaddr before that?
    >/* Changed bind as above to use &local_addr */
    I'm not sure what got put into local_addr by the above call.

    Where did you put that call in relation to the code that fills in the port
    number?
    >-- That worked for bind. listen() went fine (no errors).
    >
    >Then it seems I never get past accept().
    >Do I use local_addr or remote_addr for accept() ?
    Accept() fills in a structure with the client's address.

    I think you need to get a copy of "Unix Network Programming, Vol.1".

    --
    Barry Margolin, [email]barry.margolin@level3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  5. #4

    Default Re: Socket Setup and program in C- help

    Barry Margolin <barry.margolin@level3.com> wrote in message news:<pJDQa.148$0z4.88@news.level3.com>...
    > In article <5d7d3bea.0307140757.43c3b92b@posting.google.com >,
    > thumor <ttejas@yahoo.com> wrote:
    > >Barry Margolin <barry.margolin@level3.com> wrote in message
    > >news:<eXzPa.20$0z4.4@news.level3.com>...
    > >> In article <5d7d3bea.0307110528.24e72923@posting.google.com >,
    > >> thumor <ttejas@yahoo.com> wrote:
    > >> > if ( bind(server_fd,(struct sockaddr
    > >> >*)&remote_addr,sizeof(struct sockaddr_in)) == -1)
    > ...
    > >> >I am getting error number 227 : EADDRNOTAVAIL on bind, when I call
    > >> >function
    > >> >OpenPort("MYPORTNAME") in my main().
    > >> >
    > >>
    > >> When you call bind(), you're specifying the *local* address and port that
    > >> the server should listen on. You're getting EADDRNOTAVAIL because the
    > >> address in remote_addr is a remote address, not one of the server's local
    > >> addresses.
    > >>
    > >
    > >That was great help. I used following to correct that:
    > >
    > >/* Veriables defined */
    > >
    > >struct sockaddr_in local_addr;
    > >int addrlen;
    > >
    > >/* Initialize variables */
    > >memset( (char *)&local_addr, 0, sizeof(struct sockaddr_in));
    > >
    > >/* Placed this before bind */
    > >
    > >if ( getsockaddr(server_fd, &local_addr, &addrlen) == -1)
    > >{
    > > Log_message("Error (%d) on getsockaddr",errno);
    > >}
    > >else
    > >{
    > > Log_message("Got local address");
    > >}
    >
    > Why are you doing this? Calling bind() is what sets the socket's local
    > address. What's the purpose of calling getsockaddr before that?
    >
    > >/* Changed bind as above to use &local_addr */
    >
    > I'm not sure what got put into local_addr by the above call.
    >
    > Where did you put that call in relation to the code that fills in the port
    > number?
    >
    > >-- That worked for bind. listen() went fine (no errors).
    > >
    > >Then it seems I never get past accept().
    > >Do I use local_addr or remote_addr for accept() ?
    >
    > Accept() fills in a structure with the client's address.
    >
    > I think you need to get a copy of "Unix Network Programming, Vol.1".
    Whether I use getsockaddr() or not, the bottom line is the same,
    my accept() never returns (using local_addr).

    Is it that the accept() will come back ONLY when the client-side issues a
    *connect* on the port; giving me the *new* socket address (nportFD) to
    work with?
    thumor Guest

  6. #5

    Default Re: Socket Setup and program in C- help

    In article <5d7d3bea.0307160551.9d886fc@posting.google.com> ,
    thumor <ttejas@yahoo.com> wrote:
    >Is it that the accept() will come back ONLY when the client-side issues a
    >*connect* on the port; giving me the *new* socket address (nportFD) to
    >work with?
    Yes. The purpose of accept() is to wait for a connection, and when it
    comes in it returns the FD for that new connection to the server.

    --
    Barry Margolin, [email]barry.margolin@level3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139