2017-04-08 17:25:44 +00:00
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int UDP_SOCKET(int* pSocket, const char* pAddr, const int pPort, struct sockaddr_in* pInfo){
|
|
|
|
|
|
|
|
/* [0] Initialisation des variables
|
|
|
|
=========================================================*/
|
|
|
|
*pSocket = -1;
|
2017-04-12 07:57:59 +00:00
|
|
|
struct timeval timeout;
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [1] Création de la socket
|
|
|
|
=======================================================*/
|
|
|
|
/* 1. Création de la socket */
|
|
|
|
*pSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
/* 2. Gestion erreur */
|
|
|
|
if( *pSocket < 0 )
|
|
|
|
return -1;
|
|
|
|
|
2017-04-12 07:57:59 +00:00
|
|
|
/* 3. Timeout */
|
|
|
|
timeout.tv_sec = SOCK_TIMEOUT;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
if( setsockopt(*pSocket, SOL_SOCKET, SO_RCVTIMEO|SO_SNDTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){
|
|
|
|
close(*pSocket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
/* [2] On définit les infos de la socket
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Reset des valeurs */
|
|
|
|
bzero(pInfo, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
/* (2) On définit les infos */
|
|
|
|
pInfo->sin_family = AF_INET;
|
|
|
|
pInfo->sin_port = htons(pPort);
|
|
|
|
pInfo->sin_addr.s_addr = inet_addr(pAddr);
|
|
|
|
|
|
|
|
|
|
|
|
/* [n] Code succès
|
|
|
|
=========================================================*/
|
|
|
|
return 0;
|
|
|
|
}
|