sysdis-project/sgca/lib/network/tcp/server.c

86 lines
2.4 KiB
C
Raw Permalink Normal View History

2017-04-09 16:56:11 +00:00
/**************************
* TCP Server Dependency *
***************************
* Designed & Developed by *
* Adrien Marquès *
* <xdrm-brackets> *
***************************
* doowap31@gmail.com *
**************************/
2017-04-01 15:01:36 +00:00
#include "server.h"
int DROP_TCP_SERVER(const int pPort, int* pListenSock){
if( DEBUGMOD&HDR ) printf("====== DROP_TCP_SERVER(%d, %p) ======\n\n", pPort, (void*) pListenSock);
/* [0] Initialisation des variables
=========================================================*/
static struct sockaddr_in addr; // contiendra les infos du serveur
int STATUS; // status
*pListenSock = -1;
/* [1] Création de la socket
=======================================================*/
/* 1. Création socket */
2017-04-01 15:01:36 +00:00
*pListenSock = socket(AF_INET, SOCK_STREAM, 0);
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] socket: %d\n", *pListenSock);
2017-04-01 15:01:36 +00:00
/* 2. Gestion erreur */
2017-04-01 15:01:36 +00:00
if( *pListenSock < 0 ) return -1;
/* [2] On définit les infos de la socket
=========================================================*/
/* (1) Reset des valeurs */
bzero(&addr, sizeof(struct sockaddr_in));
/* (2) On définit les infos */
addr.sin_family = AF_INET;
addr.sin_port = htons(pPort);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
/* [3] On publie la SOCKET
=======================================================*/
/* 1. Publication socket */
int count = -1;
do{
count++;
addr.sin_port = htons(pPort+count);
STATUS = bind(*pListenSock, (struct sockaddr*) &addr, sizeof(struct sockaddr_in));
if( DEBUGMOD&SAD ) printf("\tudp:%d used, trying udp:%d\n", pPort+count-1, pPort+count);
}while( STATUS < 0 && count < 10 );
2017-04-01 15:01:36 +00:00
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] bind: %d\n", STATUS);
2017-04-01 15:01:36 +00:00
/* 2. Gestion erreur */
2017-04-01 15:01:36 +00:00
if( STATUS < 0 ) return -1;
2017-04-01 15:01:36 +00:00
/* [4] On met la socket sur écoute
=======================================================*/
/* 1. Démarrage écoute */
2017-04-01 15:01:36 +00:00
STATUS = listen(*pListenSock, MAX_TCP_THR);
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] listen: %d\n", STATUS);
2017-04-01 15:01:36 +00:00
/* 2. Gestion erreur */
2017-04-01 15:01:36 +00:00
if( STATUS < 0 ) return -1;
2017-04-01 15:01:36 +00:00
/* [n] Code succès
=========================================================*/
return pPort+count;
2017-04-01 15:01:36 +00:00
}