Jump to content
  • Advertisement
Sign in to follow this  
thekiwimaddog

Winsock Problem

This topic is 2902 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

Hi everyone!

Hi, I'm trying to adapt some sample winsock code I've found to support multiple clients. But I'm having some issues and was hoping someone could give me some pointers.

Here's what I have:

#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")

int main()
{
const int nMaxClients=5;
int nClient=0;
SOCKET Socket[nMaxClients];
SOCKET ServerSocket=NULL;
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
{
std::cout<<"WSA Initialization failed!\r\n";
WSACleanup();
system("PAUSE");
return 0;
}

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

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

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

listen(ServerSocket,SOMAXCONN);
while(1==1)//Loop
{
std::cout<<"Check For New Clients...\r\n";
Socket[nClient] = accept(ServerSocket,NULL,NULL);<--Freezes Here

if(Socket[nClient] != SOCKET_ERROR)
{
// If iMode!=0, non-blocking mode is enabled.
u_long iMode=1;
ioctlsocket(Socket[nClient],FIONBIO,&iMode);
std::cout<<"Client connected!\r\n\r\n";
nClient++;
}

char *szMessage="Hello Client!\r\n";
for(int i=0;i<nClient;i++)
{
send(Socket,szMessage,strlen(szMessage),0);

int nError=WSAGetLastError();
if(nError!=WSAEWOULDBLOCK&&nError!=0)
{
std::cout<<"Winsock error code: "<<nError<<"\r\n";
std::cout<<"Client disconnected!\r\n";

// Shutdown our socket
shutdown(Socket,SD_SEND);

// Close our socket entirely
closesocket(Socket);

break;
}
}

Sleep(1000);

}


WSACleanup();
system("PAUSE");
return 0;
}


The first time the Loop runs it works fine and contact the client. However, then after the program freezes on when I try and Accept Any More Connections:
Socket[nClient] = accept(ServerSocket,NULL,NULL);

Any Help Would Be Great!

Thanks In Advance.
David

Share this post


Link to post
Share on other sites
Advertisement
accept() will block by default and wait for a socket to connect before accept() returns.

You can put a socket into non-blocking mode by calling ioctlsocket() with FIONBIO:

unsigned long nNonblocking = 1;
ioctlsocket(ServerSocket, FIONBIO, &nNonblocking);

You'll want to put that code in just before you enter the infinite loop (while(1==1)).

Share this post


Link to post
Share on other sites
Ahh Thanks!!
I was setting that for all the client sockets but not the server socket!

Would I need to do this for all the Clients or just the Server??

Thanks Again!!

David

Share this post


Link to post
Share on other sites
Quote:
Original post by thekiwimaddog
Ahh Thanks!!
I was setting that for all the client sockets but not the server socket!

Would I need to do this for all the Clients or just the Server??

Thanks Again!!

David
Ah, whoops - I missed that bit in your code [smile]. You'll need to do it for each socket that you want to be non-blocking, the non-blocking state isn't inherited from listening socket to accepted client socket.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!