CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2000
    Posts
    60

    Remotely copying a file

    I've got a portion of code in a program that if selected, the server sends a client a copy of a specified file. It works, but whenever the sever is finished sending the client all the data in the file, the server stops sending, but the client continuously loops waiting for the end of the file. I'm having trouble determining eof for the client. The code I have is:

    Server Code:
    if(stricmp(msg, "COPY")==0)
    {
    FILE *fileIn;
    char list[61];
    char file[200];
    int numread;

    recv(client, file, sizeof(file), 0);

    if((fileIn = fopen(file, "r+b")) != NULL )
    {
    /* Attempt to read in 60 characters */
    while(!feof(fileIn))
    {
    numread = fread(list, sizeof(char), 60, fileIn);
    send(client, list, sizeof(list), 0);
    }
    fclose(fileIn);
    }
    else
    {
    strcpy(msg, "problem opening the file");
    send(client, msg, sizeof(msg), 0);
    }
    }
    else

    And the Client Code:
    if(stricmp(command, "COPY")==0)
    {
    FILE *outFile;
    char file[500];
    char list[61];
    int numread = 0;

    send(client, command, sizeof(command), 0);

    cout << "File -> ";
    cin.getline(file, 500, '\n');
    send(client, file, sizeof(file), 0);

    numread = recv(client, list, sizeof(list), 0);

    if(stricmp(list, "PROBLEM OPENING THE FILE")==0)
    {
    cout << list << endl;
    }
    else
    {
    outFile = fopen("C:\\SpikesCopy.txt", "w+b");
    while(list[numread] != NULL)
    {
    fwrite(list, sizeof(char), numread, outFile);
    numread = recv(client, list, sizeof(list), 0);
    }
    fwrite(list, sizeof(char), numread, outFile);
    fclose(outFile);
    cout << "Stopped receiving." << endl;
    }
    }
    else

    For now, the client loops to recv until it hit's NULL. I thought NULL was the end of file, but this code does not work. Any help would be greatly appreciated. I've been stuck on this problem for a while.
    Last edited by orbitalne; January 18th, 2003 at 12:19 PM.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Usually, the #define for end of file is simply EOF Try EOF
    instead of NULL and see if that helps you.

    --Paul

  3. #3
    Join Date
    Mar 2000
    Posts
    60

    determining EOF

    EOF didn't work either. I don't understand. I tried copying a 98kb file and the client quit receiving after 2.8kbs, but the server was still trying to send.

  4. #4
    Join Date
    Dec 2002
    Posts
    287
    1)Replacing NULL with EOF is not going to make a difference.
    EOF is not really part of the file.

    2)Another problem that you have is that you read numread bytes and then you access list[numread]. The list is a char array which is indexed starting from 0 !. So when you read numread bytes, the only indexes that you are supposed to access are 0,1, ..., numread - 1. numread is a little bit too big

    numread = recv(client, list, sizeof(list), 0);
    ...
    while(list[numread] != NULL)

    3)I see you are opening the files in binary mode so you cannot make the assumption the files won't contain a char value = NULL.
    If this assumption is not true, then you can send a NULL character after sending the last line from the file. Otherwise, you either rely on the server closing the connection after sending the file (which is not really that elegant - don't forget to use the SO_LINGER socket option in this case) and the client detecting the gracefull shutdown, or you modify the file transfer protocol to send the file size before sending the entire file (of course, don't forget about network/host byte ordering issues).
    I would suggest/prefer this option.
    If you need some more help/implementation details, you can also take a look at the FTP protocol.

    Dan

  5. #5
    Join Date
    Mar 2000
    Posts
    60

    copying file

    ok... thanks! yeah, EOF wasn't working that great either. and I found out that numread = recv() wasn't what I wanted either

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured