Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

simplier message sending

  • Loading branch information...
commit c73c9288fa4f9dc39a3cd32c65ef948f1d6f70c7 1 parent 07d45d6
@grobie authored
Showing with 16 additions and 8 deletions.
  1. +16 −8 duoit.c
View
24 duoit.c
@@ -92,7 +92,6 @@ void handle_new_connection(int *listener, fd_set *master, int *fdmax) {
struct sockaddr_storage remoteaddr;
int newfd; // newly accept()ed socket descriptor
char remoteIP[INET6_ADDRSTRLEN];
- char *response = "220 Welcome to duoit FTP-Server. Service ready\r\n";
addrlen = sizeof remoteaddr;
newfd = accept(*listener, (struct sockaddr *)&remoteaddr, &addrlen);
@@ -105,7 +104,7 @@ void handle_new_connection(int *listener, fd_set *master, int *fdmax) {
*fdmax = newfd;
}
- send(newfd, response, strlen(response), 0);
+ send_message(newfd, "220 Welcome to duoit FTP-Server. Service ready");
inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN);
printf("new connection from %s on socket %d\n", remoteIP, newfd);
}
@@ -139,12 +138,24 @@ int format_command(char *command, int size) {
}
/*
+ * send messages to socket
+ *
+ * TODO: prüfen was gesendet wurd, send_all
+ */
+int send_message(int socket, char *message) {
+ char reply[strlen(message) + 2];
+
+ strcpy(reply, message);
+ strcat(reply, "\r\n");
+ send(socket, reply, strlen(reply), 0);
+}
+
+/*
* dispatch message
*
* TODO
*/
int dispatch(int socket, char *received, int received_len) {
- char *message;
char *parameter;
char command[5];
int input_length;
@@ -186,8 +197,7 @@ int dispatch(int socket, char *received, int received_len) {
printf("command: %s\n", command);
- message = "502 Command not implemented.\r\n";
- send(socket, message, strlen(message), 0);
+ send_message(socket, "502 Command not implemented.");
return 0;
}
@@ -206,7 +216,6 @@ int main(int argc, char *argv[]) {
int listener; // listening socket descriptor
int i, nbytes;
char buf[256];
- char *message;
// initialize server
listener = initialize_server();
@@ -246,8 +255,7 @@ int main(int argc, char *argv[]) {
close_connection(i, &master);
} else {
if (dispatch(i, buf, nbytes)) {
- message = "500 Syntax error\r\n";
- send(i , message, strlen(message), 0);
+ send_message(i, "500 Syntax Error");
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.