SOCKET problem

  • Thread starter =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?=
  • Start date
?

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

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!!
 
I

Ian Collins

|-|e|_|_ B0Ý said:
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.
 
J

jacob navia

|-|e|_|_ B0Ý said:
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.
 
C

Chris McDonald

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


Where do you get this rubbish?
 
J

jacob navia

Chris said:
Where do you get this rubbish?

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.
 
I

Ian Collins

jacob said:
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.
 
J

jacob navia

Ian said:
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.
 
K

Kenneth Brody

jacob said:
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:[email protected]>
 
C

CBFalconer

jacob said:
|-|e|_|_ B0Ý said:
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.
 
K

Kenny McCormack

CBFalconer said:
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.
 
K

Keith Thompson

jacob navia said:
|-|e|_|_ B0Ý said:
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.
 
K

Keith Thompson

CBFalconer said:
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.
 
I

Ian Collins

Keith said:
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.
 
K

Keith Thompson

Ian Collins said:
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.
 
P

Prayag Narula

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
 
S

santosh

Prayag said:
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>
 
K

Kenneth Brody

Ian said:
Keith said:
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:[email protected]>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top