473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SOCKET problem

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
16 3809
|-|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
|-|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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mjcast | last post by:
I have been working on an ActiveX control in C#. It is packaged in a Windows Control library and the code is in a user control. The control is used as an automation receiver that is used to update a...
2
by: Nick Zdunic | last post by:
Ok - something for socket jockeys I have a .NET application using the TcpClient class from System.Net.Sockets I use this to connect to an IP address/Socket on the other side of Australia. ...
1
by: dexter15820 | last post by:
I am having a socket problem after upgrading my .net framework 1.1 to the latest patch level. After the upgrade I can establish a connection but I cannot read from the socket. If I remove the...
4
by: Laszlo Csabi | last post by:
Hi Folks, I'm using c# to create a server client application. I have to check if the socket is in"CLOSE_WAIT" state. Anyone knows any solution for this? I tried to used Socket.Connected but...
2
by: Rekkie | last post by:
Hi, I am trying to implement a ping client that is multithreading. The approach I have used is to create a ping class which I instantiate from the main thread and which contains a method...
1
by: Thomas Lerchner | last post by:
Hi! I have a problem with an UDP socket. I create socket and call BeginReceiveFrom to use the socket async. But the callback methode is being called several times with the same packet from the...
1
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program (myProblemProgram) that has a socket connected to...
0
by: Valerie Hough | last post by:
My app currently runs C#, .NET v2.0 on Windows Xp/Windows Server 2003, using asynchronous socket communication. I set up my server socket: Socket s = new Socket( AddressFamily.InterNetwork,...
7
by: =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?= | last post by:
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':...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.