Multicast
if(Initialized)return ALREADYINIT;
if(Chanell>32||Chanell<1)return INVALIDCHANELL;
TimeOutValue=TimeOut;
CurrentChanell=(unsigned char)Chanell;
//init winsock2
memset(&wsaData,0,sizeof(WSADATA));
wsaVersion=MAKEWORD(2,0);
if(WSAStartup(wsaVersion,&wsaData)!=0)return WSA2STARTUP;
//init vars
//own ip
HostName=(char*)malloc(128);
gethostname(HostName,120);
HostData = new (struct hostent);
HostData=gethostbyname(HostName);
OwnIp=(char*)malloc(4);
memcpy(OwnIp,HostData->h_addr_list[0],4);
//creating sockets
SenderSock=socket(AF_INET,SOCK_DGRAM,0);
RecieverSock=socket(AF_INET,SOCK_DGRAM,0);
if(INVALID_SOCKET==SenderSock||INVALID_SOCKET==RecieverSock)return SOCKETSERROR;
GroupIP = (char*) malloc (14);
sprintf(GroupIP,"224.0.5.%d",Chanell);
bool v=true;
setsockopt(RecieverSock,SOL_SOCKET,SO_REUSEADDR,(char*)&v,sizeof(v));
memset(&SendStruct,0,sizeof(struct sockaddr_in));
memset(&sendbind,0,sizeof(struct sockaddr_in));
memset(&recievebind,0,sizeof(struct sockaddr_in));
//prepare stuct used for sends
SendStruct.sin_addr.s_addr=inet_addr(GroupIP);
SendStruct.sin_family=AF_INET;
SendStruct.sin_port=htons(DataPort);
//bind
sendbind.sin_addr.s_addr=inet_addr(INADDR_ANY);
recievebind.sin_addr.s_addr=inet_addr(GroupIP);//htonl(INADDR_ANY);
sendbind.sin_family=AF_INET;
recievebind.sin_family=AF_INET;
sendbind.sin_port=htons(DataPort);
recievebind.sin_port=htons(DataPort);
// if(bind(SenderSock,(struct sockaddr*)&sendbind,sizeof(struct sockaddr_in))==SOCKET_ERROR)return BINDERROR;
if(bind(RecieverSock,(struct sockaddr*)&recievebind,sizeof(struct sockaddr_in))==SOCKET_ERROR)return BINDERROR;
//multicast
memset(&MulticastReq,0,sizeof(MulticastReq));
MulticastReq.imr_multiaddr.s_addr=inet_addr(GroupIP);
MulticastReq.imr_interface.s_addr=htonl(INADDR_ANY);
//setsockopt
if(setsockopt(RecieverSock,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char*)&MulticastReq,sizeof(MulticastReq))==SOCKET_ERROR)return MULTICAST;
// if(setsockopt(SenderSock,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char*)&MulticastReq,sizeof(MulticastReq))==SOCKET_ERROR)return MULTICAST;
u_char loop = false;
if(setsockopt(RecieverSock,IPPROTO_IP,IP_MULTICAST_LOOP,(char*)&loop,sizeof(loop))==SOCKET_ERROR)return MULTICAST;
// if(setsockopt(SenderSock,IPPROTO_IP,IP_MULTICAST_LOOP,(char*)&loop,sizeof(loop))==SOCKET_ERROR)return MULTICAST;
//create recieve thread
if(NULL==CreateThread(NULL,0,RecieveThread,NULL,0,NULL))return THREADERROR;
//yooooo.... global init
Message=(char*)malloc(DATASIZE);
UserCount=0;
Initialized=true;
return SUCCESS;