/******************** * * Homework 2 ******************** * Client FTP program ****************************************************************** * NOTE: Starting homework #2, add more comments here describing *the overall function performed by server ftp program * This includes, the list of ftp commands processed by server ftp. ****************************************************************** */ #include #include #include #include #include #include #include #define SERVER_FTP_PORT 5472 /* Error and OK codes */ #define OK 0 #define ER_INVALID_HOST_NAME -1 #define ER_CREATE_SOCKET_FAILED -2 #define ER_BIND_FAILED -3 #define ER_CONNECT_FAILED -4 #define ER_SEND_FAILED -5 #define ER_RECEIVE_FAILED -6 /* Function prototypes */ int clntConnect(char *serverName, int *s); int sendMessage (int s, char *msg, int msgSize); int receiveMessage(int s, char *buffer, int bufferSize, int *msgSize); /* List of all global variables */ char userCmd[1024]; /* user typed ftp command line read from keyboard */ char userCmdCopy[1024]; char *cmd; /* ftp command extracted from userCmd */ char *argument; /* argument extracted from userCmd */ char replyMsg[1024]; /* buffer to receive reply message from server */ //char *space = " "; /* space between the arguments*/ FILE *myFile; /* * main * * Function connects to the ftp server using clntConnect function. * Reads one ftp command in one line from the keyboard into userCmd array. * Sends the user command to the server. * Receive reply message from the server. * On receiving reply to QUIT ftp command from the server, * close the control connection socket and exit from main * * Parameters * argc - Count of number of arguments passed to main (input) * argv - Array of pointer to input parameters to main (input) * It is not required to pass any parameter to main * Can use it if needed. * * Return status * OK - Successful execution until QUIT command from client * N - Failed status, value of N depends on the function called or cmd processed */ int main( int argc, char *argv[] ) { /* List of local varibale */ int ccSocket; /* Control connection socket - to be used in all client communication */ int msgSize; /* size of the reply message received from the server */ int status = OK; /* * NOTE: without \n at the end of format string in printf, * UNIX will buffer (not flush) * output to display and you will not see it on monitor. */ printf("Started execution of client ftp.\n"); /* Connect to client ftp*/ printf("Calling clntConnect to connect to the server.\n"); /* changed text */ status=clntConnect("192.168.200.230 ", &ccSocket); /* Off-campus IP: "143.241.37.230" */ if(status != 0) { printf("Connection to server failed, exiting main. \n"); return (status); } /* * Read an ftp command with argument, if any, in one line from user into userCmd. * Copy ftp command part into ftpCmd and the argument into arg array. * Send the line read (both ftp cmd part and the argument part) in userCmd to server. * Receive reply message from the server. * until quit command is typed by the user. */ /*********************************************************************************/ do { printf("my ftp> "); //strcpy(userCmd, "quit"); /* This statement must be replaced in homework #2 */ /* to read the command from the user. Use gets or readln function */ gets(userCmd); /* Get the user's input and copy to userCmd */ /* Separate command and argument from userCmd */ //strcpy(cmd, userCmd); /* Modify in Homework 2. Use strtok function */ //strcpy(argument, ""); /* Modify in Homework 2. Use strtok function */ /* Separate command and argument from userCmd */ strcpy(userCmdCopy, userCmd); cmd = strtok(userCmdCopy, " "); argument = strtok(NULL, " "); printf("Split user command \"%s\" into tokens:\n",userCmd ); printf("Command:\t\"%s\"\n", cmd ); printf("Argument:\t\"%s\"\n", argument); /*********************************************************************/ /* send the userCmd to the server */ status = sendMessage(ccSocket, userCmd, strlen(userCmd)+1); if(status != OK) { break; } /* ****************************************************************** */ /* Receive reply message from the the server */ status = receiveMessage(ccSocket, replyMsg, sizeof(replyMsg), &msgSize); if(status != OK) { break; } } while (strcmp(cmd, "quit") != 0); printf("Closing control connection. \n"); close(ccSocket); /* close control connection socket */ printf("Exiting client main. \n"); return (status); } /* end main() */ /* * clntConnect * * Function to create a socket, bind local client IP address and port to the socket * and connect to the server * * Parameters * serverName - IP address of server in dot notation (input) * s - Control connection socket number (output) * * Return status * OK - Successfully connected to the server * ER_INVALID_HOST_NAME - Invalid server name * ER_CREATE_SOCKET_FAILED - Cannot create socket * ER_BIND_FAILED - bind failed * ER_CONNECT_FAILED - connect failed */ int clntConnect ( char *serverName, /* server IP address in dot notation (input) */ int *s /* control connection socket number (output) */ ) { int sock; /* local variable to keep socket number */ struct sockaddr_in clientAddress; /* local client IP address */ struct sockaddr_in serverAddress; /* server IP address */ struct hostent *serverIPstructure; /* host entry having server IP address in binary */ /* Get IP address os server in binary from server name (IP in dot natation) */ if((serverIPstructure = gethostbyname(serverName)) == NULL) { printf("%s is unknown server. \n", serverName); return (ER_INVALID_HOST_NAME); /* error return */ } /* Create control connection socket */ if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("cannot create socket "); return (ER_CREATE_SOCKET_FAILED); /* error return */ } /* initialize client address structure memory to zero */ memset((char *) &clientAddress, 0, sizeof(clientAddress)); /* Set local client IP address, and port in the address structure */ clientAddress.sin_family = AF_INET; /* Internet protocol family */ clientAddress.sin_addr.s_addr = htonl(INADDR_ANY); /* INADDR_ANY is 0, which means */ /* let the system fill client IP address */ clientAddress.sin_port = 0; /* With port set to 0, system will allocate a free port */ /* from 1024 to (64K -1) */ /* Associate the socket with local client IP address and port */ if(bind(sock,(struct sockaddr *)&clientAddress,sizeof(clientAddress))<0) { perror("cannot bind"); close(sock); return(ER_BIND_FAILED); /* bind failed */ } /* Initialize serverAddress memory to 0 */ memset((char *) &serverAddress, 0, sizeof(serverAddress)); /* Set ftp server ftp address in serverAddress */ serverAddress.sin_family = AF_INET; memcpy((char *) &serverAddress.sin_addr, serverIPstructure->h_addr, serverIPstructure->h_length); serverAddress.sin_port = htons(SERVER_FTP_PORT); /* Connect to the server */ if (connect(sock, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0) { perror("Cannot connect to server"); close (sock); /* close the control connection socket */ return(ER_CONNECT_FAILED); /* error return */ } /* Store listen socket number to be returned in output parameter 's' */ *s=sock; return(OK); /* successful return */ } // end of clntConnect() */ /* * sendMessage * * Function to send specified number of octet (bytes) to client ftp * * Parameters * s - Socket to be used to send msg to client (input) * msg - Pointer to character arrary containing msg to be sent (input) * msgSize - Number of bytes, including NULL, in the msg to be sent to client (input) * * Return status * OK - Msg successfully sent * ER_SEND_FAILED - Sending msg failed */ int sendMessage( int s, /* socket to be used to send msg to client */ char *msg, /*buffer having the message data */ int msgSize /*size of the message/data in bytes */ ) { int i; /* Print the message to be sent byte by byte as character */ for(i=0;i