diff --git a/client b/client index 82beefd..3975bb0 100755 Binary files a/client and b/client differ diff --git a/client.c b/client.c index 56106d7..9ef5caf 100644 --- a/client.c +++ b/client.c @@ -7,20 +7,16 @@ 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"; + char *msg = "some message\0"; // Reception buffer - char buffer[BUFSIZE]; + char buffer[BUFSIZE] = {'\0'}; // Will contain the response char *response; // Will contain the length of received/sent data @@ -30,8 +26,7 @@ int main(int argc, char* argv[]){ /* [2] UPD socket creation =========================================================*/ - /* (1) Socket initialization */ - sock = socket(AF_INET, SOCK_DGRAM, 0); + sock = xconnect("localhost", 4000, (struct sockaddr*) &server_addr); /* (1-) Manage error */ if( sock == -1 ){ @@ -39,41 +34,11 @@ int main(int argc, char* argv[]){ 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 - + /* (1) Send message to server */ + nb_bytes = xsend(sock, msg, (struct sockaddr*) &server_addr); /* (2) Check if send succesfully */ if( nb_bytes == -1 ){ @@ -88,12 +53,8 @@ int main(int argc, char* argv[]){ /* [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 + /* (1) Wait for response */ + nb_bytes = xlisten(sock, (struct sockaddr*) &server_addr, buffer, BUFSIZE); /* (2) Check if received successfully (and consistently) */ if( nb_bytes == -1 ){ @@ -102,7 +63,7 @@ int main(int argc, char* argv[]){ } /* (3) Copy response into var */ - response = (char *) malloc(nb_bytes * sizeof(char)); + response = (char*) malloc(nb_bytes * sizeof(char)); memcpy(response, buffer, nb_bytes); /* (4) log */ diff --git a/client.o b/client.o index 8cb6312..d7c42ba 100644 Binary files a/client.o and b/client.o differ diff --git a/lib.c b/lib.c index f45d077..f2a8f62 100644 --- a/lib.c +++ b/lib.c @@ -1,6 +1,206 @@ #include "lib.h" +int debug(const char* tag, const char* msg){ + + /* (1) Get number of characters to add */ + int diff = DEBUG_LEN - strlen(msg) - strlen(tag) - 3; + + /* (2) print msg */ + printf("[%s] %s", tag, msg); + + /* (3) print missing characters */ + for( ; diff >= 0 ; diff-- ) + printf("."); + + return 1; +} -int createUPDSocket(int port){ return 1; } + + + + +int xbind(const int port){ + + /* [0] Initialization + =========================================================*/ + int xsocket, bound; + static struct sockaddr_in addr; + + + /* [1] Create xsocket + =========================================================*/ + /* (1) Create UPD xsocket */ + xsocket = socket(AF_INET, SOCK_DGRAM, 0); + DEBUG&& debug("xbind", "creating server socket"); + + /* (r2-) Manage error */ + if( xsocket == -1 ){ + DEBUG&& printf("error\n"); + return -1; + } + + DEBUG&& printf("done\n"); + + /* [2] Set xsocket address information + =========================================================*/ + /* (1) Reset values */ + bzero(&addr, sizeof(struct sockaddr_in)); + + /* (2) Set xsocket addr */ + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + + + + /* [3] Bind to port + =========================================================*/ + /* (1) Bind and return -1 if error */ + bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr)); + DEBUG&& debug("xbind", "binding socket to port"); + + /* (2) Manage error */ + if( bound == -1 ){ + DEBUG&& printf("error\n"); + return -1; + } + + DEBUG&& printf("done\n"); + + + /* [4] Return xsocket + =========================================================*/ + return xsocket; +} + + + + + + +int xconnect(const char* hostname, const int port, struct sockaddr* serv){ + + /* [0] Initialization + =========================================================*/ + int xsocket, bound; + struct hostent *host; // data found by lookup + static struct sockaddr_in* serv_in; + serv_in = (void*) serv; + + + /* [1] Create xsocket + =========================================================*/ + /* (1) Create UPD xsocket */ + xsocket = socket(AF_INET, SOCK_DGRAM, 0); + DEBUG&& debug("xconnect", "creating client socket"); + + /* (r2-) Manage error */ + if( xsocket == -1 ){ + DEBUG&& printf("error\n"); + return -1; + } + + DEBUG&& printf("done\n"); + + + /* [2] Get information by hostname + =========================================================*/ + /* (1) Process */ + host = gethostbyname(hostname); + DEBUG&& debug("xconnect", "fetch info by hostname"); + + /* (2) Manage error */ + if( host == NULL ){ + DEBUG&& printf("error\n"); + return -1; + } + + DEBUG&& printf("found\n"); + + + + /* [3] Set server useful information from fetched info + =========================================================*/ + /* (1) Reset values */ + bzero(serv, sizeof(struct sockaddr_in)); + DEBUG&& debug("xconnect", "building server info"); + + /* (2) Set server info (ipv4, port) */ + serv_in->sin_family = AF_INET; + serv_in->sin_port = htons(port); + + /* (3) Copy server address */ + memcpy( &serv_in->sin_addr.s_addr, host->h_addr, host->h_length); + + DEBUG&& printf("built\n"); + + + // return socket + return xsocket; +} + + + + + + + + +int xlisten(const int xsocket, const struct sockaddr *client, char* buffer, int bufsize){ + + /* [1] Wait for message through xsocket + =========================================================*/ + /* (1) Useful data + memory allocation */ + socklen_t sock_len = sizeof(struct sockaddr_in); + buffer = (char*) malloc( sizeof(char) * bufsize ); + + /* (2) Listen */ + DEBUG&& debug("xlisten", "waiting for data"); + int read = recvfrom(xsocket, buffer, bufsize, 0, (struct sockaddr *) client, &sock_len); + + /* (3) Manage error */ + if( read == -1 ){ + DEBUG&& printf("error\n"); + return -1; + } + + DEBUG&& printf("received\n"); + + + + /* [2] Return number of read characters + =========================================================*/ + return read; +} + + + + + + +int xsend(const int xsocket, char* buffer, struct sockaddr* target){ + + /* [1] Send buffer (message) to target + =========================================================*/ + /* (1) Useful data */ + socklen_t addr_len = sizeof(struct sockaddr_in); + + /* (2) Send data */ + DEBUG&& debug("xsend", "sending data"); + int sent = sendto(xsocket, buffer, strlen(buffer), 0, target, addr_len); + + /* (3) Manage error */ + if( sent == -1 ){ + DEBUG&& printf("error\n"); + return -1; + } + + DEBUG&& printf("sent\n"); + + + /* [2] Return number of sent characters + =========================================================*/ + return sent; +} diff --git a/lib.h b/lib.h index f3fbe9c..2265e30 100644 --- a/lib.h +++ b/lib.h @@ -7,23 +7,31 @@ // debug flags - # define DEBUG 1 + #define DEBUG 0x1 + #define DEBUG_LEN 40 + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + /************************************************ **** Signatures **** ************************************************/ - /* CREATES A UDP SOCKET - * - * @port Port to use (if 0: random) - * - * @return socket Socket descriptor or -1 if error - * - */ - - int createUPDSocket(int port); + int debug(const char* tag, const char* msg); + int xbind(const int port); + int xconnect(const char* hostname, const int port, struct sockaddr* serv); + int xlisten(const int xsocket, const struct sockaddr *client, char* buffer, int bufsize); + int xsend(const int xsocket, char* buffer, struct sockaddr* target); #endif diff --git a/lib.o b/lib.o index 3d9b2d3..78eba4d 100644 Binary files a/lib.o and b/lib.o differ diff --git a/server b/server index c7ca090..8bcf89c 100755 Binary files a/server and b/server differ diff --git a/server.c b/server.c index 0dd56dc..c4f0c3f 100644 --- a/server.c +++ b/server.c @@ -7,14 +7,10 @@ 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 = NULL; - // Socket size - socklen_t sock_len; char client_ip[20]; // Socket int sock; @@ -35,7 +31,7 @@ int main(int argc, char* argv[]){ /* [2] Create socket =========================================================*/ /* (1) Create socket */ - sock = socket(AF_INET, SOCK_DGRAM, 0); // ipv4, udp, flags? + sock = xbind(4000); /* (1-) Manage error */ if( sock == -1 ){ @@ -43,37 +39,11 @@ int main(int argc, char* argv[]){ 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); + buf_len = xlisten(sock, (struct sockaddr*) &addr_client, buffer, BUFSIZE); - /* (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 ){ @@ -105,12 +75,7 @@ int main(int argc, char* argv[]){ /* [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 + xsend(sock, response, (struct sockaddr*)&addr_client); /* (1-) Manage error */ if( buf_len == -1 ){ diff --git a/server.o b/server.o index bf062e4..dca7da1 100644 Binary files a/server.o and b/server.o differ