Unix Network Programming Volume 1 - Unix

This is a discussion on Unix Network Programming Volume 1 - Unix ; Hi all, I am new to network programming and am trying to work my way through this book, admittedly i haven't gotten very far. The book uses it's own header file (unp.h) to include lots of the usual headers and ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Unix Network Programming Volume 1

  1. Unix Network Programming Volume 1

    Hi all,

    I am new to network programming and am trying to work my way through
    this book, admittedly i haven't gotten very far.

    The book uses it's own header file (unp.h) to include lots of the
    usual headers and set various constants necessary in many networking
    programs.

    I thought I would try to write the same programs without using the
    author's header file to make sure i understood what was going on.

    I am trying to connect my daytimeclient to the books intro/
    daytimetcpsrv.c

    Unfortunately I get a socket error when trying to connect but no
    problems when compiling.

    Here is my daytimeclient:

    #include /* error handling stuff */
    #include /* stdout, fputs */
    #include /* exit */
    #include /* bzero */
    #include /* read */
    #include /* sockaddr_in{}, htons and other Internet
    defns */
    #include /* sockaddr_in, socket, connect, read */
    #include /* inet_pton */

    #define MAXLINE 4096 /* max text line length */

    int
    main(int argc, char **argv)
    {
    int err; // for errors
    int sockfd, n;
    char recvline[MAXLINE + 1];
    struct sockaddr_in servaddr;

    if (argc != 2)
    {
    printf("Usage: %s \n", argv[0]);
    exit(1);
    }

    // create a socket
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0);
    {
    printf("Socket creation error!\n");
    exit(1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(13); /* daytime server */

    // convert ascii command line arg into proper format
    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0);
    {
    printf("inet_pton error for %s", argv[1]);
    exit(1);
    }

    if (connect(sockfd, (struct sockaddr *) &servaddr,
    sizeof(servaddr)) < 0)
    {
    printf("connect error\n");
    exit(1);
    }

    while ( (n = read(sockfd, recvline, MAXLINE)) > 0)
    {
    recvline[n] = 0; /* null terminate */
    if (fputs(recvline, stdout) == EOF)
    {
    printf("fputs error\n");
    exit(1);
    }
    }
    if (n < 0)
    {
    printf("read error\n");
    exit(1);
    }
    exit(0);
    }


    I compile the above code using:

    $ g++ Mydaytimetcpcli.c

    I fire up the daytimetcpsrv in a terminal and then try connecting with
    my daytimeclient as per:

    $ ./a.out 127.0.0.1

    Output:
    Socket creation error!

    Can someone tell me what I might be doing wrong?

    Am I including the wrong headers? Is there some flag I should be
    using with the compiler? I am lost.

    I am doing this on:
    $ uname -a
    Linux unko 2.6.15-29-386 #1 PREEMPT Mon Sep 24 17:18:25 UTC 2007 i686
    GNU/Linux
    (Ubuntu - Dapper Drake)

    Many thanks,

    Peter.


  2. Re: Unix Network Programming Volume 1

    Peter,

    Remove the semicolon at the end of the below if condition. By the
    semicolon, you are mandating the printf and exit statement. Otherwise
    it is perfect.

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0);

    Rgds,
    Nabeel

    Peter wrote:
    > Hi all,
    >
    > I am new to network programming and am trying to work my way through
    > this book, admittedly i haven't gotten very far.
    >
    > The book uses it's own header file (unp.h) to include lots of the
    > usual headers and set various constants necessary in many networking
    > programs.
    >
    > I thought I would try to write the same programs without using the
    > author's header file to make sure i understood what was going on.
    >
    > I am trying to connect my daytimeclient to the books intro/
    > daytimetcpsrv.c
    >
    > Unfortunately I get a socket error when trying to connect but no
    > problems when compiling.
    >
    > Here is my daytimeclient:
    >
    > #include /* error handling stuff */
    > #include /* stdout, fputs */
    > #include /* exit */
    > #include /* bzero */
    > #include /* read */
    > #include /* sockaddr_in{}, htons and other Internet
    > defns */
    > #include /* sockaddr_in, socket, connect, read */
    > #include /* inet_pton */
    >
    > #define MAXLINE 4096 /* max text line length */
    >
    > int
    > main(int argc, char **argv)
    > {
    > int err; // for errors
    > int sockfd, n;
    > char recvline[MAXLINE + 1];
    > struct sockaddr_in servaddr;
    >
    > if (argc != 2)
    > {
    > printf("Usage: %s \n", argv[0]);
    > exit(1);
    > }
    >
    > // create a socket
    > if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0);
    > {
    > printf("Socket creation error!\n");
    > exit(1);
    > }
    >
    > bzero(&servaddr, sizeof(servaddr));
    > servaddr.sin_family = AF_INET;
    > servaddr.sin_port = htons(13); /* daytime server */
    >
    > // convert ascii command line arg into proper format
    > if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0);
    > {
    > printf("inet_pton error for %s", argv[1]);
    > exit(1);
    > }
    >
    > if (connect(sockfd, (struct sockaddr *) &servaddr,
    > sizeof(servaddr)) < 0)
    > {
    > printf("connect error\n");
    > exit(1);
    > }
    >
    > while ( (n = read(sockfd, recvline, MAXLINE)) > 0)
    > {
    > recvline[n] = 0; /* null terminate */
    > if (fputs(recvline, stdout) == EOF)
    > {
    > printf("fputs error\n");
    > exit(1);
    > }
    > }
    > if (n < 0)
    > {
    > printf("read error\n");
    > exit(1);
    > }
    > exit(0);
    > }
    >
    >
    > I compile the above code using:
    >
    > $ g++ Mydaytimetcpcli.c
    >
    > I fire up the daytimetcpsrv in a terminal and then try connecting with
    > my daytimeclient as per:
    >
    > $ ./a.out 127.0.0.1
    >
    > Output:
    > Socket creation error!
    >
    > Can someone tell me what I might be doing wrong?
    >
    > Am I including the wrong headers? Is there some flag I should be
    > using with the compiler? I am lost.
    >
    > I am doing this on:
    > $ uname -a
    > Linux unko 2.6.15-29-386 #1 PREEMPT Mon Sep 24 17:18:25 UTC 2007 i686
    > GNU/Linux
    > (Ubuntu - Dapper Drake)
    >
    > Many thanks,
    >
    > Peter.



  3. Re: Unix Network Programming Volume 1

    On Nov 3, 7:14 pm, Nabeel wrote:
    > Peter,
    >
    > Remove the semicolon at the end of the below if condition. By the
    > semicolon, you are mandating the printf and exit statement. Otherwise
    > it is perfect.
    >
    > if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0);


    Nabeel,

    Thank you so much. Turns out I had another one at:

    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)

    Must be addicted to them.

    Thanks again,

    Peter.



+ Reply to Thread