This commit is contained in:
xdrm-brackets 2017-02-02 11:18:34 +01:00
parent 402dcd0421
commit cf4a6fbc8a
12 changed files with 55 additions and 12 deletions

BIN
client

Binary file not shown.

View File

@ -12,7 +12,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <errno.h> #include <errno.h>
#define BUFSIZE 20
#include "lib.h"
#endif #endif

BIN
client.o

Binary file not shown.

6
lib.c
View File

@ -0,0 +1,6 @@
#include "lib.h"
int createUPDSocket(int port){ return 1; }

29
lib.h
View File

@ -0,0 +1,29 @@
#ifndef _LIB_H_
#define _LIB_H_
#define BUFSIZE 20
// debug flags
# define DEBUG 1
/************************************************
**** Signatures ****
************************************************/
/* CREATES A UDP SOCKET
*
* @port<int> Port to use (if 0: random)
*
* @return socket<socket> Socket descriptor or -1 if error
*
*/
int createUPDSocket(int port);
#endif

BIN
lib.o

Binary file not shown.

View File

@ -1,10 +1,10 @@
CC=-Wall -Werror CC=-Wall -Werror -g
client.o: client.h client.c client.o: client.h client.c
gcc $(CC) -c -o client.o 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 gcc $(CC) -c -o server.o server.c
lib.o: lib.h lib.c lib.o: lib.h lib.c

BIN
server Executable file

Binary file not shown.

View File

@ -12,9 +12,10 @@ int main(int argc, char* argv[]){
// Client // Client
static struct sockaddr_in addr_client; static struct sockaddr_in addr_client;
// Client identifier // Client identifier
struct hostent *host_client; struct hostent *host_client = NULL;
// Socket size // Socket size
socklen_t sock_len; socklen_t sock_len;
char client_ip[20];
// Socket // Socket
int sock; int sock;
@ -56,7 +57,7 @@ int main(int argc, char* argv[]){
/* ( */ sizeof(addr_server) ); // length of struct /* ( */ sizeof(addr_server) ); // length of struct
/* (3-) Manage error */ /* (3-) Manage error */
if( bounded != -1 ){ if( bounded == -1 ){
perror("erreur bind"); perror("erreur bind");
exit(1); exit(1);
} }
@ -80,10 +81,13 @@ int main(int argc, char* argv[]){
exit(1); exit(1);
} }
// Get client ip
inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20);
/* (2) Fetch client information */ /* (2) Fetch client information */
// récupère nom de la machine émettrice des données // récupère nom de la machine émettrice des données
host_client = gethostbyaddr( &(addr_client.sin_addr), // ) client addr (filled) host_client = gethostbyaddr( &addr_client.sin_addr, // ) client addr (filled)
/* ( */ sizeof(long), // ) sizeof addr /* ( */ sizeof(struct in_addr), // ) sizeof addr
/* ( */ AF_INET ); // ipv4 /* ( */ AF_INET ); // ipv4
/* (2-) Manage error */ /* (2-) Manage error */
@ -96,7 +100,7 @@ int main(int argc, char* argv[]){
received = (char *) malloc(buf_len * sizeof(char)); received = (char *) malloc(buf_len * sizeof(char));
memcpy(received, buffer, buf_len); 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 /* [4] Send response
=========================================================*/ =========================================================*/

View File

@ -6,6 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <arpa/inet.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
@ -15,5 +16,7 @@
#include <errno.h> #include <errno.h>
#include "lib.h"
#endif #endif

BIN
server.o

Binary file not shown.

1
test1 Normal file
View File

@ -0,0 +1 @@
<EFBFBD>