Compare commits

..

1 Commits
master ... udp

Author SHA1 Message Date
xdrm-brackets d2972e4171 Renamed lib functions to maintain coherence with tcp branch 2017-02-08 11:08:24 +01:00
9 changed files with 17 additions and 17 deletions

BIN
client

Binary file not shown.

View File

@ -65,7 +65,7 @@ int main(int argc, char* argv[]){
/* [4] Send message
=========================================================*/
/* (1) Send message to server */
bytes = xsend(sock, to_send, &server_addr);
bytes = xwrite(sock, to_send, &server_addr);
/* (2) Check if send succesfully */
if( bytes == -1 ){
@ -80,7 +80,7 @@ int main(int argc, char* argv[]){
/* [5] Wait for response
=========================================================*/
/* (1) Wait for response */
bytes = xlisten(sock, &server_addr, to_recv, BUFSIZE);
bytes = xread(sock, &server_addr, to_recv, BUFSIZE);
/* (2) Check if received successfully (and consistently) */
if( bytes == -1 ){

BIN
client.o

Binary file not shown.

18
lib.c
View File

@ -21,7 +21,7 @@ int debug(const char* tag, const char* msg){
int xbind(const int port){
int xlisten(const int port){
/* [0] Initialization
=========================================================*/
@ -33,7 +33,7 @@ int xbind(const int port){
=========================================================*/
/* (1) Create UPD xsocket */
xsocket = socket(AF_INET, SOCK_DGRAM, 0);
DEBUG&& debug("xbind", "creating server socket");
DEBUG&& debug("xlisten", "creating server socket");
/* (r2-) Manage error */
if( xsocket == -1 ){
@ -59,7 +59,7 @@ int xbind(const int port){
=========================================================*/
/* (1) Bind and return -1 if error */
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
DEBUG&& debug("xbind", "binding socket to port");
DEBUG&& debug("xlisten", "binding socket to port");
/* (2) Manage error */
if( bound == -1 ){
@ -146,7 +146,7 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize){
int xread(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize){
/* [1] Wait for message through xsocket
=========================================================*/
@ -155,7 +155,7 @@ int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int buf
buffer = (char*) realloc(buffer, sizeof(char) * bufsize );
/* (2) Listen */
DEBUG&& debug("xlisten", "waiting for data");
DEBUG&& debug("xread", "waiting for data");
int read = recvfrom(xsocket, buffer, bufsize / sizeof(char) + sizeof(char), 0, (struct sockaddr*) client, &sock_len);
/* (3) Manage error */
@ -166,7 +166,7 @@ int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int buf
DEBUG&& printf("received\n");
printf("[xlisten:received] '%s'\n", buffer);
printf("[xread:received] '%s'\n", buffer);
/* [2] Return number of read characters
@ -179,7 +179,7 @@ int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int buf
int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){
int xwrite(const int xsocket, char* buffer, struct sockaddr_in* target){
/* [1] Send buffer (message) to target
=========================================================*/
@ -187,7 +187,7 @@ int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){
size_t addr_len = sizeof(struct sockaddr_in);
/* (2) Send data */
DEBUG&& debug("xsend", "sending data");
DEBUG&& debug("xwrite", "sending data");
int sent = sendto(xsocket, buffer, strlen(buffer)+1, 0, (struct sockaddr*) target, addr_len);
/* (3) Manage error */
@ -199,7 +199,7 @@ int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){
DEBUG&& printf("sent\n");
printf("[xsend:sent] '%s'\n", buffer);
printf("[xwrite:sent] '%s'\n", buffer);
/* [2] Return number of sent characters

6
lib.h
View File

@ -30,8 +30,8 @@
int debug(const char* tag, const char* msg);
int xbind(const int port);
int xlisten(const int port);
int xconnect(const char* hostname, const int port, struct sockaddr_in* serv);
int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize);
int xsend(const int xsocket, char* buffer, struct sockaddr_in* target);
int xread(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize);
int xwrite(const int xsocket, char* buffer, struct sockaddr_in* target);
#endif

BIN
lib.o

Binary file not shown.

BIN
server

Binary file not shown.

View File

@ -54,7 +54,7 @@ int main(int argc, char* argv[]){
/* [3] Create socket
=========================================================*/
/* (1) Create socket */
sock = xbind(port);
sock = xlisten(port);
/* (1-) Manage error */
if( sock == -1 ){
@ -66,7 +66,7 @@ int main(int argc, char* argv[]){
/* [4] Wait for client message
=========================================================*/
/* (1) Listen to data */
bytes = xlisten(sock, &addr_client, to_recv, BUFSIZE);
bytes = xread(sock, &addr_client, to_recv, BUFSIZE);
/* (2) Manage error */
@ -83,7 +83,7 @@ int main(int argc, char* argv[]){
/* [5] Send response
=========================================================*/
/* (1) Send response */
bytes = xsend(sock, to_send, &addr_client);
bytes = xwrite(sock, to_send, &addr_client);
/* (2) Manage error */
if( bytes == -1 ){

BIN
server.o

Binary file not shown.