By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

SOCKET problem

P: n/a
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!!

Oct 4 '07 #1
Share this Question
Share on Google+
16 Replies


P: n/a
|-|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.
Oct 4 '07 #2

P: n/a
|-|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
Oct 4 '07 #3

P: n/a
jacob navia <ja***@nospam.orgwrites:
>.... you can't declare
something somewhere in your function, but only at the beginning
of a block.....

Where do you get this rubbish?

--
Chris.
Oct 4 '07 #4

P: n/a
Chris McDonald wrote:
jacob navia <ja***@nospam.orgwrites:
>.... 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
Oct 4 '07 #5

P: n/a
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.
Oct 4 '07 #6

P: n/a
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
Oct 4 '07 #7

P: n/a
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:Th*************@gmail.com>

Oct 4 '07 #8

P: n/a
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

Oct 4 '07 #9

P: n/a
In article <47***************@yahoo.com>,
CBFalconer <cb********@maineline.netdrooled:
....
>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.

Oct 4 '07 #10

P: n/a
jacob navia <ja***@nospam.orgwrites:
|-|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.</OTUnfortunately, 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) ks***@mib.org <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"
Oct 4 '07 #11

P: n/a
CBFalconer <cb********@yahoo.comwrites:
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) ks***@mib.org <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"
Oct 4 '07 #12

P: n/a
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.
Oct 4 '07 #13

P: n/a
Ian Collins <ia******@hotmail.comwrites:
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) ks***@mib.org <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"
Oct 4 '07 #14

P: n/a
On Oct 4, 11:31 pm, Keith Thompson <ks...@mib.orgwrote:
<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

Oct 5 '07 #15

P: n/a
Prayag Narula wrote:
On Oct 4, 11:31 pm, Keith Thompson <ks...@mib.orgwrote:
<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>

Oct 5 '07 #16

P: n/a
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:Th*************@gmail.com>
Oct 5 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.