From c9d154d357ff1b149af17de54c3a66759ffc433b Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 18 Feb 2009 02:30:53 -0600 Subject: [PATCH] making send_file available to both client and server --- cftp.c | 26 ++++++++++++++++++++++++++ cftp.h | 1 + 2 files changed, 27 insertions(+) diff --git a/cftp.c b/cftp.c index dac0081..65ccde4 100644 --- a/cftp.c +++ b/cftp.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include "cftp.h" #include "server.h" @@ -92,6 +94,30 @@ void debug(const char *prefix, const char *msg) printf("%s: %s\n", prefix, msg); } +/* + * Sends a message to the socket. Pass in the socket and message. Expects msg + * to be already validated by validate_command first! Formats response into + * string buffer given. Works for client or server, so long as you expect a + * response message from the other side. + * + */ +void send_message(int skt, char *msg) +{ + char errmsg[ERRMSGLEN]; + memset(errmsg, '\0', ERRMSGLEN); + + if(send(skt, msg, strlen(msg), 0) < 0) + { + sprintf(errmsg, "Send Error: %s", strerror(errno)); + error(errmsg); + } + else if(recv(skt, msg, MSGLEN, 0) < 0) + { + sprintf(errmsg, "Receive Error: %s", strerror(errno)); + error(errmsg); + } +} + /* * Prints out the usage of the program to stderr. * diff --git a/cftp.h b/cftp.h index f3283ec..485925e 100644 --- a/cftp.h +++ b/cftp.h @@ -13,6 +13,7 @@ int validate_command(const char *); void split_command(char *, char *, char *); void error(const char *); void debug(const char *, const char *); +void send_message(int, char *); #endif