diff --git a/client b/client index 8918d9f..82beefd 100755 Binary files a/client and b/client differ diff --git a/client.h b/client.h index ca6902c..2262c75 100644 --- a/client.h +++ b/client.h @@ -11,8 +11,8 @@ #include #include #include - - #define BUFSIZE 20 + #include "lib.h" + #endif diff --git a/client.o b/client.o index c7aec51..8cb6312 100644 Binary files a/client.o and b/client.o differ diff --git a/lib.c b/lib.c index e69de29..f45d077 100644 --- a/lib.c +++ b/lib.c @@ -0,0 +1,6 @@ +#include "lib.h" + + + + +int createUPDSocket(int port){ return 1; } diff --git a/lib.h b/lib.h index e69de29..f3fbe9c 100644 --- a/lib.h +++ b/lib.h @@ -0,0 +1,29 @@ +#ifndef _LIB_H_ + + #define _LIB_H_ + + + #define BUFSIZE 20 + + + // debug flags + # define DEBUG 1 + + + + + /************************************************ + **** Signatures **** + ************************************************/ + + /* CREATES A UDP SOCKET + * + * @port Port to use (if 0: random) + * + * @return socket Socket descriptor or -1 if error + * + */ + + int createUPDSocket(int port); + +#endif diff --git a/lib.o b/lib.o index 1a7849e..3d9b2d3 100644 Binary files a/lib.o and b/lib.o differ diff --git a/makefile b/makefile index ec8e644..0a49cce 100644 --- a/makefile +++ b/makefile @@ -1,10 +1,10 @@ -CC=-Wall -Werror +CC=-Wall -Werror -g client.o: client.h client.c gcc $(CC) -c -o client.o client.c -server.o: server.h server.c +server.o: lib.h server.h server.c gcc $(CC) -c -o server.o server.c lib.o: lib.h lib.c diff --git a/server b/server new file mode 100755 index 0000000..c7ca090 Binary files /dev/null and b/server differ diff --git a/server.c b/server.c index 834a946..0dd56dc 100644 --- a/server.c +++ b/server.c @@ -12,9 +12,10 @@ int main(int argc, char* argv[]){ // Client static struct sockaddr_in addr_client; // Client identifier - struct hostent *host_client; + struct hostent *host_client = NULL; // Socket size socklen_t sock_len; + char client_ip[20]; // Socket int sock; @@ -51,12 +52,12 @@ int main(int argc, char* argv[]){ addr_server.sin_addr.s_addr = htonl(INADDR_ANY); // /* (3) Bind socket to local port */ - int bounded = bind(sock, // ) socket + int bounded = bind( sock, // ) socket /* ( */ (struct sockaddr*)&addr_server, // ) filled with address /* ( */ sizeof(addr_server) ); // length of struct /* (3-) Manage error */ - if( bounded != -1 ){ + if( bounded == -1 ){ perror("erreur bind"); exit(1); } @@ -80,11 +81,14 @@ int main(int argc, char* argv[]){ exit(1); } + // Get client ip + inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20); + /* (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 + host_client = gethostbyaddr( &addr_client.sin_addr, // ) client addr (filled) + /* ( */ sizeof(struct in_addr), // ) sizeof addr + /* ( */ AF_INET ); // ipv4 /* (2-) Manage error */ if( host_client == NULL ){ @@ -96,7 +100,7 @@ int main(int argc, char* argv[]){ 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) ); + printf( "recu message %s de %s:%d\n", received, client_ip, ntohs(addr_client.sin_port) ); /* [4] Send response =========================================================*/ diff --git a/server.h b/server.h index e766838..085b371 100644 --- a/server.h +++ b/server.h @@ -6,6 +6,7 @@ #include #include + #include #include #include #include @@ -13,7 +14,9 @@ #include #include #include - + + + #include "lib.h" #endif diff --git a/server.o b/server.o index 33457c9..bf062e4 100644 Binary files a/server.o and b/server.o differ diff --git a/test1 b/test1 new file mode 100644 index 0000000..13c6037 --- /dev/null +++ b/test1 @@ -0,0 +1 @@ +¸ \ No newline at end of file