Jump to content

  • Log In with Google      Sign In   
  • Create Account


Host - Clients connection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Johannes1991   Members   -  Reputation: 428

Like
0Likes
Like

Posted 12 September 2011 - 09:48 AM

I recently jumped in to Network programming and i've some consol chatting programs. All of the tutorials out there are either Server to Client or peer to peer connection. I want to do a Host and it's just a server with a player on the same machine. So i'm using asynchronization method on both the host/server and client. But I can only detect if a client connects but i can't send data or recieve from it. I think the problem is that because it's non-blocking accept doesn't give me a valid socket when accept is called.

code:




LRESULT CALLBACK MainProcedure(HWND hWnd, UINT Message,WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		case WM_ONSOCKET:
			{
				switch (WSAGETSELECTEVENT(lParam))
				{
				case FD_WRITE: 
					break;
  				case FD_READ: 
					WBOX(L"Package recieved");
					break;
				case FD_CONNECT:
    					break;
				case FD_ACCEPT:					
					break;
				case FD_CLOSE:

					break;
				}		
			}break;
	};
	return DefWindowProc(hWnd, Message, wParam, lParam);
}
void Init_Network(bool client)
{


	if(WSAStartup(MAKEWORD(2,2), &Winsock) == WSAVERNOTSUPPORTED)
	{
		WSACleanup();
		WBOX(L"Can't start winsock");
		return;
	}
	
	Socket = socket(AF_INET, SOCK_STREAM, 0); 
	WSAAsyncSelect(Socket, GameWindow.hWnd, WM_ONSOCKET, FD_READ | FD_WRITE | FD_CONNECT | FD_ACCEPT | FD_CLOSE);

	if(Socket == SOCKET_ERROR)
	{
		WSACleanup();
		WBOX(L"Can't create socket");
		return;
	}

	if(Client)
	{
		ZeroMemory(&HostAddress, sizeof(HostAddress));
		HostAddress.sin_family = AF_INET;
		HostAddress.sin_port = htons(27777);
		HostAddress.sin_addr.s_addr = inet_addr("192.168.0.151");
		connect(Socket, (sockaddr*) &HostAddress, sizeof(HostAddress));
	}
	else
	{
		ZeroMemory(&HostAddress, sizeof(HostAddress));
		HostAddress.sin_family = AF_INET;
		HostAddress.sin_port = htons(27777);
		HostAddress.sin_addr.s_addr = htonl (INADDR_ANY);

		if(bind(Socket, (sockaddr*) &HostAddress, sizeof(HostAddress)) == SOCKET_ERROR)
		{
			WBOX(L"Can't bind Socket");
			int Error = WSAGetLastError();
			WSACleanup();
			return;
		}
		
		if(listen(Socket, MaxNetworkPlayers) == SOCKET_ERROR)
		{
			WBOX(L"Can't listen to Port");
			WSACleanup();
			return;
		}

		while(NumberOfClients < MaxNetworkPlayers)
		{
			ClientSockets[NumberOfClients] = accept(Socket, (sockaddr*) &ClientAddresses[NumberOfClients], &AddressSize);
			NumberOfClients++;
		}
	}
}

EnJOJ Gaming

Sponsor:

#2 IceBreaker23   Members   -  Reputation: 596

Like
0Likes
Like

Posted 12 September 2011 - 09:55 AM

where do you send or receive something?

#3 Johannes1991   Members   -  Reputation: 428

Like
0Likes
Like

Posted 12 September 2011 - 10:16 AM

where do you send or receive something?



ops i accidentally deleted those. In
case FD_ACCEPT:
send(GameWindow.Network.ClientSockets[0], (char*) '1', 1, 0);
case FD_READ:
recv(GameWindow.Network.Socket, (char *) &Con, 1, 0);



EnJOJ Gaming

#4 Johannes1991   Members   -  Reputation: 428

Like
0Likes
Like

Posted 12 September 2011 - 11:25 AM

Fixed it!

Solution!

while(NumberOfClients < MaxNetworkPlayers)
{
   	ClientSockets[NumberOfClients] = accept(Socket, (sockaddr*) &ClientAddresses[NumberOfClients], &AddressSize);
        if(ClientSockets[NumberOfClients] != SOCKET_ERROR)
        {
              NumberOfClients++;
     	}
}

EnJOJ Gaming

#5 hplus0603   Moderators   -  Reputation: 4960

Like
0Likes
Like

Posted 12 September 2011 - 02:12 PM

