1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| /*
* multisocket.cpp
*
* Created on: 11 juil. 2017
* Author: begau
*/
#include "multiSocketSendto.h"
#define MSGBUFSIZE 1024
void init_multisendto(void)
{
WSADATA WSAData; // Contains details of the Winsock implementation
#ifdef _WIN32
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
{
printf("Error n°%d : Error during initialization.\n", SOCKET_ERRNO);
exit(SOCKET_ERRNO);
}
#endif
}
int init_connection_multisendto (struct sockaddr_in *addr, int PORT, char *GROUP_IP){
int msock;
int sizeaddr = sizeof(addr);
//create what looks like an ordinary UDP socket
if ((msock=socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
printf("Error n%d : Unable to initialize socket.\n",SOCKET_ERRNO);
//exit(SOCKET_ERRNO);
return SOCKET_ERRNO;
}
//set up destination address
memset(addr,0,sizeaddr);
addr->sin_family=AF_INET;
addr->sin_addr.s_addr=inet_addr(GROUP_IP);
addr->sin_port=htons(PORT);
return msock;
}
//now just sendto() our destination!
int write_multisendto(SOCKET msock, struct sockaddr_in *addr, char *rq)
{
int sizeaddr = sizeof(addr);
if (sendto(msock, rq, strlen(rq)+1, 0, (struct sockaddr*) addr,
sizeaddr )<0)
{
printf("Error n%d : Unable to send message.\n",SOCKET_ERRNO);
//exit(SOCKET_ERRNO);
return SOCKET_ERRNO;
}
return 0;
}
void end_multisendto(void)
{
#ifdef WIN32
WSACleanup();
#endif
}
void end_connection_multisendto(SOCKET sock)
{
if (closesocket(sock) != 0)
{
printf("Error closing the socket.\n");
exit(SOCKET_ERRNO);
}
} |
Partager