Archived

This topic is now archived and is closed to further replies.

yungivan

How to get the source ip by winsock?

Recommended Posts

yungivan    122
Hi all, I have 2 questions: 1. How to get the source ip by winsock? 2. Is there a simpler way to broadcast message to all connected clients instead of storing all clients'' ip in an array?

Share this post


Link to post
Share on other sites
Marius    100
Use UDP. here a sample class that does the job
class CZSocket
{
public:
CZSocket(){
memset(&_local_sin,0,sizeof(_local_sin));
memset(&_remote_sin,0,sizeof(_remote_sin));
memset(&_mcastGrp,0,sizeof(_mcastGrp));
_sock = INVALID_SOCKET;
};
virtual ~CZSocket(){};

void Listen(int i)
{
listen(_sock,i);
}
operator SOCKET(){return _sock;}
operator HANDLE(){return (HANDLE)_sock;}

bool AsClient(DWORD ip, int port)
{
_sock = socket(AF_INET,SOCK_STREAM,0);
if(_sock == INVALID_SOCKET)
{
_error = WSAGetLastError();
return false;
}

_local_sin.sin_family = AF_INET;
_local_sin.sin_addr.s_addr = htonl(ip);
_local_sin.sin_port = htons(port);

_error = connect(_sock,(const struct sockaddr*)&_local_sin,sizeof(_local_sin));
return _error==0;
}

int Send(BYTE* pb, int len)
{
return send(_sock,(char *)pb,len,0);
}


int Receive(BYTE* pb, int len)
{
return recv(_sock,(char *)pb,len,0);
}


bool AsClient(const char* sip, int port)
{
_sock = socket(AF_INET,SOCK_STREAM,0);
if(_sock == INVALID_SOCKET)
{
_error = WSAGetLastError();
return false;
}

_local_sin.sin_family = AF_INET;
_local_sin.sin_addr.s_addr = inet_addr(sip);
_local_sin.sin_port = htons(port);
_error = connect(_sock,(const struct sockaddr*)&_local_sin,sizeof(_local_sin));
return _error==0;
}



bool Accept(CZSocket& cliSock)
{
int clilen = sizeof(cliSock._remote_sin);
cliSock._sock = accept(_sock,
(struct sockaddr*)&cliSock._remote_sin,
&clilen);
if(_sock == INVALID_SOCKET)
{
_error = WSAGetLastError();
return false;
}
return true;
}


void Close()
{
if(_sock>0)
{
if(_groupmember )
{
_error = setsockopt (_sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char FAR *)&_mcastGrp, sizeof (_mcastGrp)) ;
}
shutdown (_sock, 0x02);
closesocket(_sock);
}
_sock=INVALID_SOCKET;
}

int AsListener(int port)
{
_sock = socket(AF_INET,SOCK_STREAM,0);
if(_sock < 0)
{
return _error = WSAGetLastError();
}
_local_sin.sin_family = AF_INET;
_local_sin.sin_addr.s_addr = htonl(INADDR_ANY);
_local_sin.sin_port = htons(port);
_error = bind(_sock,(struct sockaddr*)&_local_sin,sizeof(_local_sin));
if(_error < 0)
{
_error = WSAGetLastError();
Close();
return _error;
}
return 0;
}


int AsMulticastReceiver(const char* ipGrp, int port)
{
_sock = socket (AF_INET, SOCK_DGRAM, 0);
if (_sock == INVALID_SOCKET)
{
return _error = WSAGetLastError();
}
//server receives
_local_sin.sin_family = AF_INET;
_local_sin.sin_port = htons (port);
_local_sin.sin_addr.s_addr = htonl (INADDR_ANY);

_error = bind (_sock, (struct sockaddr FAR *) &_local_sin, sizeof (_local_sin));
if (_error == INVALID_SOCKET)
{
_error = WSAGetLastError();
Close();
return _error;
}

//join the multicast group
_mcastGrp.imr_multiaddr.s_addr = inet_addr (ipGrp);
_mcastGrp.imr_interface.s_addr = INADDR_ANY;

_error = setsockopt (_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&_mcastGrp, sizeof (_mcastGrp)) ;
if (_error == INVALID_SOCKET)
{
_error = WSAGetLastError();
Close();
return _error;
}
_groupmember = true;

return 0;
}

int MulticastRec(BYTE* pbuff, int length)
{
int iRecvLen = sizeof (_remote_sin);

_error = recvfrom (_sock, (char*)pbuff, length, 0,(struct sockaddr FAR *) &_remote_sin, &iRecvLen);
if (_error == INVALID_SOCKET)
{
Close();
return _error;
}
return _error;
}


int AsMulticastSender()
{
_sock = socket(AF_INET,SOCK_DGRAM,0);
if(_sock == INVALID_SOCKET)
{
return _error = WSAGetLastError();
}

// from server
_local_sin.sin_family = AF_INET;
_local_sin.sin_port = htons (0);
_local_sin.sin_addr.s_addr = htonl (INADDR_ANY);

if (bind (_sock, (struct sockaddr*) &_local_sin, sizeof (_local_sin)) == SOCKET_ERROR)
{
_error = WSAGetLastError();
Close();
return _error;
}
int iOptVal = 64; //time to live
if (setsockopt (_sock, IPPROTO_IP, IP_MULTICAST_TTL,
(char FAR *)&iOptVal, sizeof (int)) == SOCKET_ERROR)
{
_error = WSAGetLastError();
Close();
return _error;
}

return 0;
}

int SendTo(const char* ipGrp, int port, BYTE* buff, int length)
{
// to client
_remote_sin.sin_family = AF_INET;
_remote_sin.sin_port = htons (port);
_remote_sin.sin_addr.s_addr = inet_addr(ipGrp);
_error = sendto (_sock, (char*)buff, length, 0, (struct sockaddr FAR *) &_remote_sin, sizeof (_remote_sin)) ;
if(SOCKET_ERROR == _error)
{
Close();
return _error;
}
return _error;
}

struct ip_mreq _mcastGrp;
struct sockaddr_in _local_sin; // source
struct sockaddr_in _remote_sin; // dest
SOCKET _sock;
int _error;
bool _groupmember;
};

// server
CZSocket smultic;
smultic.AsMulticastSender();
// broadacst to all subscribers "234.100.99.97"
smultic.SendTo("234.100.99.97", 1234, (BYTE*)&netBuff,
sizeof(netBuff));
smultic.Close()

and the client

CZSocket pDgram;
pDgram.AsMulticastReceiver("234.100.99.97", 1234)) // this connects to multicast IP 234.100.99.97
int oneshoot = pDgram.MulticastRec(buffer, bufferSize);
pDgram.Close();

Works fine

MCO

Share this post


Link to post
Share on other sites