Working
This commit is contained in:
parent
402dcd0421
commit
cf4a6fbc8a
4
client.h
4
client.h
|
@ -11,8 +11,8 @@
|
|||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define BUFSIZE 20
|
||||
|
||||
|
||||
#include "lib.h"
|
||||
|
||||
#endif
|
||||
|
|
29
lib.h
29
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<int> Port to use (if 0: random)
|
||||
*
|
||||
* @return socket<socket> Socket descriptor or -1 if error
|
||||
*
|
||||
*/
|
||||
|
||||
int createUPDSocket(int port);
|
||||
|
||||
#endif
|
4
makefile
4
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
|
||||
|
|
18
server.c
18
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
|
||||
=========================================================*/
|
||||
|
|
5
server.h
5
server.h
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
@ -13,7 +14,9 @@
|
|||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
|
||||
#include "lib.h"
|
||||
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue