commit 402dcd042153b74e5ddebe0c984d76f65beec0ad Author: xdrm-brackets Date: Wed Feb 1 19:11:53 2017 +0100 Init: code cleanup + comments diff --git a/client b/client new file mode 100755 index 0000000..8918d9f Binary files /dev/null and b/client differ diff --git a/client.c b/client.c new file mode 100644 index 0000000..56106d7 --- /dev/null +++ b/client.c @@ -0,0 +1,117 @@ +#include "client.h" + + + +int main(int argc, char* argv[]){ + + /* [1] Initialisation + =========================================================*/ + /* (1) Socket information */ + // Server information + struct hostent *server_host; + // Server socket address + static struct sockaddr_in server_addr; + // Server socket address length + socklen_t sock_len; + // Local socket + int sock; + + /* (2) Misc. information */ + // Response + char *msg = "some message"; + // Reception buffer + char buffer[BUFSIZE]; + // Will contain the response + char *response; + // Will contain the length of received/sent data + int nb_bytes; + + + + /* [2] UPD socket creation + =========================================================*/ + /* (1) Socket initialization */ + sock = socket(AF_INET, SOCK_DGRAM, 0); + + /* (1-) Manage error */ + if( sock == -1 ){ + perror("erreur création socket"); + exit(1); + } + + /* (2) Get host information */ + server_host = gethostbyname("localhost"); + + /* (2-) Manage error */ + if( server_host == NULL ){ + perror("erreur adresse serveur"); + exit(1); + } + + /* (3) Create destination socket */ + bzero(&server_addr, sizeof(struct sockaddr_in)); // clean @server_addr struct before updating + + server_addr.sin_family = AF_INET; // set server addr family (ipv4) + server_addr.sin_port = htons(4000); // set server port + + memcpy( &server_addr.sin_addr.s_addr, // ) set host ipv4 addr + /* ( */ server_host->h_addr, // ) + /* ( */ server_host->h_length ); + + + + + /* [3] Send message + =========================================================*/ + // calculate server id struct size + sock_len = sizeof(struct sockaddr_in); + + /* (1) Send message to server (identified by ) */ + nb_bytes = sendto( sock, // ) socket + /* ( */ msg, // ) buffer + /* ( */ strlen(msg) + 1, // ) buffer size + /* ( */ 0, // ) flags + /* ( */ (struct sockaddr*) &server_addr, // ) server identifier + /* ( */ sock_len ); // identifier size + + + /* (2) Check if send succesfully */ + if( nb_bytes == -1 ){ + perror("erreur envoi message"); + exit(1); + } + + // log + printf("paquet envoyé, nb_bytes = %d\n",nb_bytes); + + + + /* [4] Wait for response + =========================================================*/ + nb_bytes = recvfrom( sock, // ) socket + /* ( */ buffer, // ) receive buffer + /* ( */ BUFSIZE, // ) buffer size + /* ( */ 0, // ) flags + /* ( */ (struct sockaddr*) &server_addr, // ) server identifier + /* ( */ &sock_len ); // identifier size + + /* (2) Check if received successfully (and consistently) */ + if( nb_bytes == -1 ){ + perror("erreur réponse serveur"); + exit(1); + } + + /* (3) Copy response into var */ + response = (char *) malloc(nb_bytes * sizeof(char)); + memcpy(response, buffer, nb_bytes); + + /* (4) log */ + printf("response recue du serveur : %s\n",response); + + + /* [5] Close socket + =========================================================*/ + close(sock); + + return 0; +} diff --git a/client.h b/client.h new file mode 100644 index 0000000..ca6902c --- /dev/null +++ b/client.h @@ -0,0 +1,18 @@ +#ifndef _CLIENT_H_ + + #define _CLIENT_H_ + + #include + #include + #include + #include + #include + #include + #include + #include + #include + + #define BUFSIZE 20 + + +#endif diff --git a/client.o b/client.o new file mode 100644 index 0000000..c7aec51 Binary files /dev/null and b/client.o differ diff --git a/lib.c b/lib.c new file mode 100644 index 0000000..e69de29 diff --git a/lib.h b/lib.h new file mode 100644 index 0000000..e69de29 diff --git a/lib.o b/lib.o new file mode 100644 index 0000000..1a7849e Binary files /dev/null and b/lib.o differ diff --git a/makefile b/makefile new file mode 100644 index 0000000..ec8e644 --- /dev/null +++ b/makefile @@ -0,0 +1,26 @@ +CC=-Wall -Werror + + +client.o: client.h client.c + gcc $(CC) -c -o client.o client.c + +server.o: server.h server.c + gcc $(CC) -c -o server.o server.c + +lib.o: lib.h lib.c + gcc $(CC) -c -o lib.o lib.c + +server: lib.o server.o + gcc $(CC) -o server lib.o server.o + +client: lib.o client.o + gcc $(CC) -o client lib.o client.o + + + + +all: client server + +clean: + rm -rf ./*.o + rm client server diff --git a/server.c b/server.c new file mode 100644 index 0000000..834a946 --- /dev/null +++ b/server.c @@ -0,0 +1,120 @@ +#include "server.h" + + + +int main(int argc, char* argv[]){ + + /* [1] Initialisation + =========================================================*/ + /* (1) Socket info */ + // Server (local) + static struct sockaddr_in addr_server; + // Client + static struct sockaddr_in addr_client; + // Client identifier + struct hostent *host_client; + // Socket size + socklen_t sock_len; + // Socket + int sock; + + + /* (2) Misc. information */ + // response + char *response = "bien recu"; + // reception buffer + char buffer[BUFSIZE]; + // received buffer + char *received; + // length of received/sent data + int buf_len; + + + + /* [2] Create socket + =========================================================*/ + /* (1) Create socket */ + sock = socket(AF_INET, SOCK_DGRAM, 0); // ipv4, udp, flags? + + /* (1-) Manage error */ + if( sock == -1 ){ + perror("erreur création socket"); + exit(1); + } + + + /* (2) Set socket information */ + bzero(&addr_server, sizeof(struct sockaddr_in)); + + addr_server.sin_family = AF_INET; // ipv4 + addr_server.sin_port = htons(4000); // port: 4000 + addr_server.sin_addr.s_addr = htonl(INADDR_ANY); // + + /* (3) Bind socket to local port */ + int bounded = bind(sock, // ) socket + /* ( */ (struct sockaddr*)&addr_server, // ) filled with address + /* ( */ sizeof(addr_server) ); // length of struct + + /* (3-) Manage error */ + if( bounded != -1 ){ + perror("erreur bind"); + exit(1); + } + + /* [3] Wait for client message + =========================================================*/ + // set socket length + sock_len = sizeof(struct sockaddr_in); + + /* (1) Wait for message */ + buf_len = recvfrom(sock, // ) socket + /* ( */ buffer, // ) buffer + /* ( */ BUFSIZE, // ) buffer length + /* ( */ 0, // ) flags + /* ( */ (struct sockaddr *)&addr_client, // ) client addr (filled) + /* ( */ &sock_len ); + + /* (1-) Manage error */ + if( buf_len == -1 ){ + perror("erreur réception paquet"); + exit(1); + } + + /* (2) Fetch client information */ + // récupère nom de la machine émettrice des données + host_client = gethostbyaddr( &(addr_client.sin_addr), // ) client addr (filled) + /* ( */ sizeof(long), // ) sizeof addr + /* ( */ AF_INET ); // ipv4 + + /* (2-) Manage error */ + if( host_client == NULL ){ + perror("erreur gethostbyaddr"); + exit(1); + } + + /* (3) Store message */ + received = (char *) malloc(buf_len * sizeof(char)); + memcpy(received, buffer, buf_len); + + printf( "recu message %s de %s:%d\n", received, host_client->h_name, ntohs(addr_client.sin_port) ); + + /* [4] Send response + =========================================================*/ + /* (1) Send response */ + buf_len = sendto(sock, // ) socket + /* ( */ response, // ) response + /* ( */ strlen(response)+1, // ) response length + /* ( */ 0, // ) flags + /* ( */ (struct sockaddr*) &addr_client, // ) client identifier + /* ( */ sock_len ); // identifier length + + /* (1-) Manage error */ + if( buf_len == -1 ){ + perror("erreur envoi réponse"); + exit(1); + } + + /* [5] Close socket + =========================================================*/ + close(sock); +} diff --git a/server.h b/server.h new file mode 100644 index 0000000..e766838 --- /dev/null +++ b/server.h @@ -0,0 +1,19 @@ +#ifndef _SERVER_H_ + + #define _SERVER_H_ + + + + #include + #include + #include + #include + #include + #include + #include + #include + #include + + + +#endif diff --git a/server.o b/server.o new file mode 100644 index 0000000..33457c9 Binary files /dev/null and b/server.o differ