while(NumberOfClients < MaxNetworkPlayers)
{
   	ClientSockets[NumberOfClients] = accept(Socket, (sockaddr*) &ClientAddresses[NumberOfClients], &AddressSize);
        if(ClientSockets[NumberOfClients] != SOCKET_ERROR)
        {
              NumberOfClients++;
 		}
}


So, nobody can send or receive any packets until the maximum number of players has joined the game? That doesn't seem right to me.
enum Bool { True, False, FileNotFound };

#6 Johannes1991   Members   -  Reputation: 428

Like
0Likes
Like

Posted 13 September 2011 - 09:27 AM


while(NumberOfClients < MaxNetworkPlayers)
{
   	ClientSockets[NumberOfClients] = accept(Socket, (sockaddr*) &ClientAddresses[NumberOfClients], &AddressSize);
        if(ClientSockets[NumberOfClients] != SOCKET_ERROR)
        {
              NumberOfClients++;
 		}
}


So, nobody can send or receive any packets until the maximum number of players has joined the game? That doesn't seem right to me.



I'll thead that bad boy then! ;P


EnJOJ Gaming

#7 rip-off   Moderators   -  Reputation: 7642

Like
0Likes
Like

Posted 13 September 2011 - 09:51 AM

This doesn't require threading, if that is what you are saying.

#8 Nettention   Members   -  Reputation: 100

Like
0Likes
Like

Posted 16 September 2011 - 12:12 AM

I recently jumped in to Network programming and i've some consol chatting programs. All of the tutorials out there are either Server to Client or peer to peer connection. I want to do a Host and it's just a server with a player on the same machine. So i'm using asynchronization method on both the host/server and client. But I can only detect if a client connects but i can't send data or recieve from it. I think the problem is that because it's non-blocking accept doesn't give me a valid socket when accept is called.

code:




LRESULT CALLBACK MainProcedure(HWND hWnd, UINT Message,WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_ONSOCKET:
            {
                switch (WSAGETSELECTEVENT(lParam))
                {
                case FD_WRITE: 
                    break;
                  case FD_READ: 
                    WBOX(L"Package recieved");
                    break;
                case FD_CONNECT:
                        break;
                case FD_ACCEPT:                    
                    break;
                case FD_CLOSE:

                    break;
                }        
            }break;
    };
    return DefWindowProc(hWnd, Message, wParam, lParam);
}
void Init_Network(bool client)
{


    if(WSAStartup(MAKEWORD(2,2), &Winsock) == WSAVERNOTSUPPORTED)
    {
        WSACleanup();
        WBOX(L"Can't start winsock");
        return;
    }
    
    Socket = socket(AF_INET, SOCK_STREAM, 0); 
    WSAAsyncSelect(Socket, GameWindow.hWnd, WM_ONSOCKET, FD_READ | FD_WRITE | FD_CONNECT | FD_ACCEPT | FD_CLOSE);

    if(Socket == SOCKET_ERROR)
    {
        WSACleanup();
        WBOX(L"Can't create socket");
        return;
    }

    if(Client)
    {
        ZeroMemory(&HostAddress, sizeof(HostAddress));
        HostAddress.sin_family = AF_INET;
        HostAddress.sin_port = htons(27777);
        HostAddress.sin_addr.s_addr = inet_addr("192.168.0.151");
        connect(Socket, (sockaddr*) &HostAddress, sizeof(HostAddress));
    }
    else
    {
        ZeroMemory(&HostAddress, sizeof(HostAddress));
        HostAddress.sin_family = AF_INET;
        HostAddress.sin_port = htons(27777);
        HostAddress.sin_addr.s_addr = htonl (INADDR_ANY);

        if(bind(Socket, (sockaddr*) &HostAddress, sizeof(HostAddress)) == SOCKET_ERROR)
        {
            WBOX(L"Can't bind Socket");
            int Error = WSAGetLastError();
            WSACleanup();
            return;
        }
        
        if(listen(Socket, MaxNetworkPlayers) == SOCKET_ERROR)
        {
            WBOX(L"Can't listen to Port");
            WSACleanup();
            return;
        }

        while(NumberOfClients < MaxNetworkPlayers)
        {
            ClientSockets[NumberOfClients] = accept(Socket, (sockaddr*) &ClientAddresses[NumberOfClients], &AddressSize);
            NumberOfClients++;
        }
    }
}



[font="맑은 고딕"]I think, it is incorrect to usage of non-blocking socket.[/font]

[font="맑은 고딕"]You should call accept when FD_ACCEPT has arrived first, otherwise you can’t get valid socket. [/font]


The ProudNet is powerful server & network engine for small or large-scale multiplayer online game.
http://www.nettention.com




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS