views:

225

answers:

2

I want to ask, why I cannot transfer file from server to client? When I start to send the file from server, the client side program will have problem. So, I spend some times to check the code, But I still cannot find out the problem Can anyone point out the problem for me?

CLIENTFILE.C

#include stdio.h
#include stdlib.h
#include time.h
#include netinet/in.h
#include fcntl.h
#include sys/types.h
#include string.h
#include stdarg.h
#define PORT 5678
#define MLEN 1000
int main(int argc, char *argv [])
{

        int sockfd;
        int number,message;
        char outbuff[MLEN],inbuff[MLEN];
        //char PWD_buffer[_MAX_PATH];
        struct sockaddr_in servaddr;
        FILE *fp;
        int numbytes;  
        char buf[2048];



        if (argc != 2)
                fprintf(stderr, "error");

        if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
                fprintf(stderr, "socket error");

        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(PORT);

        if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
                fprintf(stderr, "connect error");

        if ( (fp = fopen("/home/na/nall9047/write.txt", "w")) == NULL){
                perror("fopen");
                exit(1);
        }
        printf("Still NO PROBLEM!\n");

        //Receive file from server
        while(1){
                numbytes = read(sockfd, buf, sizeof(buf));
                printf("read %d bytes, ", numbytes);

                if(numbytes == 0){
                        printf("\n");
                        break;
                }
                numbytes = fwrite(buf, sizeof(char), numbytes, fp);
                printf("fwrite %d bytes\n", numbytes);
        }

        fclose(fp);
        close(sockfd); 
        return 0;
}

SERVERFILE.C

#include stdio.h
#include fcntl.h
#include stdlib.h
#include time.h
#include string.h
#include netinet/in.h
#include errno.h
#include sys/types.h
#include sys/socket.h
#includ estdarg.h
#define PORT 5678
#define MLEN 1000
int main(int argc, char *argv [])
{

        int listenfd, connfd;
        int number, message, numbytes;
        int h, i, j, alen;
        int nread;
        struct sockaddr_in servaddr; 
        struct sockaddr_in cliaddr;
        FILE *in_file, *out_file, *fp;
        char buf[4096];



        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if (listenfd < 0)
                 fprintf(stderr,"listen error") ;

        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family      = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port        = htons(PORT);

        if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
                fprintf(stderr,"bind error") ;



        alen = sizeof(struct sockaddr);
        connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &alen);

        if (connfd < 0)
                fprintf(stderr,"error connecting") ;

        printf("accept one client from %s!\n", inet_ntoa(cliaddr.sin_addr));

        fp = fopen ("/home/na/nall9047/read.txt", "r"); // open file stored in server

        if (fp == NULL) {
                printf("\nfile NOT exist");
        }

        //Sending file
        while(!feof(fp)){

                numbytes = fread(buf, sizeof(char), sizeof(buf), fp);
                printf("fread %d bytes, ", numbytes);
                numbytes = write(connfd, buf, numbytes);
                printf("Sending %d bytes\n",numbytes);
        }

        fclose (fp);    
        close(listenfd);
        close(connfd);
        return 0;
}
A: 

Appart from anything else, fread is the name of a function and evaluates to a non-zero function pointer value. So:

 while(fread < 0){

can never be true.

anon
oh am sorry it was actually while(!feof(fp)){it was by mistake i edited it now...i guess the problem is near the while loop?
@cool feof() is wrong too - you should almost never use it. Instead, loop as long as fread returns non-zero.
anon
like how do i use fread in while loop ?can u pls gimme the synatx
@cool while( fread( .... ) != 0 ) {
anon
A: 

A couple of problems i see:

  • You're read()ing and write()ing a socket handle. Works on some OS's, but not others. You'll want to recv() and send() instead if you care at all about portability (or if your OS is one of the ones read/write breaks on).

  • You're close()ing a socket handle without shutdown()ing it first. That could well cause the last bytes sent to be discarded.

  • fread is a function, not a variable. You probably want your loop to look like

    while (!feof(fp)) {

UPDATE: Where are you setting the address to connect to?

cHao
thanx cHao actually it is while(!feof(fp)){but i mistakenly put it as while(fread < 0) i now changed it in the code.. i feel there is some mistake in this while loop ?
Aside from what's already been said, i can't tell you much without knowing what kind of problem you're having.
cHao
i feel like am not able to read the file from server side like, may be there should problem with while loop in serverfile.c file this my view i dont know if i'm right?
so i thought i would changewhile(!feof(fp)){ towhile( fread(fp,numbytes, ) != 0 ) {but my question is what should i use in FILE *stream in the above changed while loop ?
You could try changing the while to read like... while (numbytes = fread(....)) { .
cHao
so what could be parameters of fread ? that is where i am struck
fp is your FILE*. It should be the last arg to fread and/or fwrite.
cHao
You're already fread()ing. Take a look at your own code.
cHao
so it should be this right?while(fread(buf, sizeof(buf), 1, fp)
No, cause then the char you read will never get sent (you're not writing it to anywhere). Take what's currently the first line of your while loop, and make it your while condition instead.
cHao
thank you cHao will do that and compile it out