Socket Trouble

[b][red]This message was edited by what99 at 2002-7-31 20:34:58[/red][/b][hr]
[b][red]This message was edited by what99 at 2002-7-31 20:7:41[/red][/b][hr]
I am having a basic problem. I get an error saying "error C2275: 'SOCKET' : illegal use of this type as an expression."

I have looked at many examples and this looks like it is set right. I am using Visual Studio 6.0. I have linked wsock32.lib and mpr.lib.



#include
#include
//#include
#include
#include
#include
#include

int main() {

WSADATA WsaDat;
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
{
printf("WSA Initialization Failed");
}


SOCKET Socket;
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
printf("Socket creation failed.");
}

}

I haven't got to the rest of the program since the socket initialization won't work. I have also tried and have the same error in Dev C++. Is there another file I am supposed to link or include? What else could be wrong?

Thanks


Comments

  • : [b][red]This message was edited by what99 at 2002-7-31 20:34:58[/red][/b][hr]
    : [b][red]This message was edited by what99 at 2002-7-31 20:7:41[/red][/b][hr]
    : I am having a basic problem. I get an error saying "error C2275: 'SOCKET' : illegal use of this type as an expression."
    :
    : I have looked at many examples and this looks like it is set right. I am using Visual Studio 6.0. I have linked wsock32.lib and mpr.lib.
    :
    :
    :
    : #include
    : #include
    : //#include
    : #include
    : #include
    : #include
    : #include
    :
    : int main() {
    :
    : WSADATA WsaDat;
    : if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
    : {
    : printf("WSA Initialization Failed");
    : }
    :
    :
    : SOCKET Socket;
    : Socket = socket(AF_INET, SOCK_STREAM, 0);
    : if (Socket == INVALID_SOCKET)
    : {
    : printf("Socket creation failed.");
    : }
    :
    : }
    :
    : I haven't got to the rest of the program since the socket initialization won't work. I have also tried and have the same error in Dev C++. Is there another file I am supposed to link or include? What else could be wrong?
    :
    : Thanks
    :
    :
    :

    Strange. I compiled that piece of code with VC++ 6.0 (enterprise edition) and it worked well.

    Are you compiling your code as C? If you are, you need to declare Socket variable before that WSAStartup statement.
  • [b][red]This message was edited by what99 at 2002-8-1 7:54:7[/red][/b][hr]
    [b][red]This message was edited by what99 at 2002-8-1 7:53:33[/red][/b][hr]
    [b][red]This message was edited by what99 at 2002-8-1 7:43:16[/red][/b][hr]
    [QUOTE]Strange. I compiled that piece of code with VC++ 6.0 (enterprise edition) and it worked well.

    Are you compiling your code as C? If you are, you need to declare Socket variable before that WSAStartup statement.[/QUOTE]


    That must be the problem. My file is called sockprac.c. This is my first try at sockets in C.


    #include
    #include
    //#include
    #include
    #include
    #include
    #include

    int main() {

    SOCKET Socket;

    WSADATA WsaDat;
    if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
    {
    printf("WSA Initialization Failed");
    }


    //SOCKET Socket;
    Socket = socket(AF_INET, SOCK_STREAM, 0);
    if (Socket == INVALID_SOCKET)
    {
    printf("Socket creation failed.");
    }
    }

    So is this how it should be. I get no errors at compile time, but if you could verify I'd really appriciate it. Thanks










  • [b][red]This message was edited by weicco at 2002-8-2 3:29:40[/red][/b][hr]
    : [b][red]This message was edited by what99 at 2002-8-1 7:54:7[/red][/b][hr]
    : [b][red]This message was edited by what99 at 2002-8-1 7:53:33[/red][/b][hr]
    : [b][red]This message was edited by what99 at 2002-8-1 7:43:16[/red][/b][hr]
    : [QUOTE]Strange. I compiled that piece of code with VC++ 6.0 (enterprise edition) and it worked well.
    :
    : Are you compiling your code as C? If you are, you need to declare Socket variable before that WSAStartup statement.[/QUOTE]
    :
    :
    : That must be the problem. My file is called sockprac.c. This is my first try at sockets in C.
    :
    :
    : #include
    : #include
    : //#include
    : #include
    : #include
    : #include
    : #include
    :
    : int main() {
    :
    : SOCKET Socket;
    :
    : WSADATA WsaDat;
    : if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
    : {
    : printf("WSA Initialization Failed");
    : }
    :
    :
    : //SOCKET Socket;
    : Socket = socket(AF_INET, SOCK_STREAM, 0);
    : if (Socket == INVALID_SOCKET)
    : {
    : printf("Socket creation failed.");
    : }
    : }
    :
    : So is this how it should be. I get no errors at compile time, but if you could verify I'd really appriciate it. Thanks
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :

    hmm... I'm not too good in english so this is little difficult to me to explain... but let's try.

    In C all variables must be declared before any statement (you can initialize variables). Like this:

    [code]int main(void) {
    int i; // ok
    char c; // ok
    some_func();
    int n; // error
    return 0;
    }
    [/code]

    Of course you can declare variables like this:

    [code]int main(void) {
    int i; // ok
    if (some_func()) {
    int n; // on;
    }
    int a; // error;

    {
    int b; // ok
    }
    return 0;
    }
    [/code]

    I hope you understand. If not maybe somebody with better english skills could explain... :)


  • Perfectly clear. Thank you much.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories