Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Cliente envia uma série de comandos para o servidor */
- // Apple Xcode
- // main.c
- // cliente
- #include <netinet/in.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "funcoes.h"
- // stream read/write
- FILE *ligar_ao_servidor(struct hostent *the_host_info, int the_port)
- {
- struct sockaddr_in the_client_addr;
- int the_socket_ref;
- FILE *the_fp;
- // create an endpoint for communication
- the_socket_ref = socket(PF_INET, SOCK_STREAM, 0);
- if (the_socket_ref == -1)
- {
- perror("erro no socket");
- return NULL;
- }
- // fill a byte string with a byte value
- memset(&the_client_addr, 0, sizeof(the_client_addr));
- the_client_addr.sin_family = AF_INET;
- the_client_addr.sin_addr = *((struct in_addr *) the_host_info->h_addr);
- the_client_addr.sin_port = htons(the_port);
- if (connect(the_socket_ref, (struct sockaddr *) &the_client_addr, sizeof(struct sockaddr_in)) < 0)
- {
- perror("erro na ligacao");
- return NULL;
- }
- // stream open function
- the_fp = fdopen(the_socket_ref, "r+");
- if (the_fp == NULL)
- {
- close(the_socket_ref);
- }
- // return stream
- return the_fp;
- }
- int main(int argc, const char * argv[])
- {
- veiculo_t v1;
- int the_port;
- struct hostent* the_host_info;
- char the_user_option;
- char the_buffer[100];
- char *the_flag;
- FILE *the_fp;
- // input arguments
- if (argc != 3)
- {
- printf("utilizacao do programa: %s ip_servidor numero_porta\n", argv[0]);
- exit(1);
- }
- // do it only once
- // get network host entry
- the_host_info = gethostbyname(argv[1]);
- // convert ASCII string to integer
- the_port = atoi(argv[2]);
- do
- {
- printf("1: inserir registo\n");
- printf("2: imprimir registos\n");
- printf("3: apagar registo\n\n");
- printf("4: sair\n");
- // user option
- // get a line from a stream
- fgets(the_buffer, sizeof(the_buffer), stdin);
- // input format conversion
- sscanf(the_buffer, "%c", &the_user_option);
- switch (the_user_option)
- {
- case '1':
- // insert record
- ler_registo(&v1);
- // output record
- imprimir_registo(&v1);
- // connect to server
- the_fp = ligar_ao_servidor(the_host_info, the_port);
- if (the_fp == NULL)
- {
- printf("erro na ligacao.");
- return -1;
- }
- // send user option
- fputc('1', the_fp);
- // send proprietario
- // binary stream input/output
- fwrite(v1.proprietario, sizeof(char), strlen(v1.proprietario), the_fp);
- // send delimeter
- fputc('\n', the_fp);
- // send matricula
- fwrite(v1.matricula, sizeof(char), 6, the_fp);
- // close a stream
- fclose(the_fp);
- break;
- case '2':
- // output all records
- the_fp = ligar_ao_servidor(the_host_info, the_port);
- if (the_fp == NULL)
- {
- printf("erro na ligacao.");
- return -1;
- }
- // send user option
- fputc('2', the_fp);
- // read data
- while (fgets(the_buffer, sizeof(the_buffer), the_fp) != NULL)
- {
- // read proprietario
- strcpy(v1.proprietario, the_buffer);
- the_flag = strchr(v1.proprietario, '\n');
- *the_flag = '\0';
- // read matricula
- fread (v1.matricula, sizeof(char), 6, the_fp);
- v1.apagado = '\0';
- imprimir_registo(&v1);
- }
- // close a stream
- fclose (the_fp);
- break;
- case '3':
- // delete record
- printf("matricula a eliminar: ");
- fgets(the_buffer, sizeof(the_buffer), stdin);
- the_fp = ligar_ao_servidor(the_host_info, the_port);
- if (the_fp == NULL)
- {
- printf("erro na ligacao.");
- return -1;
- }
- // send user option
- fputc('3', the_fp);
- // send matricula
- fwrite(the_buffer, sizeof(char), 6, the_fp);
- // close a stream
- fclose(the_fp);
- break;
- }
- // do it until user press 4
- } while (the_user_option != '4');
- return 0;
- }
- //
- // funcoes.c
- // cliente
- #include "funcoes.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // read record
- void ler_registo(veiculo_t *v)
- {
- char the_buffer[81];
- char *the_temp;
- // ini
- v->apagado = '\0';
- // read proprietario
- printf("proprietario: ");
- // get a line from a stream
- fgets(the_buffer, sizeof(the_buffer), stdin);
- // remove char '\n'
- // locate character in string
- the_temp = strchr(the_buffer, '\n');
- if (the_temp != NULL)
- {
- *the_temp = '\0';
- }
- // copy strings
- strcpy(v->proprietario, the_buffer);
- // read matricula
- printf("matricula: ");
- // get a line from a stream
- fgets(the_buffer, sizeof(the_buffer), stdin);
- // copy only 6 chars
- // copy memory area
- memcpy(v->matricula, the_buffer, sizeof(v->matricula));
- }
- // print record
- void imprimir_registo(veiculo_t *v)
- {
- // 0: if record deleted: ignored
- if (v->apagado == '\0')
- {
- int i;
- char the_aux[7];
- // copy matricula to the_aux[] as string
- for (i = 0; i < 6; i++)
- {
- the_aux[i] = v->matricula[i];
- }
- the_aux[i] = '\0';
- printf("proprietario: %-10s com matricula: %s apagada: %d\n", v->proprietario, the_aux, v->apagado);
- }
- }
- //
- // funcoes.h
- // cliente
- #ifndef HEADER
- #define HEADER
- #define NAME_BUFFER_SIZE 80
- typedef struct
- {
- char matricula[6];
- char proprietario[NAME_BUFFER_SIZE];
- char apagado; // 0: record not deleted
- } veiculo_t;
- void ler_registo(veiculo_t *v);
- void imprimir_registo(veiculo_t *v);
- #endif //HEADER
- //
- // main.c
- // servidor
- #include "srv_aux.h"
- #include <netinet/in.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <arpa/inet.h>
- #include <string.h>
- #include <signal.h>
- int criar_socket_servidor_inet_sock_stream(int the_port);
- void atende_pedido(int);
- void sigalrm_handler(int signum)
- {
- printf("timeout!\n");
- }
- int main(int argc, const char * argv[])
- {
- int ns, s;
- unsigned int the_client_length;
- struct sockaddr_in the_client_addr;
- // input arguments
- if (argc != 2)
- {
- printf("utilizacao do programa: %s numero_porta\n", argv[0]);
- exit(1);
- }
- // simplified software signal facilities
- signal(SIGPIPE, SIG_IGN);
- // needed to accept SIGALRM from func alarm
- struct sigaction act;
- act.sa_handler = sigalrm_handler;
- act.sa_flags = 0;
- sigemptyset(&(act.sa_mask));
- sigaction(SIGALRM, &act, NULL);
- s = criar_socket_servidor_inet_sock_stream(atoi(argv[1]));
- while(1)
- {
- printf("a aguardar ligacao:\n");
- the_client_length = sizeof(the_client_addr);
- // accept connection
- ns = accept(s, (struct sockaddr *) &the_client_addr, &the_client_length);
- if (ns == -1)
- {
- perror("erro accept");
- sleep(1);
- continue;
- }
- // ip and port
- printf("ligacao, ip: %s, porta: %d\n", inet_ntoa(*((struct in_addr *) &(the_client_addr.sin_addr))), ntohs(the_client_addr.sin_port));
- atende_pedido(ns);
- }
- return 0;
- }
- void atende_pedido(int ns)
- {
- // stream open function
- FILE *the_fp = fdopen(ns, "r+");
- if (the_fp == NULL)
- {
- perror("erro na abertura");
- close(ns);
- return;
- }
- // get next character or word from input stream
- char the_user_option = fgetc(the_fp);
- printf("operacao: %c\n", the_user_option);
- switch(the_user_option)
- {
- case '1':
- // server add record requested by the client
- srv_adicionarRegisto(the_fp);
- break;
- case '2':
- // server outputs records requested by client
- srv_listarRegistos(the_fp);
- break;
- case '3':
- // server delete record requested by the client
- srv_eliminarRegisto(the_fp);
- break;
- default:
- // invalid option
- printf("opcao invalida\n");
- break;
- }
- // close a stream
- fclose(the_fp);
- }
- int criar_socket_servidor_inet_sock_stream(int the_port)
- {
- int the_socket_ref;
- struct sockaddr_in the_serv_addr;
- // create an endpoint for communication
- the_socket_ref = socket(PF_INET, SOCK_STREAM, 0);
- if (the_socket_ref == -1)
- {
- perror("erro no socket");
- exit(1);
- }
- // fill a byte string with a byte value
- memset(&the_serv_addr, 0, sizeof(the_serv_addr));
- the_serv_addr.sin_family = AF_INET;
- the_serv_addr.sin_addr.s_addr = INADDR_ANY;
- the_serv_addr.sin_port = htons(the_port);
- if (bind(the_socket_ref, (struct sockaddr *)&the_serv_addr, sizeof(the_serv_addr)) < 0)
- {
- perror("erro na atrib");
- exit(1);
- }
- // listen for connections on a socket
- listen(the_socket_ref, 5);
- // the return value is a descriptor referencing the socket
- return(the_socket_ref);
- }
- //
- // srv_aux.c
- // servidor
- #include "srv_aux.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #define FILE_NAME "dados.dat"
- void srv_adicionarRegisto(FILE *the_fp)
- {
- char the_buffer[100];
- char *the_search;
- // binary stream input
- fread(the_buffer, sizeof(char), sizeof(the_buffer), the_fp);
- // search for delimiter
- // locate character in string
- the_search = strchr(the_buffer, '\n');
- *the_search = '\0';
- veiculo_t registo;
- // copy memory area
- memcpy(registo.proprietario, the_buffer, the_search + 1 - the_buffer);
- the_search++;
- // copy memory area
- memcpy(registo.matricula, the_search, 6);
- // insert record into file
- // stream open function: file
- FILE *the_file = fopen(FILE_NAME, "a");
- if (the_file != NULL)
- {
- // binary stream input/output: file
- fwrite(®isto, sizeof(veiculo_t), 1, the_file);
- imprimir_registo(®isto);
- // close a stream: file
- fclose (the_file);
- }
- }
- // output records
- void srv_listarRegistos(FILE *the_fp)
- {
- // stream open function: file
- FILE *the_file = fopen(FILE_NAME, "r");
- if (the_file == NULL)
- {
- printf("erro na abertura do ficheiro\n");
- return;
- }
- veiculo_t v1;
- // fread: binary stream input
- while (fread(&v1, sizeof(veiculo_t), 1, the_file) != 0)
- {
- // if record not deleted
- if (v1.apagado == 0)
- {
- // write proprietario to file
- fwrite(v1.proprietario, sizeof (char), strlen(v1.proprietario), the_fp);
- // displays proprietario
- puts(v1.proprietario);
- // write delimeter to file
- fputc('\n', the_fp);
- // write matricula to file
- fwrite(v1.matricula, sizeof (char), 6, the_fp);
- }
- }
- // close a stream: file
- fclose (the_file);
- }
- void srv_eliminarRegisto(FILE *the_fp)
- {
- // stream open function: file
- FILE *the_file = fopen(FILE_NAME, "r+");
- if (the_file == NULL)
- {
- printf("erro na abertura do ficheiro\n");
- return;
- }
- veiculo_t veiculo;
- char the_file_value[7];
- char the_comp_value[20];
- int the_num_of_elements = 0;
- // from socket
- // binary stream input
- fread(the_comp_value, sizeof(char), sizeof(the_comp_value), the_fp);
- the_comp_value[6] = '\0';
- while (fread(&veiculo, sizeof(veiculo_t), 1, the_file) != 0)
- {
- // get right position
- the_num_of_elements++;
- // matricula to string
- // copy memory area
- memcpy(the_file_value, veiculo.matricula, 6);
- the_file_value[6] = '\0';
- // if matricula exits, set record matricula to apagado = 1
- // compare strings
- if (strcmp(the_comp_value, the_file_value) == 0)
- {
- // delete record
- veiculo.apagado = '1';
- // point to right position
- // reposition a stream
- fseek(the_file, (the_num_of_elements - 1) * sizeof(veiculo_t), SEEK_SET);
- // write it back
- fwrite(&veiculo, sizeof(veiculo_t), 1, the_file);
- break;
- }
- }
- // close a stream: file
- fclose(the_file);
- }
- // print record
- void imprimir_registo(veiculo_t *v)
- {
- // 0: if record deleted: ignored
- if (v->apagado == '\0')
- {
- int i;
- char the_aux[7];
- // copy matricula to the_temp[] as string
- for (i = 0; i < 6; i++)
- {
- the_aux[i] = v->matricula[i];
- }
- the_aux[i] = '\0';
- printf("proprietario: %-10s com matricula: %s apagada: %d\n", v->proprietario, the_aux, v->apagado);
- }
- }
- //
- // srv_aux.h
- // servidor
- #ifndef INCLUDE_SRV_AUX_H
- #define INCLUDE_SRV_AUX_H
- #define NAME_BUFFER_SIZE 80
- #include <stdio.h>
- typedef struct
- {
- char matricula[6];
- char proprietario[NAME_BUFFER_SIZE];
- char apagado; // 0: record not deleted
- } veiculo_t;
- void srv_adicionarRegisto(FILE *the_fp);
- void srv_listarRegistos(FILE *the_fp);
- void srv_eliminarRegisto (FILE *the_fp);
- void imprimir_registo(veiculo_t *v);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement