Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Beatrix - ServerProtocol.cpp

ServerProtocol.cpp

Caricato da: Piero Tofy
Scarica il programma completo

  1. #include "ServerProtocol.h"
  2.  
  3.  
  4. /* ServerProtocol.cpp
  5.  
  6. Si veda il file ServerProtocol.h per l'utilizzo delle funzioni
  7. presenti nel modulo */
  8.  
  9. /* Apre il server su una porta e ritorna un riferimento al socket */
  10. SOCKET OpenServer(int iPort){
  11.     WORD sockVersion;
  12.     WSADATA wsaData;
  13.     int rVal;
  14.  
  15.         /* Il controllo Winsocket in windows richiede l'inizializazione */
  16.     sockVersion = MAKEWORD(2,0);
  17.     WSAStartup(sockVersion, &wsaData);
  18.        
  19.     /* Crea un socket internet, tcp, che usi il protocollo tcp/ip */
  20.     //SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  21.         SOCKET s = WSASocket (AF_INET, SOCK_STREAM, 0, 0, 0, 0);
  22.  
  23.  
  24.  
  25.     if(s == INVALID_SOCKET){
  26.         WSACleanup();
  27.         return SERVER_SOCKET_ERROR;
  28.     }
  29.  
  30.         /* Riempiamo la struttura */
  31.     SOCKADDR_IN sin;
  32.     sin.sin_family = PF_INET;
  33.     sin.sin_port = htons(iPort);
  34.     sin.sin_addr.s_addr = INADDR_ANY;
  35.  
  36.     /* Ok, proviamo a bindare il server */
  37.     rVal = bind(s, (LPSOCKADDR)&sin, sizeof(sin));
  38.     if(rVal == SOCKET_ERROR){
  39.         WSACleanup();
  40.         return SERVER_SOCKET_ERROR;
  41.     }
  42.  
  43.     /* Mettiti in ascolto per al massimo 1 client */
  44.     rVal = listen(s, 1);
  45.     if(rVal == SOCKET_ERROR){
  46.         WSACleanup();
  47.         return SERVER_SOCKET_ERROR;
  48.     }
  49.  
  50.         return s;
  51. }
  52.  
  53.  
  54. /* Ritorna un oggetto SOCKET alla prima connessione di un client */
  55. SOCKET WaitForClientConnection(SOCKET s){
  56.         SOCKET client;
  57.     client = accept(s, NULL, NULL);
  58.  
  59.     if(client == INVALID_SOCKET){
  60.         WSACleanup();
  61.         return SERVER_SOCKET_ERROR;
  62.     }
  63.  
  64.         /* TODO: Controllo password */
  65.  
  66.     return client;
  67. }
  68.  
  69. /* Funzione per ricevere un messaggio dalla socket */
  70. int RecvFrom(SOCKET s, char *pBuffer){
  71.         char *tmpBuffer = new char[BUFFER_LENGTH];
  72.         int res;
  73.  
  74.         /* Azzera il buffer */
  75.         *pBuffer = '\0';
  76.  
  77.         /* Riceve i dati fino a che non viene inviato il carattere '\n' */
  78.         do{
  79.                 res=recv(s,tmpBuffer,sizeof(tmpBuffer),0);
  80.                
  81.                 /* Se riceve un pacchetto non valido esce */
  82.                 if (tmpBuffer[0] == '\0') return SERVER_SOCKET_ERROR;
  83.  
  84.                 if (res>=0)     *(tmpBuffer+res)='\0';
  85.                 lstrcat(pBuffer,tmpBuffer);
  86.         }while(*(tmpBuffer+res-1) != '\n');
  87.  
  88.         return SERVER_SOCKET_OK;
  89. }
  90.  
  91. /* Funzione per inviare un messaggio alla socket */
  92. int SendTo(SOCKET s, char *pText){
  93.         return send(s, pText,lstrlen(pText),0);
  94. }
  95.  
  96. /* Funzione per identificare il tipo di comando ricevuto */
  97. char *GetCommand(char *pRecvBuf){
  98.         /* Il tipo di comando viene inviato alla testa di ogni pacchetto
  99.         quindi estraiamolo */
  100.         return GetToken(pRecvBuf,0);
  101. }
  102.  
  103. /* Funzione per prendere una parte del comando ricevuto dal server */
  104. char *GetToken(char *pRecvBuf, int iTokenNumber){
  105.         char *Buffer = new char[BUFFER_LENGTH];
  106.         memset(Buffer,'\0',BUFFER_LENGTH);
  107.         char *pBuffer = Buffer;
  108.         int iTokenCount = 0;
  109.         int iTokensCount = TokensCount(pRecvBuf)-1;
  110.  
  111.         /* Scorre il buffer fino ad incontrare il token desiderato... */
  112.         while((iTokenCount < iTokenNumber) && (*pRecvBuf)) if (*pRecvBuf++ == SEPARATOR) iTokenCount++;
  113.  
  114.         /* Inserisce nel buffer temporaneo il contenuto del token */
  115.         while((*pRecvBuf != SEPARATOR) && (*pRecvBuf)) *pBuffer++ = *pRecvBuf++;
  116.        
  117.         /* Tronca gli ultimi due caratteri se si tratta dell'ultimo token */
  118.         if (iTokensCount == iTokenNumber) *(pBuffer-2) = '\0';
  119.  
  120.         return Buffer;
  121. }
  122.  
  123. // Conta i Token di una stringa
  124. int TokensCount(char *pString) {
  125.         char *p = pString;
  126.         int iTemp = 1;
  127.  
  128.         while(*p) if (*p++ == SEPARATOR) iTemp++;
  129.  
  130.         return iTemp;
  131. }
  132.  
  133. /* Funzione per notificare l'avvenuta esecuzione del comando */
  134. void SendCommandNotify(SOCKET s, char *pCommand, bool Success){
  135.         char *pBuffer = new char[BUFFER_LENGTH];
  136.  
  137.         /* Aggiunge il valore di OK al comando e invia
  138.         i dati alla socket */
  139.         wsprintf(pBuffer,"%s%c%s\n\r\0",Success ? OKCOMMAND : NOCOMMAND,SEPARATOR,pCommand);
  140.         SendTo(s,pBuffer);
  141. }
  142.  
  143. /* Funzione per inizializzare una socket client */
  144. SOCKET InitClientSocket(char *pHost, int iPort){
  145.  struct sockaddr_in saClient;
  146.  struct hostent *pHostinfo;
  147.  SOCKET sock;
  148.  
  149.  WORD version;
  150.  WSADATA WSAData;
  151.  version=MAKEWORD(1,1);
  152.  WSAStartup(version, &WSAData);
  153.  
  154.  
  155.  //Avvia il socket
  156.  if ((sock=socket(AF_INET,SOCK_STREAM,0))==SOCKET_ERROR){
  157.   return 0;
  158.  }
  159.  
  160.  //Risolve il DNS
  161.  pHostinfo=gethostbyname(pHost);
  162.  if (pHostinfo==NULL){
  163.   return 0;
  164.  }
  165.  
  166.  //Imposta la connessione con il server...
  167.  saClient.sin_family=AF_INET;
  168.  saClient.sin_addr=*((struct in_addr *)pHostinfo->h_addr);
  169.  saClient.sin_port=htons(iPort);
  170.  
  171.  
  172.  //Si connette al server...
  173.  if (connect(sock,(struct sockaddr *)&saClient, sizeof(saClient))){
  174.   return 0;
  175.  }
  176.  
  177.  return sock;
  178. }