2017-04-11 17:12:34 +00:00
|
|
|
/**************************
|
|
|
|
* TCP Client Dependency *
|
|
|
|
***************************
|
|
|
|
* Designed & Developed by *
|
|
|
|
* Adrien Marquès *
|
|
|
|
* <xdrm-brackets> *
|
|
|
|
***************************
|
|
|
|
* doowap31@gmail.com *
|
|
|
|
**************************/
|
2017-04-08 17:25:44 +00:00
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
int TCP_CONNECT(int* pSocket, const in_addr_t pAddr, const int pPort, const time_t pTimeout, struct sockaddr_in* pInfo){
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
/* [0] Initialisation des variables
|
|
|
|
=========================================================*/
|
2017-04-28 12:54:02 +00:00
|
|
|
struct timeval timeout;
|
2017-04-08 17:25:44 +00:00
|
|
|
*pSocket = -1;
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] Création de la socket
|
|
|
|
=======================================================*/
|
|
|
|
/* 1. Création de la socket */
|
|
|
|
*pSocket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
/* 2. Gestion erreur */
|
|
|
|
if( *pSocket < 0 )
|
|
|
|
return -1;
|
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
/* 3. Gestion timeout */
|
|
|
|
timeout.tv_sec = pTimeout;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
if( setsockopt(*pSocket, SOL_SOCKET, SO_RCVTIMEO, (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);
|
2017-04-26 12:25:05 +00:00
|
|
|
pInfo->sin_addr.s_addr = pAddr;
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [3] On se connecte au serveur
|
|
|
|
=========================================================*/
|
|
|
|
if( connect(*pSocket, (struct sockaddr*) pInfo, sizeof(struct sockaddr_in)) < 0 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [n] Code succès
|
|
|
|
=========================================================*/
|
|
|
|
return 0;
|
|
|
|
}
|