Compare commits
No commits in common. "udp" and "master" have entirely different histories.
4
client.c
4
client.c
|
@ -65,7 +65,7 @@ int main(int argc, char* argv[]){
|
||||||
/* [4] Send message
|
/* [4] Send message
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Send message to server */
|
/* (1) Send message to server */
|
||||||
bytes = xwrite(sock, to_send, &server_addr);
|
bytes = xsend(sock, to_send, &server_addr);
|
||||||
|
|
||||||
/* (2) Check if send succesfully */
|
/* (2) Check if send succesfully */
|
||||||
if( bytes == -1 ){
|
if( bytes == -1 ){
|
||||||
|
@ -80,7 +80,7 @@ int main(int argc, char* argv[]){
|
||||||
/* [5] Wait for response
|
/* [5] Wait for response
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Wait for response */
|
/* (1) Wait for response */
|
||||||
bytes = xread(sock, &server_addr, to_recv, BUFSIZE);
|
bytes = xlisten(sock, &server_addr, to_recv, BUFSIZE);
|
||||||
|
|
||||||
/* (2) Check if received successfully (and consistently) */
|
/* (2) Check if received successfully (and consistently) */
|
||||||
if( bytes == -1 ){
|
if( bytes == -1 ){
|
||||||
|
|
18
lib.c
18
lib.c
|
@ -21,7 +21,7 @@ int debug(const char* tag, const char* msg){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int xlisten(const int port){
|
int xbind(const int port){
|
||||||
|
|
||||||
/* [0] Initialization
|
/* [0] Initialization
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -33,7 +33,7 @@ int xlisten(const int port){
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Create UPD xsocket */
|
/* (1) Create UPD xsocket */
|
||||||
xsocket = socket(AF_INET, SOCK_DGRAM, 0);
|
xsocket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
DEBUG&& debug("xlisten", "creating server socket");
|
DEBUG&& debug("xbind", "creating server socket");
|
||||||
|
|
||||||
/* (r2-) Manage error */
|
/* (r2-) Manage error */
|
||||||
if( xsocket == -1 ){
|
if( xsocket == -1 ){
|
||||||
|
@ -59,7 +59,7 @@ int xlisten(const int port){
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Bind and return -1 if error */
|
/* (1) Bind and return -1 if error */
|
||||||
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
|
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
|
||||||
DEBUG&& debug("xlisten", "binding socket to port");
|
DEBUG&& debug("xbind", "binding socket to port");
|
||||||
|
|
||||||
/* (2) Manage error */
|
/* (2) Manage error */
|
||||||
if( bound == -1 ){
|
if( bound == -1 ){
|
||||||
|
@ -146,7 +146,7 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int xread(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize){
|
int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize){
|
||||||
|
|
||||||
/* [1] Wait for message through xsocket
|
/* [1] Wait for message through xsocket
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -155,7 +155,7 @@ int xread(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsi
|
||||||
buffer = (char*) realloc(buffer, sizeof(char) * bufsize );
|
buffer = (char*) realloc(buffer, sizeof(char) * bufsize );
|
||||||
|
|
||||||
/* (2) Listen */
|
/* (2) Listen */
|
||||||
DEBUG&& debug("xread", "waiting for data");
|
DEBUG&& debug("xlisten", "waiting for data");
|
||||||
int read = recvfrom(xsocket, buffer, bufsize / sizeof(char) + sizeof(char), 0, (struct sockaddr*) client, &sock_len);
|
int read = recvfrom(xsocket, buffer, bufsize / sizeof(char) + sizeof(char), 0, (struct sockaddr*) client, &sock_len);
|
||||||
|
|
||||||
/* (3) Manage error */
|
/* (3) Manage error */
|
||||||
|
@ -166,7 +166,7 @@ int xread(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsi
|
||||||
|
|
||||||
DEBUG&& printf("received\n");
|
DEBUG&& printf("received\n");
|
||||||
|
|
||||||
printf("[xread:received] '%s'\n", buffer);
|
printf("[xlisten:received] '%s'\n", buffer);
|
||||||
|
|
||||||
|
|
||||||
/* [2] Return number of read characters
|
/* [2] Return number of read characters
|
||||||
|
@ -179,7 +179,7 @@ int xread(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int xwrite(const int xsocket, char* buffer, struct sockaddr_in* target){
|
int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){
|
||||||
|
|
||||||
/* [1] Send buffer (message) to target
|
/* [1] Send buffer (message) to target
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -187,7 +187,7 @@ int xwrite(const int xsocket, char* buffer, struct sockaddr_in* target){
|
||||||
size_t addr_len = sizeof(struct sockaddr_in);
|
size_t addr_len = sizeof(struct sockaddr_in);
|
||||||
|
|
||||||
/* (2) Send data */
|
/* (2) Send data */
|
||||||
DEBUG&& debug("xwrite", "sending data");
|
DEBUG&& debug("xsend", "sending data");
|
||||||
int sent = sendto(xsocket, buffer, strlen(buffer)+1, 0, (struct sockaddr*) target, addr_len);
|
int sent = sendto(xsocket, buffer, strlen(buffer)+1, 0, (struct sockaddr*) target, addr_len);
|
||||||
|
|
||||||
/* (3) Manage error */
|
/* (3) Manage error */
|
||||||
|
@ -199,7 +199,7 @@ int xwrite(const int xsocket, char* buffer, struct sockaddr_in* target){
|
||||||
DEBUG&& printf("sent\n");
|
DEBUG&& printf("sent\n");
|
||||||
|
|
||||||
|
|
||||||
printf("[xwrite:sent] '%s'\n", buffer);
|
printf("[xsend:sent] '%s'\n", buffer);
|
||||||
|
|
||||||
|
|
||||||
/* [2] Return number of sent characters
|
/* [2] Return number of sent characters
|
||||||
|
|
6
lib.h
6
lib.h
|
@ -30,8 +30,8 @@
|
||||||
|
|
||||||
int debug(const char* tag, const char* msg);
|
int debug(const char* tag, const char* msg);
|
||||||
|
|
||||||
int xlisten(const int port);
|
int xbind(const int port);
|
||||||
int xconnect(const char* hostname, const int port, struct sockaddr_in* serv);
|
int xconnect(const char* hostname, const int port, struct sockaddr_in* serv);
|
||||||
int xread(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize);
|
int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize);
|
||||||
int xwrite(const int xsocket, char* buffer, struct sockaddr_in* target);
|
int xsend(const int xsocket, char* buffer, struct sockaddr_in* target);
|
||||||
#endif
|
#endif
|
||||||
|
|
6
server.c
6
server.c
|
@ -54,7 +54,7 @@ int main(int argc, char* argv[]){
|
||||||
/* [3] Create socket
|
/* [3] Create socket
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Create socket */
|
/* (1) Create socket */
|
||||||
sock = xlisten(port);
|
sock = xbind(port);
|
||||||
|
|
||||||
/* (1-) Manage error */
|
/* (1-) Manage error */
|
||||||
if( sock == -1 ){
|
if( sock == -1 ){
|
||||||
|
@ -66,7 +66,7 @@ int main(int argc, char* argv[]){
|
||||||
/* [4] Wait for client message
|
/* [4] Wait for client message
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Listen to data */
|
/* (1) Listen to data */
|
||||||
bytes = xread(sock, &addr_client, to_recv, BUFSIZE);
|
bytes = xlisten(sock, &addr_client, to_recv, BUFSIZE);
|
||||||
|
|
||||||
|
|
||||||
/* (2) Manage error */
|
/* (2) Manage error */
|
||||||
|
@ -83,7 +83,7 @@ int main(int argc, char* argv[]){
|
||||||
/* [5] Send response
|
/* [5] Send response
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Send response */
|
/* (1) Send response */
|
||||||
bytes = xwrite(sock, to_send, &addr_client);
|
bytes = xsend(sock, to_send, &addr_client);
|
||||||
|
|
||||||
/* (2) Manage error */
|
/* (2) Manage error */
|
||||||
if( bytes == -1 ){
|
if( bytes == -1 ){
|
||||||
|
|
Loading…
Reference in New Issue