Jump to content
  • Advertisement
Sign in to follow this  
Johannes1991

Host - Clients connection

This topic is 2532 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++;
}
}
}

Share this post


Link to post
Share on other sites
Advertisement

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);


Share this post


Link to post
Share on other sites
Fixed it!

Solution!


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

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

[quote name='Johannes1991' timestamp='1315848307' post='4860760']

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.
[/quote]


I'll thead that bad boy then! ;P

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!