Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <ctype.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include "../errlib
- #define MAX_File_number_to_request 5
- #define RequestCmd_length 1000
- #define BUFLEN 128 /* BUFFER LENGTH */
- int main(int argc ,char * argv[])
- {
- //check arg number
- if(argc<=3)
- {
- fprintf(stderr,"missing parmeters. goy only %d . but expected at least 3",argc);
- }
- //get port number
- int port =atoi(argv[2]);//Convert string to integer
- //check if a vlaid ip address ofr not
- //struct hostent *Gethostbyname (const char *hostname);
- //gethostbyname() function returns a structure of type hostent for the given host name.
- //hostname--> is either a hostname or an IPv4 address in standard dot notation
- struct hostent *server;
- const char *hostname=argv[1];//???why index 1 . why not 0???
- server =gethostbyname(hostname);
- if(server==NULL)
- {
- fprintf(stderr,"gethostbyname() failed for : %s \n",hostname);
- exit(1);
- }
- //get ipaddress number
- //#include <arpa/inet.h>
- //int inet_aton(const char *cp, struct in_addr *addr);
- //inet_aton()-convert Internet dot address to network address
- struct in_addr serverIPaddr_struct; /* server IP addr. structure */
- int result = inet_aton(hostname, &serverIPaddr_struct);
- if (!result)
- {
- fprintf(stderr,"Invalid address.quitting \n");
- //err_quit("Invalid address");
- }
- //file names
- char files2Request[MAX_File_number_to_request];
- int n=argc-2;
- if(n>MAX_File_number_to_request)
- fprintf(stderr,"only 50 file can be asked at a time. U asked for %d . so 1st %d i will be asked",n,MAX_File_number_to_request);
- for (int i=0;i<n;i++)
- {
- files2Request[i]=argv[i+2];
- printf("%s\n",files2Request[i]);
- }
- //construct request command
- char cmds[RequestCmd_length][500];
- //char * strcpy ( char * destination, const char * source );
- for ( i=0;i<n;i++)
- {
- bzero(cmds[i],RequestCmd_length);
- strcpy(cmds[i],"GET");//ASCII characters “GET”
- strcat(cmds[i]," ");// ASCII space character
- strcat(cmds[i],files2Request[i]);// ASCII characters of the file name
- strcat(cmds[i],"\r");//ASCII carriage return (CR)
- strcat(cmds[i],"\n");//ASCII line feed (LF)
- fprintf(stderr,"CMD-%s : %s \n",cmd[i]);
- }
- /* create the socket */
- printf("Creating socket\n");
- int sockfd = Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- printf("done. Socket fd number: %d\n",sockfd);
- /* prepare address structure */
- struct sockaddr_in serv_addr;
- bzero(&serv_addr, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(port); //The htons() function converts the unsigned short integer hostshort from host byte order to network byte order.
- serv_addr.sin_addr = serverIPaddr_struct;
- /* connect */
- //showAddr("Connecting to target address", &serv_addr);
- printf("Connecting to target address\n");
- //Connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
- //printf("done.\n");
- if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0){
- error("Connection Failed");
- }
- else
- {
- printf("Client: Connecting...\n");
- }
- // a file descriptor (FD, less frequently fildes) is an abstract indicator (handle) used to access a file or other input/output resource, such as a pipe or network socket
- //The write() function shall attempt to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes
- Write(sockfd, cmds[0], strlen(buf));
- /* reads exactly "n" bytes from a descriptor */
- //ssize_t readn (int fd, void *vptr, size_t n)
- int indexCount;
- char receivedData[BUFLEN];
- //read 1st 3 character -->"+ O K"
- int character_read_count=readn(sockfd, receivedData, 3);///why 5 bit ??? why not 3 bit
- //+ O K CR LF B1 B2 B3 B4 T1 T2 T3 T4 File content
- //read fist 3 character "+ O K"
- char *t= "+OK";
- int g=strlen(t);
- if( strncmp(receivedData,t,g) == 0 )
- {
- //read next 2 character. CR LF . useless . but we need to read increase the ptr
- character_read_count=readn(sockfd, receivedData, 2);
- //read next 4 bit. B1 B2 B3 B4
- uint32_t file_bytes;
- int n = readn(sockfd, &file_bytes, 4);
- file_bytes = ntohl(file_bytes);
- printf("file_bytes: %d\n",file_bytes);
- //read next 4 bit. T1 T2 T3 T4
- uint32_t file_tm;
- n = readn(sockfd, &file_tm, 4);
- file_tm = ntohl(file_tm);
- printf("file_tm: %d\n",file_tm);
- //Writes an array of count elements, each one with a size of size bytes, from the block //of memory pointed by ptr to the current position in the stream.
- //size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
- char *fileContent;
- fileContent=(char*)malloc(file_bytes* sizeof(char));
- n = readn(sockfd, fileContent, file_bytes);
- if(character_read_count != file_bytes)
- {
- printf("wrong number of byte received \n");
- }
- else
- {
- char *fname;
- fname=files2Request[0];
- strcat(fname,"_Recieved");
- FILE *fp;
- fp = fopen(fname, "w");
- if(NULL == fp)
- {
- printf("Error opening file");
- return 1;
- }
- else
- {
- fwrite(fileContent, sizeof(char), fileContent, fp);
- fclose(fp);
- }
- }
- }
- Write(sockfd, "QUIT/r/n", 6);
- close(sockfd);
- printf("===========================================================\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement