little network/socket-program with parse error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • selekta

    #1

    little network/socket-program with parse error

    hi,

    i've recently started to deal a little bit with tcp/ip-socket-programming
    (linux).
    Now i've written a little test-program (as displayed further down).
    When i'm trying to compile it the gcc-compiler return "parse error in
    l.23/.24 before token '='. "
    the line before those two ( host_addr.sin_f amily = AF_INET;) is acepted
    without any problems.
    I'd be happy if anybody could tell me where's my fault.

    Thanks a lot.

    I'm sorry for mistakes in my english.

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>

    #define host_IP = "127.0.0.1" ; //IP only for test purpose
    #define host_PORT = 1024; //port number only for test purpose

    int main(void)
    {
    int sock;
    struct sockaddr_in host_addr;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1)
    printf("couldn' t establish socket.\n");
    else
    printf("socket created.\n");

    host_addr.sin_f amily = AF_INET;
    // l. 23// host_addr.sin_p ort = htons(host_PORT );
    //l. 24// host_addr.sin_a ddr.s_addr = inet_addr(host_ IP);

    if (connect(sock, (struct sockaddr *)&host_addr,si zeof(&host_addr )) != -1)
    printf("connect ed to server.\n");
    else
    printf("couldn' t connect.\n");

    close(sock);
    printf("socket closed.\n");
    }


  • Case

    #2
    Re: little network/socket-program with parse error

    selekta wrote:[color=blue]
    > hi,
    >
    > i've recently started to deal a little bit with tcp/ip-socket-programming
    > (linux).
    > Now i've written a little test-program (as displayed further down).
    > When i'm trying to compile it the gcc-compiler return "parse error in
    > l.23/.24 before token '='. "
    > the line before those two ( host_addr.sin_f amily = AF_INET;) is acepted
    > without any problems.
    > I'd be happy if anybody could tell me where's my fault.
    >
    > Thanks a lot.
    >
    > I'm sorry for mistakes in my english.
    >
    > #include <stdio.h>
    > #include <sys/types.h>
    > #include <sys/socket.h>
    > #include <netinet/in.h>
    > #include <arpa/inet.h>
    > #include <unistd.h>
    >
    > #define host_IP = "127.0.0.1" ; //IP only for test purpose
    > #define host_PORT = 1024; //port number only for test purpose[/color]

    The preprocessor directive #define should be seen as nothing
    more than a text-processor 'find-replace' operation. So what
    happens below where you get compiler errors is that

    'host_PORT'

    is literally replaced with

    '= 1024;'

    Looking at your code below, clearly this is an error. So:

    #define host_PORT 1024

    BTW, it is a kind of non-written rule to write #define'd names
    in capitals: 'HOST_PORT'.

    Same for 'host_IP'.

    Many compiler have a switch to spit out the code after preprocessing.
    In a few well known compilers this is the -E switch. Can be handy
    in cases like this.
    [color=blue]
    >
    > int main(void)
    > {
    > int sock;
    > struct sockaddr_in host_addr;
    >
    > sock = socket(AF_INET, SOCK_STREAM, 0);
    > if (sock == -1)
    > printf("couldn' t establish socket.\n");
    > else
    > printf("socket created.\n");
    >
    > host_addr.sin_f amily = AF_INET;
    > // l. 23// host_addr.sin_p ort = htons(host_PORT );
    > //l. 24// host_addr.sin_a ddr.s_addr = inet_addr(host_ IP);
    >
    > if (connect(sock, (struct sockaddr *)&host_addr,si zeof(&host_addr )) != -1)
    > printf("connect ed to server.\n");
    > else
    > printf("couldn' t connect.\n");
    >
    > close(sock);
    > printf("socket closed.\n");
    > }
    >
    >[/color]

    Comment

    • Devrobcom

      #3
      Re: little network/socket-program with parse error

      Your defines shall not have any =
      #define host_IP "127.0.0.1" ; //IP only for test purpose
      #define host_PORT 1024; //port number only for test purpose

      "selekta" <n.rebel@gmx.ne t> wrote in message
      news:c9hqi4$1tk $06$1@news.t-online.com...[color=blue]
      > hi,
      >
      > i've recently started to deal a little bit with tcp/ip-socket-programming
      > (linux).
      > Now i've written a little test-program (as displayed further down).
      > When i'm trying to compile it the gcc-compiler return "parse error in
      > l.23/.24 before token '='. "
      > the line before those two ( host_addr.sin_f amily = AF_INET;) is acepted
      > without any problems.
      > I'd be happy if anybody could tell me where's my fault.
      >
      > Thanks a lot.
      >
      > I'm sorry for mistakes in my english.
      >
      > #include <stdio.h>
      > #include <sys/types.h>
      > #include <sys/socket.h>
      > #include <netinet/in.h>
      > #include <arpa/inet.h>
      > #include <unistd.h>
      >
      > #define host_IP = "127.0.0.1" ; //IP only for test purpose
      > #define host_PORT = 1024; //port number only for test purpose
      >
      > int main(void)
      > {
      > int sock;
      > struct sockaddr_in host_addr;
      >
      > sock = socket(AF_INET, SOCK_STREAM, 0);
      > if (sock == -1)
      > printf("couldn' t establish socket.\n");
      > else
      > printf("socket created.\n");
      >
      > host_addr.sin_f amily = AF_INET;
      > // l. 23// host_addr.sin_p ort = htons(host_PORT );
      > //l. 24// host_addr.sin_a ddr.s_addr = inet_addr(host_ IP);
      >
      > if (connect(sock, (struct sockaddr *)&host_addr,si zeof(&host_addr ))[/color]
      != -1)[color=blue]
      > printf("connect ed to server.\n");
      > else
      > printf("couldn' t connect.\n");
      >
      > close(sock);
      > printf("socket closed.\n");
      > }
      >
      >[/color]


      Comment

      • Harti Brandt

        #4
        Re: little network/socket-program with parse error

        On Tue, 1 Jun 2004, Devrobcom wrote:

        D>Your defines shall not have any =
        D>#define host_IP "127.0.0.1" ; //IP only for test purpose
        D>#define host_PORT 1024; //port number only for test purpose

        They also should not have a ';'.

        harti

        D>
        D>"selekta" <n.rebel@gmx.ne t> wrote in message
        D>news:c9hqi4$1 tk$06$1@news.t-online.com...
        D>> hi,
        D>>
        D>> i've recently started to deal a little bit with tcp/ip-socket-programming
        D>> (linux).
        D>> Now i've written a little test-program (as displayed further down).
        D>> When i'm trying to compile it the gcc-compiler return "parse error in
        D>> l.23/.24 before token '='. "
        D>> the line before those two ( host_addr.sin_f amily = AF_INET;) is acepted
        D>> without any problems.
        D>> I'd be happy if anybody could tell me where's my fault.
        D>>
        D>> Thanks a lot.
        D>>
        D>> I'm sorry for mistakes in my english.
        D>>
        D>> #include <stdio.h>
        D>> #include <sys/types.h>
        D>> #include <sys/socket.h>
        D>> #include <netinet/in.h>
        D>> #include <arpa/inet.h>
        D>> #include <unistd.h>
        D>>
        D>> #define host_IP = "127.0.0.1" ; //IP only for test purpose
        D>> #define host_PORT = 1024; //port number only for test purpose
        D>>
        D>> int main(void)
        D>> {
        D>> int sock;
        D>> struct sockaddr_in host_addr;
        D>>
        D>> sock = socket(AF_INET, SOCK_STREAM, 0);
        D>> if (sock == -1)
        D>> printf("couldn' t establish socket.\n");
        D>> else
        D>> printf("socket created.\n");
        D>>
        D>> host_addr.sin_f amily = AF_INET;
        D>> // l. 23// host_addr.sin_p ort = htons(host_PORT );
        D>> //l. 24// host_addr.sin_a ddr.s_addr = inet_addr(host_ IP);
        D>>
        D>> if (connect(sock, (struct sockaddr *)&host_addr,si zeof(&host_addr ))
        D>!= -1)
        D>> printf("connect ed to server.\n");
        D>> else
        D>> printf("couldn' t connect.\n");
        D>>
        D>> close(sock);
        D>> printf("socket closed.\n");
        D>> }
        D>>
        D>>
        D>
        D>
        D>

        Comment

        • CBFalconer

          #5
          Re: little network/socket-program with parse error

          selekta wrote:[color=blue]
          >[/color]
          .... snip ...[color=blue]
          >
          > #include <stdio.h>
          > #include <sys/types.h>
          > #include <sys/socket.h>
          > #include <netinet/in.h>
          > #include <arpa/inet.h>
          > #include <unistd.h>[/color]

          None of the above, apart from stdio.h, are defined in standard C.
          Thus it is impossible to deal with your problems in this
          newsgroup. You need a system specific group.
          [color=blue]
          >
          > #define host_IP = "127.0.0.1" ; //IP only for test purpose
          > #define host_PORT = 1024; //port number only for test purpose[/color]

          Do not use // comments in newsgroups, they do not survive line
          wrapping particularly well.
          [color=blue]
          >
          > int main(void)
          > {
          > int sock;
          > struct sockaddr_in host_addr;[/color]

          because things like this are undefined here.

          --
          A: Because it fouls the order in which people normally read text.
          Q: Why is top-posting such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?

          Comment

          • Joona I Palaste

            #6
            Re: little network/socket-program with parse error

            CBFalconer <cbfalconer@yah oo.com> scribbled the following:[color=blue]
            > selekta wrote:[color=green]
            >>[/color]
            > ... snip ...[color=green]
            >>
            >> #include <stdio.h>
            >> #include <sys/types.h>
            >> #include <sys/socket.h>
            >> #include <netinet/in.h>
            >> #include <arpa/inet.h>
            >> #include <unistd.h>[/color][/color]
            [color=blue]
            > None of the above, apart from stdio.h, are defined in standard C.
            > Thus it is impossible to deal with your problems in this
            > newsgroup. You need a system specific group.[/color]
            [color=blue][color=green]
            >>
            >> #define host_IP = "127.0.0.1" ; //IP only for test purpose
            >> #define host_PORT = 1024; //port number only for test purpose[/color][/color]
            [color=blue]
            > Do not use // comments in newsgroups, they do not survive line
            > wrapping particularly well.[/color]

            Way to miss the real problem completely, CBFalconer.
            [color=blue][color=green]
            >> int main(void)
            >> {
            >> int sock;
            >> struct sockaddr_in host_addr;[/color][/color]
            [color=blue]
            > because things like this are undefined here.[/color]

            --
            /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
            \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
            "How can we possibly use sex to get what we want? Sex IS what we want."
            - Dr. Frasier Crane

            Comment

            • Richard Tobin

              #7
              Re: little network/socket-program with parse error

              In article <40BC8D5F.F426A 4BD@yahoo.com>,
              CBFalconer <cbfalconer@wor ldnet.att.net> wrote:
              [color=blue]
              >Thus it is impossible to deal with your problems in this
              >newsgroup.[/color]

              And yet, remarkably, several people did.

              -- Richard

              Comment

              • CBFalconer

                #8
                Re: little network/socket-program with parse error

                Richard Tobin wrote:[color=blue]
                > CBFalconer <cbfalconer@wor ldnet.att.net> wrote:
                >[color=green]
                >> Thus it is impossible to deal with your problems in this
                >> newsgroup.[/color]
                >
                > And yet, remarkably, several people did.[/color]

                More power to them. You concealed it behind a mass of uncheckable
                code very well.

                --
                fix (vb.): 1. to paper over, obscure, hide from public view; 2.
                to work around, in a way that produces unintended consequences
                that are worse than the original problem. Usage: "Windows ME
                fixes many of the shortcomings of Windows 98 SE". - Hutchison


                Comment

                Working...