Jump to content
  • Advertisement
Sign in to follow this  
newbie123

UDP server that handle multiple clients

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

Hello

I have been trying for that past 2 days to create a server that handle multiple clients but i can't figure out how to do it. I've done my research but i still can't making things work.
i found some articles about this subject but all of the articles are using TCP not UDP.

what i have done so far is created a SOCKET array and I'm trying to bind them but its giving me an error 10048. its binding the first socket but its not binding the other sockets.

please I'm desperate. help
Server.h

#pragma once
#include <iostream>
#include <string>
#include <WinSock2.h>

class Host
{
public:
Host();
~Host();
void Init();
void Bind();
void RcevData();
void PrintData();
void CloseSocket();
void CleanUp();

private:
unsigned short Port;
static const int Max_n_Client = 5;
char RecvBuf[1024];
int iResult, BufLen, SenderAddrSize;
WSADATA wsaData;
SOCKET RecvSocket[Max_n_Client];
sockaddr_in RecvAddr, SenderAddr;
};


Server.cpp

#include "Host.h"

Host::Host()
{
iResult = 0;
Port = 27015;
BufLen = 1024;
SenderAddrSize = sizeof (SenderAddr);
}

Host::~Host()
{
}

void Host::Init()
{
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
std::cout<< "WSAStartup failed with error \n" << iResult;
else
std::cout<< "WSAStartup was Initionlized\n";


for(int i=0; i<Max_n_Client; i++)
{
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

if (RecvSocket == INVALID_SOCKET)
std::cout<< "socket failed with error \n"<< WSAGetLastError();
else
std::cout<< "socket " << i << " was Initionlized\n";
}
}

void Host::Bind()
{
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

for(int i=0; i<Max_n_Client; i++)
iResult = bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));

if (iResult != ERROR)
std::cout<< "bind failed with error \n" << WSAGetLastError();
else
{
std::cout<< "socket 1 was binded to port " << Port << std::endl;
std::cout<< "Waiting to receive data ...\n";
}


//Non blocking socket
u_long iMode=1;
for(int i=0; i<Max_n_Client; i++)
ioctlsocket(RecvSocket,FIONBIO,&iMode);
}

void Host::RcevData()
{
while(true)
{
for(int i=0; i<Max_n_Client; i++)
iResult = recvfrom(RecvSocket, RecvBuf, BufLen, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
PrintData();
}
}

void Host::PrintData()
{
if (iResult > 1)
{
std::cout<< "Client connected \n";

std::cout<<RecvBuf << "\n";
}

if(iResult == -1)
{
std::cout<< "Client disconected..\n";
}
}

void Host::CloseSocket()
{
std::cout<< "Finished receiving. Closing socket.\n";

for(int i=0; i<Max_n_Client; i++)
iResult = closesocket(RecvSocket);

if (iResult == SOCKET_ERROR)
std::cout<< "closesocket failed with error \n" << WSAGetLastError();
}

void Host::CleanUp()
{
std::cout<<"Exiting.\n";
WSACleanup();
}

Share this post


Link to post
Share on other sites
Advertisement
You can't bind multiple sockets to the same address. If you want all your clients to send data to the same address you only use one socket.

Share this post


Link to post
Share on other sites
You should use one socket for all clients as you can only bind one socket to the same address. Remember, UDP is connectionless, clients can all send to the same address.

Share this post


Link to post
Share on other sites
Hidden

You should use one socket for all clients as you can only bind one socket to the same address. Remember, UDP is connectionless, clients can all send to the same address.


ok thanks but how can I handle and keep track of all the clients data ? how can i tell which client sent what data ?

Share this post


Link to post

[quote name='Madhed' timestamp='1324851972' post='4897339']
You should use one socket for all clients as you can only bind one socket to the same address. Remember, UDP is connectionless, clients can all send to the same address.


ok thanks but how can I handle and keep track of all the clients data ? how can i tell which client sent what data ?
[/quote]

the recvfrom function gives you the address of the sender (Both IP and port) , http://msdn.microsoft.com/en-us/library/windows/desktop/ms740120%28v=vs.85%29.aspx
(Multiple users may share IP but will never share both IP and port) as for how you keep track of it, thats up to you.

Share this post


Link to post
Share on other sites

[quote name='FantasyVI' timestamp='1324852724' post='4897343']
[quote name='Madhed' timestamp='1324851972' post='4897339']
You should use one socket for all clients as you can only bind one socket to the same address. Remember, UDP is connectionless, clients can all send to the same address.


ok thanks but how can I handle and keep track of all the clients data ? how can i tell which client sent what data ?
[/quote]

the recvfrom function gives you the address of the sender (Both IP and port) , http://msdn.microsof...v=vs.85%29.aspx
(Multiple users may share IP but will never share both IP and port) as for how you keep track of it, thats up to you.
[/quote]

thanks that was very helpful. I'll try to fix the problem now

Share this post


Link to post
Share on other sites
ok lets say Client X with adress X and Port X sending string X to the server, and after sometime client Y with address Y and port Y send string Y to the server. How can store string X in buffer1 and string Y in buffer2 ?

Share this post


Link to post
Share on other sites

ok lets say Client X with adress X and Port X sending string X to the server, and after sometime client Y with address Y and port Y send string Y to the server. How can store string X in buffer1 and string Y in buffer2 ?


You'll have to differentiate based on the address (IP:port) returned by recvfrom().

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!