Linux SystemLinux Systems Programming Tutorial - Unix

This is a discussion on Linux SystemLinux Systems Programming Tutorial - Unix ; does someone konw Linux Systems Programming Tutorial (Part 4- Programming with Sockets) by Chris Brown? One of the best tutorials i ever read. I have one problem with a tcp/ip socket exapamle. first of all it seems not all libarys ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Linux SystemLinux Systems Programming Tutorial

  1. Linux SystemLinux Systems Programming Tutorial

    does someone konw Linux Systems Programming Tutorial

    (Part 4- Programming with Sockets)
    by Chris Brown?


    One of the best tutorials i ever read.


    I have one problem with a tcp/ip socket exapamle.
    first of all it seems not all libarys necesary are #included but after
    adding it compiles perfekt.
    but when executed a "segmetation falt (core dump)" retournes in the shell..

    i could not find wher the mistake is in the code.. and i ame alomost
    sure that ate autor can program very well.
    so i am confused by the fault.

    i compiled it with gcc under ubuntu both the latest versiones..

    does someone know how to fix this?

    the code is:

    __________________________________________________ _____________________________

    1 /* Client for square service */
    2
    3 #include
    4 #include
    5 #include
    6 #include
    7 #include
    8
    9 int tcp_connect(char *host, char *service)
    10 {
    11 struct hostent *host_info;
    12 struct servent *serv_info;
    13 struct sockaddr_in server;
    14 int sock;
    15
    16 /* Create a socket */
    17 sock = socket(AF_INET, SOCK_STREAM, 0);
    18
    19 /* Look up the host's IP address */
    20 host_info = gethostbyname(host);
    21
    22 /* Look up the service's port */
    23 serv_info = getservbyname(service, "tcp");
    24
    25 /* Copy these into a socket address */
    26 server.sin_family = AF_INET;
    27 memcpy((char *)&server.sin_addr, host_info->h_addr,
    host_info->h_length);
    28 server.sin_port = serv_info->s_port;
    29
    30 /* Now make the connection */
    31 connect(sock, (struct sockaddr *)&server, sizeof server);
    32 return sock;
    33 }
    34
    35 int main(int argc, char *argv[])
    36 {
    37 int fdnet, x, y;
    38
    39 /* Connect to service */
    40 fdnet = tcp_connect(argv[1], "square");
    41 printf("fdnet = %d\n", fdnet);
    42
    43 x = 5;
    44 /* Conduct brief dialogue with server */
    45 write(fdnet, &x, sizeof x);
    46 read (fdnet, &y, sizeof y);
    47
    48 printf("%d squared is %d\n", x, y);
    49 }


    __________________________________________________ __________________________________


    thanks


  2. Re: Linux SystemLinux Systems Programming Tutorial

    arsenic <274417503@web.de> wrote:
    > first of all it seems not all libarys necesary are #included but after
    > adding it compiles perfekt.

    declares the prototype for memcpy
    > but when executed a "segmetation falt (core dump)" retournes in the shell..

    This isn't a surprise. Your code does not test if either of the
    functions return without error. gethostbyname() and getservbyname() return
    NULL if an error occured. Insert the necessary tests and your program
    won't SEGFAULT but will print an useful error message [perror()].

    Hints: - Pass the servers name as first commandline argument to your client
    - service square isn't usually defined in /etc/services
    > sure that ate autor can program very well.

    ???

    Regards Ralf
    --
    Ralf Becker ralf@akk.org
    Arbeitskreis Kultur und Kommunikation / Universitaet Karlsruhe
    Paulckeplatz 1 76131 Karlsruhe
    Tel 0721/96403-22 Fax 0721/608-4019
    --------------------

  3. Re: Linux SystemLinux Systems Programming Tutorial

    "Ralf Becker" schrieb im Newsbeitrag
    news:ff391o$q06$1@news2.rz.uni-karlsruhe.de...
    > arsenic <274417503@web.de> wrote:
    >> first of all it seems not all libarys necesary are #included but after
    >> adding it compiles perfekt.

    > declares the prototype for memcpy
    >> but when executed a "segmetation falt (core dump)" retournes in the
    >> shell..

    > This isn't a surprise. Your code does not test if either of the
    > functions return without error. gethostbyname() and getservbyname() return
    > NULL if an error occured. Insert the necessary tests and your program
    > won't SEGFAULT but will print an useful error message [perror()].

    herror(), not perror() as the error number is stored in h_errno, not errno.
    herror() is marked obselete in the man-page, but I don't quite understand
    why, as this and the also marke obsolete hstrerror() would be the only
    functions available to translate a h_errno into a usable error message and
    as per the same man-page gethostbyname() sets h_errno and not errno, so the
    only alternative would be to write a replacement for them...
    getservbyname() doesn't seem to set errno nor h_errno

    Bye, Jojo



+ Reply to Thread