SOCKET problem

Discussion in 'C Programming' started by =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=, Oct 4, 2007.

  1. hi all!
    I got a problem. I declared a SOCKET var in my C program but when i
    compiled the program it displayed like
    *--------------------------------------------------------------*
    *'SOCKET': illegal use of this type as an expression *
    * C:\..\..\..\include\winsock2.h: see declaration of 'SOCKET' *
    *--------------------------------------------------------------*

    What i wrote in my program is like this..:

    //-------------------------------------------------------------
    //I am still trying to create a Socket and use it

    SOCKET m_socket; // <- *this is where i got mad!
    m_socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(m_socket==INVALID_SOCKET){
    printf("Error at socket(): %ld\n socket yadre!!",
    WSAGetLastError());
    WSACleanup();
    return 0;
    }else{
    printf("socket() is ok [sijinaba yare]!\n");
    }
    //-------------------------------------------------------------

    How i compiled is like this...:
    [compiler info: C/C++ compiler version 14.00.50727.42 for 80x86]
    -
    -cl /Tc sok_unf.c ws2_32.Lib

    More info:
    I m using Windows XP SP2.


    Please help me out. i'll be very thankful to you!!
     
    =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=, Oct 4, 2007
    #1
    1. Advertising

  2. =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=

    Ian Collins Guest

    |-|e|_|_ B0Ý wrote:
    > hi all!
    > I got a problem. I declared a SOCKET var in my C program but when i
    > compiled the program it displayed like
    > *--------------------------------------------------------------*
    > *'SOCKET': illegal use of this type as an expression *
    > * C:\..\..\..\include\winsock2.h: see declaration of 'SOCKET' *
    > *--------------------------------------------------------------*
    >

    You'll have to ask on windows group, this is very platform specific.

    --
    Ian Collins.
     
    Ian Collins, Oct 4, 2007
    #2
    1. Advertising

  3. =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=

    jacob navia Guest

    |-|e|_|_ B0Ý wrote:
    > hi all!
    > I got a problem. I declared a SOCKET var in my C program but when i
    > compiled the program it displayed like
    > *--------------------------------------------------------------*
    > *'SOCKET': illegal use of this type as an expression *
    > * C:\..\..\..\include\winsock2.h: see declaration of 'SOCKET' *
    > *--------------------------------------------------------------*
    >
    > What i wrote in my program is like this..:
    >
    > //-------------------------------------------------------------
    > //I am still trying to create a Socket and use it
    >
    > SOCKET m_socket; // <- *this is where i got mad!
    > m_socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    > if(m_socket==INVALID_SOCKET){
    > printf("Error at socket(): %ld\n socket yadre!!",
    > WSAGetLastError());
    > WSACleanup();
    > return 0;
    > }else{
    > printf("socket() is ok [sijinaba yare]!\n");
    > }
    > //-------------------------------------------------------------
    >
    > How i compiled is like this...:
    > [compiler info: C/C++ compiler version 14.00.50727.42 for 80x86]
    > -
    > -cl /Tc sok_unf.c ws2_32.Lib
    >
    > More info:
    > I m using Windows XP SP2.
    >
    >
    > Please help me out. i'll be very thankful to you!!
    >


    Microsoft compiler doesn't use standard C and you can't declare
    something somewhere in your function, but only at the beginning
    of a block. I guess you have something BEFORE the declaration that
    you are not showing us. Please send more context.

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Oct 4, 2007
    #3
  4. jacob navia <> writes:

    >.... you can't declare
    >something somewhere in your function, but only at the beginning
    >of a block.....



    Where do you get this rubbish?

    --
    Chris.
     
    Chris McDonald, Oct 4, 2007
    #4
  5. =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=

    jacob navia Guest

    Chris McDonald wrote:
    > jacob navia <> writes:
    >
    >> .... you can't declare
    >> something somewhere in your function, but only at the beginning
    >> of a block.....

    >
    >
    > Where do you get this rubbish?
    >
    > --
    > Chris.


    Hi Chris.

    The mixing of declarations and code was introduced into
    the language by the C99 standard, 8 years ago. Before
    speaking "where do you get this rubbish" you should refresh
    your knowledge abut C.

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Oct 4, 2007
    #5
  6. =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=

    Ian Collins Guest

    jacob navia wrote:
    > |-|e|_|_ B0Ý wrote:
    >>
    >> Please help me out. i'll be very thankful to you!!
    >>

    >
    > Microsoft compiler doesn't use standard C and you can't declare
    > something somewhere in your function, but only at the beginning
    > of a block. I guess you have something BEFORE the declaration that
    > you are not showing us. Please send more context.
    >

    No! please spare us from more totally platform specific code. The
    question belongs on a windows group, not here.

    --
    Ian Collins.
     
    Ian Collins, Oct 4, 2007
    #6
  7. =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=

    jacob navia Guest

    Ian Collins wrote:
    > jacob navia wrote:
    >> |-|e|_|_ B0Ý wrote:
    >>> Please help me out. i'll be very thankful to you!!
    >>>

    >> Microsoft compiler doesn't use standard C and you can't declare
    >> something somewhere in your function, but only at the beginning
    >> of a block. I guess you have something BEFORE the declaration that
    >> you are not showing us. Please send more context.
    >>

    > No! please spare us from more totally platform specific code. The
    > question belongs on a windows group, not here.
    >


    The code is important because I suspect a syntax error. That has nothing
    to do with the code itself but with how that code is layed out.


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Oct 4, 2007
    #7
  8. jacob navia wrote:
    >
    > Ian Collins wrote:
    > > jacob navia wrote:
    > >> |-|e|_|_ B0Ý wrote:
    > >>> Please help me out. i'll be very thankful to you!!
    > >>>
    > >> Microsoft compiler doesn't use standard C and you can't declare
    > >> something somewhere in your function, but only at the beginning
    > >> of a block. I guess you have something BEFORE the declaration that
    > >> you are not showing us. Please send more context.
    > >>

    > > No! please spare us from more totally platform specific code. The
    > > question belongs on a windows group, not here.
    > >

    >
    > The code is important because I suspect a syntax error. That has nothing
    > to do with the code itself but with how that code is layed out.


    Well, I guess if he were to replace "SOCKET" with "unsigned int",
    and he gets the same error (complaining about "unsigned int" rather
    tha "SOCKET", of course), then we can eliminate (for now) the
    Windows-specificness of the question. In that case, I agree with
    Jacob about needing the rest of the code. Of course, the rest of
    the code may contain enough Windows-specific things that it would
    be best to go elsewhere for answers. On the other hand, it may be
    unrelated to the names of the typedefs used or functions called,
    and unrelated to the Windowsness of the code. (How many hands was
    that?)

    That's the problem with such small snippets -- there's not enough
    information given to properly diagnose the problem.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Oct 4, 2007
    #8
  9. =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=

    CBFalconer Guest

    jacob navia wrote:
    > |-|e|_|_ B0Ý wrote:
    >
    >> I got a problem. I declared a SOCKET var in my C program but when i
    >> compiled the program it displayed like
    >> *--------------------------------------------------------------*
    >> *'SOCKET': illegal use of this type as an expression *
    >> * C:\..\..\..\include\winsock2.h: see declaration of 'SOCKET' *
    >> *--------------------------------------------------------------*
    >>
    >> What i wrote in my program is like this..:
    >>
    >> //-------------------------------------------------------------
    >> //I am still trying to create a Socket and use it
    >>
    >> SOCKET m_socket; // <- *this is where i got mad!
    >> m_socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    >> if(m_socket==INVALID_SOCKET){
    >> printf("Error at socket(): %ld\n socket yadre!!",
    >> WSAGetLastError());
    >> WSACleanup();
    >> return 0;
    >> }else{
    >> printf("socket() is ok [sijinaba yare]!\n");
    >> }
    >> //-------------------------------------------------------------
    >>
    >> How i compiled is like this...:
    >> [compiler info: C/C++ compiler version 14.00.50727.42 for 80x86]
    >> -
    >> -cl /Tc sok_unf.c ws2_32.Lib
    >>
    >> More info:
    >> I m using Windows XP SP2.

    >
    > Microsoft compiler doesn't use standard C and you can't declare
    > something somewhere in your function, but only at the beginning
    > of a block. I guess you have something BEFORE the declaration that
    > you are not showing us. Please send more context.


    You know very well that this is off-topic on c.l.c, and that asking
    for 'more context' is simply worsening the offence. If you had
    simply pointed out the improvident use of // comments or something
    similar, there would have been no offence.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Oct 4, 2007
    #9
  10. In article <>,
    CBFalconer <> drooled:
    ....
    >You know very well that this is off-topic on c.l.c, and that asking
    >for 'more context' is simply worsening the offence. If you had
    >simply pointed out the improvident use of // comments or something
    >similar, there would have been no offence.


    Pay no attention to the CBF.
     
    Kenny McCormack, Oct 4, 2007
    #10
  11. jacob navia <> writes:
    > |-|e|_|_ B0Ý wrote:
    >> I got a problem. I declared a SOCKET var in my C program but when i
    >> compiled the program it displayed like
    >> *--------------------------------------------------------------*
    >> *'SOCKET': illegal use of this type as an expression *
    >> * C:\..\..\..\include\winsock2.h: see declaration of 'SOCKET' *
    >> *--------------------------------------------------------------*
    >> What i wrote in my program is like this..:
    >> //-------------------------------------------------------------
    >> //I am still trying to create a Socket and use it
    >> SOCKET m_socket; // <- *this is where i got mad!
    >> m_socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    >> if(m_socket==INVALID_SOCKET){
    >> printf("Error at socket(): %ld\n socket yadre!!",
    >> WSAGetLastError());
    >> WSACleanup();
    >> return 0;
    >> }else{
    >> printf("socket() is ok [sijinaba yare]!\n");
    >> }

    [snip]
    >
    > Microsoft compiler doesn't use standard C and you can't declare
    > something somewhere in your function, but only at the beginning
    > of a block. I guess you have something BEFORE the declaration that
    > you are not showing us. Please send more context.


    That's misleading, or at least incomplete. Simply asserting that
    Microsoft's compiler "doesn't use standard C", without bothering to
    explain that it simply implements an earlier standard, is not
    particularly helpful.

    Microsoft's compiler, as far as I know, implements the C90 standard
    (perhaps C95, which is C90 with a few minor additions). Under the C90
    standard, declarations and statements may not be mixed within a block;
    all the declarations must come first, followed by all the statements.
    (If you want a declaration following a statement, you can introduce a
    nested block.)

    The C99 standard permits declarations and statements to be mixed
    within a block. <OT>C++ also allows this.</OT> Unfortunately, very
    few compilers fully implement the C99 standard.

    The line ``SOCKET m_socket;'' appears to be a declaration of a
    variable named ``m_socket'', of type ``SOCKET''. The most likely
    reason for the error message is that you have a statement preceding
    the declaration; a compiler that implements C90 or C95 rather than C99
    will complain about this. It would be nice if your compiler produced
    a clearer message, something like "declarations may not follow
    statements within a block", but since it's actually a syntax error,
    the compiler gets confused; it can't quite figure out what you *meant*
    to say. Since it's expecting to see a statement, it doesn't even
    recognize ``SOCKET m_socket;'' as a declaration.

    This is only a guess, since I can't see the rest of your code. If
    I've guessed right, you can fix the problem by rearranging your code
    so that statements follow declarations in each block (it's probably as
    simple as moving ``SOCKET m_socket;'' up a line or two). If not, post
    again (here or elsewhere, depending on the nature of the problem).

    SOCKETs are not part of standard C; they're a system-specific feature.
    If your problem were caused by your use of SOCKETs, you'd need to ask
    about it in a system-specific newsgroups, probably
    comp.os.ms-windows.programmer.win32 or one of the microsoft.* groups.

    But in this case, your problem is *probably* related to which version
    of the C standard your compiler supports; you'd have the same problem
    declaring a variable of type int rather than SOCKET. If that's the
    case, then this is exactly the right place to ask.

    Incidentally, "//" comments are non-standard in C90 and C95; they were
    introduced to the language in C99. Apparently your compiler supports
    them as an extension. If you want your code to be as portable as
    possible, you should use "/* ... */" comments rather than "// comments
    -- but since you're using SOCKETs, your code isn't portable anyway.
    Using "//" comments in Usenet postings can also cause problems, since
    line-wrapping can introduce syntax errors.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 4, 2007
    #11
  12. CBFalconer <> writes:
    > jacob navia wrote:

    [snip]
    >> Microsoft compiler doesn't use standard C and you can't declare
    >> something somewhere in your function, but only at the beginning
    >> of a block. I guess you have something BEFORE the declaration that
    >> you are not showing us. Please send more context.

    >
    > You know very well that this is off-topic on c.l.c, and that asking
    > for 'more context' is simply worsening the offence. If you had
    > simply pointed out the improvident use of // comments or something
    > similar, there would have been no offence.


    It's very likely that the error was caused by mixing declarations and
    statements. That's purely a C issue, even if the code uses
    system-specific extensions. We don't know what a SOCKET is, but we
    can tell that ``SOCKET m_socket;'' is a declaration.

    See my other response elsethread.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 4, 2007
    #12
  13. =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=

    Ian Collins Guest

    Keith Thompson wrote:
    >
    > The line ``SOCKET m_socket;'' appears to be a declaration of a
    > variable named ``m_socket'', of type ``SOCKET''. The most likely
    > reason for the error message is that you have a statement preceding
    > the declaration; a compiler that implements C90 or C95 rather than C99
    > will complain about this. It would be nice if your compiler produced
    > a clearer message, something like "declarations may not follow
    > statements within a block", but since it's actually a syntax error,
    > the compiler gets confused; it can't quite figure out what you *meant*
    > to say. Since it's expecting to see a statement, it doesn't even
    > recognize ``SOCKET m_socket;'' as a declaration.
    >

    It turns out the OP omitted the 'struct' keyword before SOCKET (the
    question was multi-posted).

    Which just goes to show what a waste of time and effort speculation
    based on incomplete or inappropriate code fragment is. I'm sure someone
    on a windows group familiar with that type or macro or whatever would
    have been able to answer with the snippet provided. That's why
    redirection is the most appropriate response in these situations.

    --
    Ian Collins.
     
    Ian Collins, Oct 4, 2007
    #13
  14. Ian Collins <> writes:
    > Keith Thompson wrote:
    >> The line ``SOCKET m_socket;'' appears to be a declaration of a
    >> variable named ``m_socket'', of type ``SOCKET''. The most likely
    >> reason for the error message is that you have a statement preceding
    >> the declaration; a compiler that implements C90 or C95 rather than C99
    >> will complain about this. It would be nice if your compiler produced
    >> a clearer message, something like "declarations may not follow
    >> statements within a block", but since it's actually a syntax error,
    >> the compiler gets confused; it can't quite figure out what you *meant*
    >> to say. Since it's expecting to see a statement, it doesn't even
    >> recognize ``SOCKET m_socket;'' as a declaration.
    >>

    > It turns out the OP omitted the 'struct' keyword before SOCKET (the
    > question was multi-posted).


    Then the error message "'SOCKET': illegal use of this type as an
    expression" is really bad.

    > Which just goes to show what a waste of time and effort speculation
    > based on incomplete or inappropriate code fragment is. I'm sure someone
    > on a windows group familiar with that type or macro or whatever would
    > have been able to answer with the snippet provided. That's why
    > redirection is the most appropriate response in these situations.


    Fair enough. My guess (which was the same as jacob's) was IMHO a
    reasonable one, but apparently it was wrong.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 4, 2007
    #14
  15. On Oct 4, 11:31 pm, Keith Thompson <> wrote:
    <snip>
    >
    > > It turns out the OP omitted the 'struct' keyword before SOCKET (the
    > > question was multi-posted).


    This is the defination of SOCKET I found in WINSOCK.H and WINSOCK2.H

    typedef u_int SOCKET;

    So it is not a structure. Where did you find the other post ?

    >
    > Then the error message "'SOCKET': illegal use of this type as an
    > expression" is really bad.


    Well it does ask you to look it up in WINSOCK.H,
    I don't think it helps in anyway though. So, I agree.
    >
    > Fair enough. My guess (which was the same as jacob's) was IMHO a
    > reasonable one, but apparently it was wrong.


    I guess the same, I have faced similar problem with Nokia's compiler
    and it was released last year and I personally found it very annoying
    and particularly difficult to catch.

    Regards,
    Prayag Narula
     
    Prayag Narula, Oct 5, 2007
    #15
  16. =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=

    santosh Guest

    Prayag Narula wrote:

    > On Oct 4, 11:31 pm, Keith Thompson <> wrote:
    > <snip>
    >>
    >> > It turns out the OP omitted the 'struct' keyword before SOCKET (the
    >> > question was multi-posted).

    >
    > This is the defination of SOCKET I found in WINSOCK.H and WINSOCK2.H
    >
    > typedef u_int SOCKET;
    >
    > So it is not a structure. Where did you find the other post ?


    I suspect in a Windows newsgroup that Ian was following.

    Since SOCKET is not defined by the C Standard we really cannot say how
    exactly it's defined in the OP's case. It could very well be defined as an
    u_int for you and as a struct for the OP and as a macro for me. The point
    is we really cannot say without the OP actually showing us his
    implementation's definition.

    The best strategy is for the OP to ask in a group specialised for his
    system.

    <snip>
     
    santosh, Oct 5, 2007
    #16
  17. Ian Collins wrote:
    >
    > Keith Thompson wrote:
    > >
    > > The line ``SOCKET m_socket;'' appears to be a declaration of a
    > > variable named ``m_socket'', of type ``SOCKET''. The most likely

    [...]
    > It turns out the OP omitted the 'struct' keyword before SOCKET (the
    > question was multi-posted).
    >
    > Which just goes to show what a waste of time and effort speculation
    > based on incomplete or inappropriate code fragment is. I'm sure someone
    > on a windows group familiar with that type or macro or whatever would
    > have been able to answer with the snippet provided. That's why
    > redirection is the most appropriate response in these situations.


    Well, I happen to be somewhat familar with sockets on Windows, and
    "omitted the 'struct' keyword before SOCKET" doesn't make sense to
    me, as my Windows headers show "typedef u_int SOCKET;" as its
    definition. (Hence my earlier suggestion to eliminate the 'Windowsness'
    of the question by replacing "SOCKET" with "unsigned int", just as a
    test, and seeing what happened.)

    But, I do agree that the incomplete snippet was not enough to base
    anything more than guesses.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Oct 5, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    5,016
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,014
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    584
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    699
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    746
Loading...

Share This Page