Home › Forums › C Programming › Concept utilising file transfer from client to server
- This topic has 1 reply, 2 voices, and was last updated 15 years, 2 months ago by MonicaLoche.
Viewing 1 reply thread
- AuthorPosts
- July 31, 2007 at 3:14 am #1993paritaParticipant
hey,
i have designed a code linking the server and the client. my purpose is to transfer a file from server as desired by the client. the code for client side is as under:-12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061<br />#include <stdio.h><br />#include <sys/types.h><br />#include <sys/socket.h><br />#include <netinet/in.h><br />#include <netdb.h><br />#include <string.h><br />#include <stdlib.h><br />#include <fstream.h><br />#include <iostream.h><br />int main(int argc, char *argv[])<br />{<br />int sockfd, portno,n;<br />struct sockaddr_in serv_addr;<br />struct hostent *server;<br />char buffer[256];<br />if (argc < 3) {<br />fprintf(stderr,"usage %s hostname portn", argv[0]);<br />exit(0);<br />}<br />portno = atoi(argv[2]);<br />sockfd = socket(AF_INET, SOCK_STREAM, 0);<br />if (sockfd < 0)<br />("ERROR opening socket");<br />server = gethostbyname(argv[1]);<br />if (server == NULL) {<br />fprintf(stderr,"ERROR, no such hostn");<br />exit(0);<br />}<br />bzero((char *) &serv_addr, sizeof(serv_addr));<br />serv_addr.sin_family = AF_INET;<br />bcopy((char *)server->h_addr,<br />(char *)&serv_addr.sin_addr.s_addr,<br />server->h_length);<br />serv_addr.sin_port = htons(portno);<br />if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)<br />("ERROR connecting");<br />n = recv(sockfd,buffer,255,0);<br />printf("the list of files:%sn",buffer);<br />printf("Please enter the file no:");<br />bzero(buffer,256);<br />fgets(buffer,255,stdin);<br />n = send(sockfd,buffer,strlen(buffer),0);<br />if (n < 0)<br />("ERROR writing to socket");<br />bzero(buffer,256);<br />n = recv(sockfd,buffer,255,0);<br />if (n < 0)<br />("ERROR reading from socket");<br />printf("%sn",buffer);<br />fstream fout;<br />fout.open("d.txt",ios::out);<br />n=recv(sockfd,buffer,255,0);<br />printf("content of buffer:%sn",buffer);<br />if (n < 0)<br />printf("ERROR reading from socket");<br />char write(sockfd,buffer, sizeof(buffer));<br />fout.close();<br />return 0;<br />}<br />the code for server side is as under:-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100/* A simple server in the internet domain using TCP<br />The port number is passed as an argument */<br />#include <iostream.h><br />#include <stdio.h><br />#include <sys/types.h><br />#include <sys/socket.h><br />#include <netinet/in.h><br />#include <stdlib.h><br />#include <string.h><br />#include <fstream.h><br />int main(int argc, char *argv[])<br />{<br />const int N=80;<br />char line [N]; <br />int sockfd, newsockfd, portno, clilen;<br />char *ch;<br />struct sockaddr_in serv_addr, cli_addr;<br />char buffer[256];<br />int n;<br />if (argc < 2) {<br />fprintf(stderr,"ERROR, no port providedn");<br />exit(1);<br />}<br />sockfd = socket(AF_INET, SOCK_STREAM, 0);<br />if (sockfd < 0)<br />("ERROR opening socket");<br />bzero((char *) &serv_addr, sizeof(serv_addr));<br />portno = atoi(argv[1]);<br />serv_addr.sin_family = AF_INET;<br />serv_addr.sin_addr.s_addr = INADDR_ANY;<br />serv_addr.sin_port = htons(portno);<br />if (bind(sockfd, (struct sockaddr *) &serv_addr,<br />sizeof(serv_addr)) < 0)<br />("ERROR on binding");<br />listen(sockfd,5);<br />clilen = sizeof(cli_addr);<br />newsockfd = accept(sockfd,<br />(struct sockaddr *) &cli_addr,(socklen_t *)&clilen);<br />if (newsockfd < 0)<br />("ERROR on accept");<br />n=send(newsockfd,"here is the list of files:1 for ankur.txt,2 for a.txt,3 for b.txt",255,0); <br />bzero(buffer,256);<br />n = recv(newsockfd,buffer,255,0);<br />if (n < 0) ("ERROR reading from socket");<br />printf("Here is the message: %cn",buffer[0]);<br />//ch=buffer;<br />switch (buffer[0])<br />{<br />case '1': <br />{ n=send(newsockfd,"file exists",18,0);<br />ifstream fin;<br />fin.open("ankur.txt");<br />while(fin)<br />{<br />fin.getline(line,N);<br />printf("Read line is :%sn",line);<br />n=send(newsockfd,line,sizeof(line),0);<br />}fin.close();<br />if (n < 0)<br />{<br />printf("ERROR writing to socket");<br />return 0;<br />}<br />}<br />break;<br /><br />case '2': { n=send(newsockfd,"file exists",18,0);<br /><br />ifstream fin;<br />fin.open("a.txt");<br />while(fin)<br />{<br />fin.getline(line,N);<br />printf("Read line is :%sn",line);<br />n=send(newsockfd,line,sizeof(line),0);<br /><br />}fin.close();<br />if (n < 0) ("ERROR writing to socket");<br />return 0;<br />}break;<br />case '3':{ n=send(newsockfd,"file exists",18,0);<br /><br />ifstream fin;<br />fin.open("b.txt");<br />while(fin)<br />{<br />fin.getline(line,N);<br />printf("Read line is :%sn",line);<br />n=send(newsockfd,line,sizeof(line),0);<br /><br />}fin.close();<br />if (n < 0) ("ERROR writing to socket");<br />return 0;<br />}break;<br />default:<br />{ n = send(newsockfd,"file doesnot exists",18,0);<br />}break;<br /><br />}<br />}But, i am not able to accomplish the task.
Please help me with ur assistance. - September 15, 2009 at 2:55 am #3247MonicaLocheParticipant
I guess there is ERROR writing to socket because you might need some socket initialization.
__________________
Writing jobs
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.