开发者

Variable or Field 'nameOfVariable' declared void no idea why :(

I've googled and searched on various websites and I can not work out why I get the follow errors from one of my functions.

error: variable or field 'ConnectClient' declared void

error: 'Socket' was not declared in this scope

Both of those errors are for the line 38 of my code, which is the...

void ConnectClient(Socket _newConnection)

From what I understood from other posts asking about this question, it normally came down to inconsistencies with the header file (Which I'm NOT using for this little test) or they had a return type in the void function which I don't have.

NOTE: I've removed extra bits of code that relate to part of the game code, and stacks of commented out code which I was testing. This program is a little test into winsock use

#include <iostream>
#include <winsock2.h>

using namespace std;
const int serverPort = 31337;
const int serverMaxClients = 10;
SOCKET serverSocket;
SOCKET clientSocketArray[serverMaxClients];   //should this have a -1 on it?

bool clientSlotTaken[serverMaxClients];
sockaddr clientSocketAddresses[serverMaxClients];

int FindFreePlayerSpot()
{
    for (int slotIndex = 0; slotIndex < serverMaxClients; slotIndex++)
    {
        if (clientSlotTaken[slotIndex] == false)
        {
            return slotIndex;
        }
    }
    return -1;
}

void ConnectClient(Socket _newConnection)
{
    int slotIndex = FindFreePlayerSpot();
    if (slotIndex == -1)
    {
        closesocket(_newConnection);
        cout << "No player slots available, connection aborted"
    }
    else //Should have a connection
    {
        clientSocketArray[slotIndex] = _newConnection;
        if (clientSocketArray[slotIndex] == INVALID_SOCKET)
        {
            cout << "Client Connection Error - INVALID SOCKET " << WSAGetLastError();
        }
        else //Client should have connected
        {
            cout << "Client Connected on socket slot [" << slotIndex << "] from " << "\n";
        开发者_如何学C    clientSlotTaken[slotIndex] = true;
        }
    }
}

void StartWinSock()
{
    WSADATA WsaDat;
    if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
    {
        std::cout<<"WSA Initialization failed!\r\n";
        WSACleanup();
        system("PAUSE");
    }

    serverSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(serverSocket==INVALID_SOCKET)
    {
        std::cout<<"Server Socket creation failed.\r\n";
        WSACleanup();
        system("PAUSE");
    }

    SOCKADDR_IN serverInf;
    serverInf.sin_family=AF_INET;
    serverInf.sin_addr.s_addr=INADDR_ANY;
    serverInf.sin_port=htons(serverPort);

    if(bind(serverSocket,(SOCKADDR*)(&serverInf),sizeof(serverInf))==SOCKET_ERROR)
    {
        std::cout<<"Unable to bind socket!\r\n";
        WSACleanup();
        system("PAUSE");
    }

    if(listen(serverSocket,SOMAXCONN)==SOCKET_ERROR)
    {
        std::cout<<"Unable to Listen!\r\n";
        WSACleanup();
        system("PAUSE");
    }

    u_long iMode=1;
    ioctlsocket(serverSocket,FIONBIO,&iMode);
}


int main()
{
    playerShip = new PlayerShip;
    bool runningServer = true;

        cout << "Starting WinSock\n";
        StartWinSock();
        cout << "Running on port " << serverPort << "\n";


        while (true)
        {
            //Check for a connection
            Socket newConnection = accept(serverSocket,NULL,NULL);
            if (newConnection != SOCKET_ERROR)
            {
                cout << "Incomming connection attempt";
                ConnectClient(newConnection);
            }
            Sleep(1);
        }


    WSACleanup();
    return 0;
}


void ConnectClient(Socket _newConnection)
{
    int slotIndex = FindFreePlayerSpot();
    if (slotIndex == -1)
    {
        closesocket(_newConnection);
        cout << "No player slots available, connection aborted"
    }

Two problems here.

1)It should be SOCKET, not Socket.

2) You're missing a semi-colon (;) at end of the cout statement above.

Fix those two errors above, the related compiler errors in main() and your code will at least compile. As to whether it works or not...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