Error C2275: illegal use of this type as an expression NEED HELP - Programmers Heaven

Howdy, Stranger!

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

Categories

Error C2275: illegal use of this type as an expression NEED HELP

eternities_endeternities_end Posts: 13Member
I just started winsock programing in C. I tried to build my first server, but every time I compile it I get the following errors
error C2275: 'SOCKET' : illegal use of this type as an expression

I tried using the code from the MSDN lib. but every time i get the same error when I try to initialize a error
The code is as follows.

#include
#include "winsock2.h"

void main() {

// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()
");

// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld
", WSAGetLastError() );
WSACleanup();
return;
}

// Bind the socket.
sockaddr_in service;

service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr( "127.0.0.1" );
service.sin_port = htons( 27015 );

if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
printf( "bind() failed.
" );
closesocket(m_socket);
return;
}

// Listen on the socket.
if ( listen( m_socket, 1 ) == SOCKET_ERROR )
printf( "Error listening on socket.
");

// Accept connections.
SOCKET AcceptSocket;

printf( "Waiting for a client to connect...
" );
while (1) {
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR ) {
AcceptSocket = accept( m_socket, NULL, NULL );
}
printf( "Client Connected.
");
m_socket = AcceptSocket;
break;
}

// Send and receive data.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "Server: Sending Data.";
char recvbuf[32] = "";

bytesRecv = recv( m_socket, recvbuf, 32, 0 );
printf( "Bytes Recv: %ld
", bytesRecv );

bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld
", bytesSent );

return;
}

Any help would be greatly appreciated

Thanks in advance.

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    : I just started winsock programing in C. I tried to build my first server, but every time I compile it I get the following errors
    : error C2275: 'SOCKET' : illegal use of this type as an expression
    :
    : I tried using the code from the MSDN lib. but every time i get the same error when I try to initialize a error


    your code compiles without any errors or warnings on VC++ 6.0 compiler.
  • DB1DB1 Posts: 1,142Member
    : I just started winsock programing in C. I tried to build my first server, but every time I compile it I get the following errors
    : error C2275: 'SOCKET' : illegal use of this type as an expression
    :


    What compiler are you using? SOCKET is normally typedef'd in the winsock header
    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • eternities_endeternities_end Posts: 13Member
    : : I just started winsock programing in C. I tried to build my first server, but every time I compile it I get the following errors
    : : error C2275: 'SOCKET' : illegal use of this type as an expression
    : :
    :
    :
    : What compiler are you using? SOCKET is normally typedef'd in the winsock header
    : [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]
    :
    :

    Thanks for the reply.
    I seem to have figured out what the problem was, I saved the file as a .c prog when i changed it and compiled it as a cpp prog no problems. I thought SOCKET worked in C also?
    Now I seem to have run into another problem
    while (RetVal != SOCKET_ERROR);
    {
    RetVal = recv(Socket,String,50,0);
    if (RetVal == 0 || RetVal == WSAECONNRESET)
    {
    printf("Connection was closed");
    break;
    }

    printf("The string is:%d", RetVal);
    }
    When I build the above code I keep getting a illegal break; why is that?
    I took what was given in the msdn lib and used it and it worked fine.
    This is what I got from the msdn lib
    // while (bytesRecv != SOCKET_ERROR)
    // {
    // bytesRecv = recv(Socket,recvBuffer,32,0);
    // if (bytesRecv==0 || bytesRecv == WSAECONNRESET)
    {
    // printf("The connection was closed
    ");
    // break;
    // }
    // printf( "Bytes Recv: %ld
    ", &bytesRecv );
    //}
    I can not seem to find what is different between my code and the one I got from the msdn lib, if anyone could tell me the difference I would be very greatful

    This is the full code

    #include
    #include

    void main()
    {
    WSADATA wsaData;
    int err;

    if (WSAStartup(MAKEWORD(1,1),&wsaData)!=0)
    {
    printf("Error in WSA initialization");
    }

    SOCKET Socket;
    Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if (Socket==SOCKET_ERROR)
    {
    printf("Failure in initializing the socket");
    }

    // Binding the socket
    // SOCKADDR_IN sockaddr has to be defined
    // It is the struct that holds data about the port is going to be binded to
    sockaddr_in SockAddr;
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_port = htons(27015);

    // Binding on IP add 127.0.0.1
    SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    // Binding the socket
    // The bind() function takes three parameters,
    //a socket to bind to, a pointer to a data structure
    //that has the port information (structure type STRUCTADDR),
    //and the size of the structure with the port information.
    if (bind(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)))
    {
    printf("Error in binding a socket");
    }

    // Listening on the socket
    if (listen(Socket,1)!=0)
    {
    printf("Unable to listen on the socket");
    }

    // The socket has to be listening for the exact momement
    //at which the connection is made other wise it will miss
    //it and the connection will not be established
    SOCKET tempSocket = SOCKET_ERROR;
    printf("Waiting for connection....");
    while (tempSocket == SOCKET_ERROR)
    {
    tempSocket = accept(Socket,NULL,NULL);
    }
    Socket = tempSocket;
    printf("Client connected");

    // Getting data from the socket
    int RetVal ;
    char String[50];
    int bytesRecv;
    char recvBuffer[32];


    // while (bytesRecv != SOCKET_ERROR)
    // {
    // bytesRecv = recv(Socket,recvBuffer,32,0);
    // if (bytesRecv==0 || bytesRecv == WSAECONNRESET)
    {
    // printf("The connection was closed
    ");
    // break;
    // }
    // printf( "Bytes Recv: %ld
    ", &bytesRecv );
    //}



    while (RetVal != SOCKET_ERROR);
    {
    RetVal = recv(Socket,String,50,0);
    if (RetVal == 0 || RetVal == WSAECONNRESET)
    {
    printf("Connection was closed");
    break;
    }

    printf("The string is:%d", RetVal);
    }
    // printf("%d", RetVal);

    WSACleanup();
    return;


    }


    Thanks in advance


Sign In or Register to comment.