Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.0 kB
2
Indexable
Never
 // =========================== SERVER.H =====================================
 #include<sys/types.h>
 #include<sys/socket.h>
 #include<netdb.h>

#include<string.h>
#include<stdio.h>

#define PORT 22032
#define PENDING_CON 0
#define COMMAND "03"
#define WEBSITE "www.yahoo.com"

 int CheckCommand(char *buffer, char *command){

 if(buffer[0] == command[0] && buffer[1] == command[1] && buffer[2] == '#'){
 return 1;
 }
 else{
 return 0;
 }

 }

int main(){
    char buffer_data_recv[100];
    int data_recv;
    char web_page[2000];

    int client_socket;
    int server_socket;
    
// SOCKET OPENING 
    int true = 1;
    server_socket = socket(AF_INET, SOCK_STREAM, 0);
 // Deschidere socket
 // AF_INET - IPv4
 // SOCK_STREAM - conectare TCP
    if(server_socket == -1){
    perror(" Socket was not opened. \n");}
    
    puts(" Socket was successfully opened.");
    

// BINDING THE SOCKET
    struct sockaddr_in serv_addr;
    serv_addr.sin_family = AF_INET; // IPv4
    serv_addr.sin_port = htons(PORT); // port
    serv_addr.sin_addr.s_addr = INADDR_ANY; //atuomatic selection of interface

 if(bind(server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1){
 perror(" Error binding the socket \n");
 }else{
 printf(" Bind was ok on port  %d \n ", PORT);
 }


// LISTENING TO THE SOCKET  

 if(listen(server_socket, PENDING_CON ) == -1){
 perror("\n Error waiting for client! \n");
 }else{
 puts("\n Waiting for client \n");
 }
 
 while(1){
    struct sockaddr_storage their_addr;
    socklen_t addr_size;

    addr_size = sizeof their_addr;
    int accept_socket = accept(server_socket, (struct sockaddr *)&their_addr, &addr_size);
    if(accept_socket == -1){
    perror(" Connecion was accepted \n");
    send(accept_socket, "Connection denited!\n", 100, 0);

    }else{
    puts(" Connecion was accepted! ");
    send(accept_socket, "Connecion was accepted! \n", 100, 0);

    }
    if(accept_socket == 0){
    continue;
         }
 

 do{
    data_recv = recv(accept_socket, buffer_data_recv, sizeof buffer_data_recv, 0);
    if(!CheckCommand(buffer_data_recv, COMMAND)){
    puts( " -> Comanda neimplementata!");
    send(accept_socket, "Comanda neimplementata! \n", 100, 0);
    continue;
    }else if(data_recv > 0){
    puts(" -> Comanda implementata ");
    }
 }
 while (data_recv>0);
     close(accept_socket);
     puts("Client deconectat");
 }
  return 0;
    }
   
    
 
 
 

 // Optiune Socket
 // - server_socket - socketul utilizat
 // - SOL_SOCKET - pentru cine ii definita optiunea
 // - SO_REUSEADDR - permite socket-ului sa faca bind la o adresa si port deja

 if(setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int)) == -1){
 perror(" -> Eroare la setarea optiuni \n");
 return 0;
 }

 return server_socket;
 }



 void bind_server_socket(int sock, int port){
 struct sockaddr_in serv_addr;

 serv_addr.sin_family = AF_INET; // IPv4
 serv_addr.sin_port = htons(port); // port
 serv_addr.sin_addr.s_addr = INADDR_ANY; //atuomatic selection of interface

 if(bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1){
 perror(" Error binding the socket \n");
 }else{
 printf(" Bind was ok on port  %d \n ", port);
 }

 }
 // -------- END FUNCTION ---------

 // -------- START LISTEN FUNCTION -------

 }
 // --------- END FUNCTION --------

 // --------- ACCEPT CONNECTIONS FUNCTION ---------
 int accept_connections(int sock){
 struct sockaddr_storage their_addr;
 socklen_t addr_size;

 addr_size = sizeof their_addr;
 int accept_socket = accept(sock, (struct sockaddr *)&their_addr, &addr_size);
 if(accept_socket == -1){
 perror(" Connecion was accepted \n");
 send(accept_socket, "Connection denited!\n", 100, 0);
 return 0;
 }else{
 puts(" Connecion was accepted! ");
 send(accept_socket, "Connecion was accepted! \n", 100, 0);
 return accept_socket;
 }
 }
 // ---------- END FUNCTION --------