Init: code cleanup + comments

This commit is contained in:
xdrm-brackets 2017-02-01 19:11:53 +01:00
commit 402dcd0421
11 changed files with 300 additions and 0 deletions

BIN
client Executable file

Binary file not shown.

117
client.c Normal file
View File

@ -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 <sockaddr_in>) */
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;
}

18
client.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef _CLIENT_H_
#define _CLIENT_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#define BUFSIZE 20
#endif

BIN
client.o Normal file

Binary file not shown.

0
lib.c Normal file
View File

0
lib.h Normal file
View File

BIN
lib.o Normal file

Binary file not shown.

26
makefile Normal file
View File

@ -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

120
server.c Normal file
View File

@ -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);
}

19
server.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef _SERVER_H_
#define _SERVER_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#endif

BIN
server.o Normal file

Binary file not shown.