Solved

receiving

Posted on 2011-03-18
80
456 Views
Last Modified: 2012-05-11
hi all,
After sending the first 2 files from client to server correctly, there is a mismatch between sending and receiving  at third file. I just send message header from client , but server receive both message header and file content. even though the file content will be sent after message header.

Please advise what cause this issue. Thanks a lot.

client
t after append its filesize is :/home/ubuntu/program/client.c 8674
File name and its size just sent!!!. No content here
just send file name and its size !!!!!
start sending file content , respective to filename
In 2048 block
i is 0. next block
......
=============================
server
Here is the message from client: /home/ubuntu/program/client.c 8674
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
.....

client.c
server.c
0
Comment
Question by:BeginToLearn
  • 42
  • 29
  • 9
80 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
ID: 35167738
Hi Begin,

The first things that I'd do is rework the client side SendFile function.

After the file is opened, the entire loop to transfer the file can be as simple as this:
 
  buffer= (char*)malloc(sizeof(char) *BUFFER_LEN);
  while ((result= fread (buffer, 1, BUFFER_SEND, fp) > 0)
  {                
    byte_sent= send(sock, buffer, result, 0);
  }
  free(buffer);

Everything else is debug or obfuscation.


Kent
0
 

Author Comment

by:BeginToLearn
ID: 35167817
huh? I need to send trunks of each file and multiple files in a directory and subdirectory. I don't send whole file at a time.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35167886
Hi Begin,

What do you mean "trunks of each file"?  


Lines 133 - 206 can all be replaced with that short loop.  You're doing an awful lot of processing that you don't need to do.

If you want to add verification and sanity checks you can, and it certainly makes sense to do that.  But having a chunk of code to send a 2K block, another to send a shorter last block, and another to send a file shorter than 2K is over-complication.



Kent
0
 

Author Comment

by:BeginToLearn
ID: 35167912
For example for it need to transfer a 4 GB file. It will send each 2048 byte portion at a time. That's why from line 133-206 chop the file portion.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35167968

Hi Begin,

I'm not sure what you're saying there.  Can you tell me a little more?

Once you've identified a file to transfer, most of the logic is done.  Actually sending the data can be as simple as reading from the file an writing the data to the socket.


Kent
0
 

Author Comment

by:BeginToLearn
ID: 35168028
here in my idea to transfer files in a directory and its subdirectory from client to server:
  + Read all file name  in directory and find total of files to be sent from client to server
  + send total file to server. this will help to determine how many iteration in for loop in receiving at server side
  + for each file, send filename and its size to server. file size help to determine how many iteration  to send portions in in for loop in order to send complete file. Similar to server side.  
         
 
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35168259
Hi Begin,

Ok.  Let's look at your 3 bullets in big-picture terms.

- The first bullet is what you're trying to accomplish.  That is, you want to copy an entire folder and all subfolders to another system.
- The second bullet tells the server how many items the client is going to send.
- The third bullet is how you intend to send each file.

The first bullet has nothing to do with the code.  That's your goal.

The second bullet and thirds bullets should be restructured.  The program logic would look something like this:

main () {
  send_all_files ();
}

send_all_files () {
  list_all_files ();
  send_file_count ();
  for (idx = 0; idx < filecount; idx++)
    send_file (file[idx]);
)

send_file () {
}


Your code doesn't need to be any more than about 1/2 of of what you have now.


Kent

 

 

The client side code will be easier to code and understand if you separate the application logic (what you're trying to do) from the system logic (how you're going to do it).

To make that happen, the sendFile function can be broken up into smaller pieces.



0
 

Author Comment

by:BeginToLearn
ID: 35168287
Let me restruct my code in following format now. tks.

    main () {
  send_all_files ();
}

send_all_files () {
  list_all_files ();
  send_file_count ();
  for (idx = 0; idx < filecount; idx++)
    send_file (file[idx]);
)

send_file () {
}

PS: just wonder whether you see what cause mismatch. hihi.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35168325
Hi Begin,

I looked briefly, but quickly saw that things were already too complicated for the relatively simple task that you're trying to do and didn't look any further.


Kent
0
 

Author Comment

by:BeginToLearn
ID: 35168363
HI Kdo,
 I see. I am restruct my function now. I will post back when I finish it. Tks. I am so desperated now :)
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35168586
Don't be desperate.  :)

We'll make this happen!
0
 

Author Comment

by:BeginToLearn
ID: 35168601
I just finished to reorganise the code.  So please advise  what error i have made. tks a lot.
client.c
server.c
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35168827
Hi Begin,

Going back to the send() function, it still reduces to the code below.


Kent

void sendfile( SOCKET sock, string filename)
{
        FILE *fp;
        fp= fopen(filename.c_str(),"r");// t is the filename string
        long BytesSent;
       
	if(fp == NULL)
	{ 
		printf("error to open %s",filename.c_str());
                exit(1);
  } 
	// determine file size 
        fseek(fp, 0, SEEK_END);
        int filesize = ftell(fp);
        rewind(fp);
      
        string t = filenamefilesize(filename, filesize);      
              
        char *pch = (char*)malloc(sizeof(char) *t.length() +1);

        memset(pch,'\0',sizeof(char)*t.length()+1);

        strcpy(pch, t.c_str());
        printf("File name and its size just sent!!!. No content here\n");

        send(sock, pch,t.size(),0);
        printf("just send file name and its size !!!!!\n");
              

    buffer= (char*)malloc(sizeof(char) *BUFFER_LEN);
  while ((result= fread (buffer, 1, BUFFER_SEND, fp) > 0)
  {                
    BytesSent + send (sock, buffer, result, 0);
  }
  free(buffer);             
  fclose(fp);
}
}

Open in new window

0
 

Author Comment

by:BeginToLearn
ID: 35168943
I am appreciated your motivation. You simplify sendfile() to very succinct level. I don't know why I never thought about it.  
0
 

Author Comment

by:BeginToLearn
ID: 35169062
First feedback is it's amazing. i am speechless. It send all files now. I gonna test with file in subdirectory.
Please explain what i did wrong in my fumble code :)
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35169106
Looking at your OP code (sorry, EE went off-line), your list includes a folder. If you have n files in a single folder, then you should be able to transfer the n files. But as you try to go up to the parent, that appears to be where the problem occurs.

Shouldn't your list only include files and not folders?
0
 

Author Comment

by:BeginToLearn
ID: 35169122
I am running ddd debugger now. i like it. but there is the problem. for example when i try to see the buffer value in server side. I always see '\000' < repeat 2048 times>, but i can see the value result is 2048.
 It can't work with the subdirectory.

 >>Shouldn't your list only include files and not folders?
could you please elaborate more? tks.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35169150
In your OP, you had this LOC:
>>   //  cout<<"<LIST> has :"<<t<<endl;
Uncommented, it produces the entire list of files to transfer. But, on that list was the sub-folder. Is that intentional? I didn't think you wanted folders to be on this list since you don't want to transfer a folder.
0
 

Author Comment

by:BeginToLearn
ID: 35169158
<LIST> has :/home/ubuntu/program/client
<LIST> has :/home/ubuntu/program/server.c
<LIST> has :/home/ubuntu/program/client.c
<LIST> has :/home/ubuntu/program/. 4096
<LIST> has :/home/ubuntu/program/test
<LIST> has :/home/ubuntu/program/huh/text_edit_commands.html
<LIST> has :/home/ubuntu/program/huh/text_compare_session_settings.html
<LIST> has :/home/ubuntu/program/huh
<LIST> has :/home/ubuntu/program/server
<LIST> has :/home/ubuntu/program/test.c

do u mean i should not include "." in the list?

0
 

Author Comment

by:BeginToLearn
ID: 35169182
I want to transfer all files and folders too :) For example FolderA has 8 files, and a FOLDERB. inside FOLDERB, there are 5 files. So i want to transfer all :) l
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35169183
I see that huh is a folder, and it is on the list. When you do with this entry?
0
 

Author Comment

by:BeginToLearn
ID: 35169204
oh i want to transfer "huh" folder and all files inside that folder too. It's similar like we copy and paste folder .
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35169266
Ok. If you have one subfolder, then keep in mind that the problem seems to appear only after completing the transfer of all the files in the subfolder.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35169296
Hi Begin,

I was going to suggest that you drop all of the data that your server code receives and just print out what you think that it should be doing.


Kent
0
 

Author Comment

by:BeginToLearn
ID: 35169475
I gonna remove all display.im offline for 8hrs.tks
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35169528
The client sends the name of the subfolder to the server. When the server receives this subfolder name, the size is 0. The server opens this subfolder as a file. Shouldn't the server be made aware that this name is a folder and then, if necessary, create the folder?
0
 

Author Comment

by:BeginToLearn
ID: 35169549
Server should creae folder like copy and paste hihi
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35169589
Ok, never did that way before to create a folder. I see that a file called "subfolder 0" was created. Hmm, it should have "drwx" attributes, but it only has "rw" attributes.

I had a chance to look at latest code patching in kdo's last code post. Stepping through the debugger in client and server, both exited normally with all files created. Then executed both without debugger and got a seg fault. So, some memory clobbering is going on somewhere.
0
 

Author Comment

by:BeginToLearn
ID: 35169619
I guess list have some problems
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35169645
How are you testing. I am on one system and change ubuntu to localhost. So the files get created in the correct folder since we provide the full pathname, and the folder already exists. Are you testing with two systems?
0
 

Author Comment

by:BeginToLearn
ID: 35169718
For testing in same pc,we test easily bc we appended filesizein filename.list loax all file before writing so it can aviid conflict.tks
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35169741
Right, by adding the filesize, we get a new filename - good idea.

I was wondering what you saw regarding the folder issue. I did not get a folder, but rather a file having the same subfolder name with a "space 0" appended to the name. Did you see this behavior.

fopen does not create a directory.

But you can do it several ways. Take a look at:
     http://rdsrc.us/EM87E2

But, now you have the issue of having the server distinguish between a file and a folder. And then there is the order in the list. If the server receives the full pathname where some of the folders do not exist, then you will have to parse the pathname into the set of folders, and verify that each exists, and once you find one that does not exist, then you can create it plus all of its subfolders (from the rest of the pathname.

If taking that approach, then having the actual folder name on the list is redundant. On the other hand, if you require that all the folders of a pathname already exist, then the folder name should be received before the files and folders that are under it. Then your server can test for the folder existence, and if needed, it can create the folder.
0
 

Author Comment

by:BeginToLearn
ID: 35169840
I see what you meant. I thought linux treat evertthimg like file.thar t I assume fopen could  create folder.i must do research aboyt it.im of computer for 6 hrs.tks for tellung me what issues need to study.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35169917
Hi Begin,

Something to keep in mind is that there is little correlation to the size of the buffer or object that you send() and the size of the buffer or object that you receive().

That is, the client can issue a send of 10 bytes and another of 20 bytes.  Nothing in the network layers say that the data will be received in buffers of 10 and 20 lengths.  If you call receive () with a buffer size of 30 or more bytes, you may well get both objects that you sent in a single read.

One way around that is to send something in the front of each block with details about the block.

The example below shows 1 (of many) possible ways to do this.  The key to this is that the server is flexible.  The sending of an unknown number of blocks isn't rigid and inflexible.


Kent

#define MAXBLOCK 2000
  enum {NOTHING, FILENAME, DATA};

  int   BlockType;
  int   BlockLength;
  char   Buffer[MAXBLOCK ];

//  Send the file name

  BlockType = FILENAME;
  send (sock, &BlockType, sizeof (int), 0);
  BlockLength = strlen (Buffer);
  send (sock, &BlockType, sizeof (int), 0);
  send (sock, Buffer, BlockLength, 0);

//  Send the data

  buffer= (char*)malloc(sizeof(char) * MAXBLOCK );
  while ((BlockLength = fread (Buffer, 1, MAXBLOCK, fp) > 0)
  {                
    BlockType = DATA;
    send (sock, &BlockType, sizeof (int), 0);
    send (sock, &BlockLength, sizeof (int), 0);
    send (sock, &BlockType, sizeof (int), 0);    
    BytesSent + send (sock, buffer, result, 0);
  }
  free(buffer);             
  fclose(fp);

Open in new window

0
 

Author Comment

by:BeginToLearn
ID: 35169973
I will study your guide more careful bc I cant access to pc now.but I see the problem more clear now.file path issue when create with different folder n write in it.
0
 

Author Comment

by:BeginToLearn
ID: 35170124
I have 1 idea to test it.let put all test data in 1 folder   and store data after servee receive in another folder.by that way, no need to worry about testing issue hihi
0
 

Author Comment

by:BeginToLearn
ID: 35170592
back to action :) i am reading  all feedback carefully. tks a lot.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 35171398
I transferred all the files including those in a subfolder. The list did not include subfolder names (and since I'm on one system, the subfolders exist so no problem). When remoting, you'll have to deal with folder creation as we spoke.

Before sending the filename/filesize string, I sent a binary int value of the length of this message, and then only read the string.

By rights, you could send the total number of files also as a binary int (i.e., 4 byte transfer).

The problem was that when the server read the filename/filesize message into its large buffer, that buffer may also have been filled with part of the trunk (which you would then ignore). One approach would be to check the string length of the filename/filesize message and see whether it matched the return value of the read. If the return value of the read was larger, then you know that some of the trunk got into the buffer, so you would then write out that to the disk.

But now you are out of sync w.r.t. your number of chunks and remainder approach. I never liked that approach anyway because as kdo also agrees, it is unnecessarily complex. Please review the comment made in
      http://rdsrc.us/q6EGdN :
"I don't think you need to deal with whether a file is greater than BUFFER_SEND. While I don't believe that is the cause of your segment fault, it does clutter up your design. Before you receive the first chunk of data from the file, you know how many bytes you will receive. So that is your count. The server just keeps reading bytes (decrementing the count after each read until 0)".

"When you read n bytes, you should check the return value always checking for errors  of course; but also to determine how many bytes you actually read. It is not necessarily an error if you read less than n bytes - it just means that you need another pass on read. In any case you just decrement the byte_count_remaining variable that was initialized to the original file size, and you know that you are done when this  byte_count_remaining variable is 0. If somehow you end up with more or less than this final value of 0, then you have an error."

Nevertheless, even without doing the code redesign improvements, the transfers orked here.

Also, you should check for error conditions on every read or send. In the server, there is one read where you do not check the return value. Be back tonight (maybe earlier) to see if you got this working
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35171407
It's very useful (I would say important) to remove as many warnings as possible, so that when a serious warning appears while maintaining your program, then you will actually quickly notice it rather than have to scan a dozen warnings.

To get rid of many warnings, you can replace the printf statements with either perror() or your error() which in turn calls perror() and exits.

Here is another example (same with t1):
>> if( t2= close(sockfd) == -1)
>>    error(...)
Just add the parenthesis as you see fit. Note that == has precedence over = , and changing the order will cause t2 to have different values when there is an error.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35171433
Re: the subfolder issue when going remote..
Before trying to write code, it might be beneficial for you to jot down pseudo-code for review.

You do not have to include folders in the list as they are inferred from the full pathnames that you are providing. But now you have to parse and test and if necessary, create a directory.

Alternatively, you could send first a tree-list message that represents all the required folders.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:BeginToLearn
ID: 35172363
After reading all comments carefully, I want to verify those bullets:
   + Remove all warnings and use error().
   + In server, receiving block with current implementing is cluttered up. I should use file size as decremental flag until it equal 0 . ( use similar approach from client side)  
   + Use system call to make folder
   + First, send a tree-list message for all require folders and create those folder if not exist. Then I can use the current code.
   + In the code Kdo post, I have question
 

//  Send the file name

  BlockType = FILENAME;

  send (sock, &BlockType, sizeof (int), 0);
 
  BlockLength = strlen (Buffer);

  send (sock, &BlockType, sizeof (int), 0);  << why do we need to send BlockType second time?

  send (sock, Buffer, BlockLength, 0);

//  Send the data

  buffer= (char*)malloc(sizeof(char) * MAXBLOCK );

  while ((BlockLength = fread (Buffer, 1, MAXBLOCK, fp) > 0)
  {                
    BlockType = DATA;
    send (sock, &BlockType, sizeof (int), 0);
    send (sock, &BlockLength, sizeof (int), 0);
    send (sock, &BlockType, sizeof (int), 0);  <<<< why do we need to send BlockType second time?

    BytesSent + send (sock, buffer, result, 0);
  }

Tks a lot.

     
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35172581
If your filename or folder names have spaces in it, then your parsing fails. You can use strrchr to find the filename and filesize with one usage of strrchr :
     http://www.cplusplus.com/reference/clibrary/cstring/strrchr/
The idea of messaging is for the sender and receiver to know exactly how many bytes are being sent so that the receiver reads exactly what is needed for the message and not accidentally reading part of the subsequent message. One approach is to have a consistent fixed length message header structure which includes the number of bytes being sent in the body of the message. Another way used for short messages of known width is to just send the data in binary form (this is basically a brief message header). Numbers that fit into a 32-bit integer is an example of this latter case, so you don't need the length.

When sending binary 32- or 16-bit integers, then if you need generality, then you need to deal with platforms having different endianness. If you need this generality, then see:
     http://en.wikipedia.org/wiki/Endianness  -- Endianness  
     http://linux.die.net/man/3/ntohl             -- host/network ordering
To avoid endianness issues, you could define an ASCII message field of constant width (say 8 chars) that represents the "message length" padded with spaces.

Messages:  

1. Number of files to be transferred

You should either send a message: "messge length" + "number of files ASCII string" or you can just send a 16- or 32-bit integer that represents the total number of files.

2. Filename + Filesize

This is the message that you already send prior to sending the trunk of the file. You should preceed this message with the number of bytes in the message so that server reads exactly those number of bytes. (I sent a 32-bit int, and then was able to transfer all files including those in subfolders.)

3. Trunk of the File

Since you already sent the filesize, it is not necesary to send the filesize again.However, the above is used just to get your program working. The other advice given is to make your program have a consistent messaging pattern so that there are no special rules for each message. I would try to get a functional program working first, and make step-wise improvements in the software engineering aspect of your development with complete regression test for each improvement (make sure you backup your previous working version).

0
 

Author Comment

by:BeginToLearn
ID: 35172673
  For my level, I think i don't need to worry about Endianness and host/network ordering.
   Let me work two things:
         + fix string parse by using strrchr
          +on the the improvement of message. By sending number of files to transfer and filename + filesize. In server, receiving block with current implementing is cluttered up. I should use file size as decremental flag until it equal 0 . ( use similar approach from client side). Give me around 1 hr to finish those tasks.    
 
     After I improve them, I can work on directory. tks a lot.  
0
 

Author Comment

by:BeginToLearn
ID: 35172936
my little progress. It works with only files. Please take a look.
Should I move on to build the tree-directory list now? tks for your patient. I am so slow.
client.c
server.c
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35173094
I'll try to take a look at your code as soon as I get a chance (today, if possible; certainly tomorrow).

>> Should I move on to build the tree-directory list now?
Not sure how familiar you are with tree structures. If you are and believe you have a good handle on how you would define a tree-directory message, then you could post pseudo-code. But there are a number of steps:

(1) You have to collect the data in some container that you define, and then
(2) convert that container to a single tree-directory message.
This sounds like a lot and probably should be one or even two new questions depending upon the amount of expertise you already have in this area. And that's just on the client side.
You may need other questions for the server:
(3) on how to parse this message and
(4) actually ensure that the directory tree is created properly before you receive the set of file transfer messages.

>> I am so slow.
Not really. :)
0
 

Author Comment

by:BeginToLearn
ID: 35173178
I need to open a new question regarding to building tree-directory. I think i can handle the tree from with your guide in another question. I will be back at midnight .  
 Tks for your motivaion and help.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35175598
Hi BeginToLearn,

I got involved here primarily because of the subfolder issue, which you are starting to work in the new question you opened. (But I would ignore that problem until you fully understand the remaining issues here.)

I also made some suggestions that are related to the segment faults stemming from your file transfer. Now, kdo is providing you with an excellent methodology for sending messages. So, please continue with kdo in this regards. In the end you will have a working program in this question (with only subfolder issues remaining).

Could you check to see whether you uploaded the latest version of your client and server code. I tried compiling the latest version, but got warnings of which at least one represents a serious functional error (i.e., not just a compiler nuisance warning). I think you should strive to post only no-warning files. If you are unable to remove the warnings, then you can always ask a new question to remove compile/build warnings/errors, and you will get a quick response.

In your uploads, I did not see the change related to this important comment:

>> Filename + Filesize
>> This is the message that you already send prior to sending the trunk of the file. You should precede this message with the number of bytes in the message so that server reads exactly those number of bytes. (I sent a 32-bit int, and then was able to transfer all files including those in subfolders.)

Note that this comment is for the quick method regarding what kdo already spoke about. Namely, you only want to read exactly the number of bytes that you know you should read and none more. kdo's approach is general in dealing with this issue. As long as you have time, go with kdo's approach.

A heads-up for you. There are issues in the new server approach w.r.t. the number of bytes read and written. It would be beneficial to your learning experience if you could take the time to try to figure out what is wrong yourself.

If you run up against a wall, then by all means let kdo know the problem, and he'll give you a good hint.

I got your program to work in my quick and dirty method. But, to prepare for commercial programs, I'd go with a more uniform messaging approach.
0
 

Author Comment

by:BeginToLearn
ID: 35175658
what does  "w.r.t" present?
 My upload should have no error in compiling.  I forgot to change  code reflecting the important comment  
    >> Filename + Filesize      
     >>This is the message that you already send prior to sending the trunk of the file. You should precede this message with the number of bytes in the message so that server reads exactly those number of bytes. (I sent a 32-bit int, and then was able to transfer all files including those in subfolders.)

 Let me do it now.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35175668
w.r.t. = with respect to

Ok, but there are other issues w.r.t. the server not writing the correct number of bytes to the file.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35175671
Ok, don't forget to use the gcc or g++ -Wall compiler option
0
 

Author Comment

by:BeginToLearn
ID: 35175681
omg, now i recompiled and executed them again before working on that important comment, I go segmentation fault. haiz
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35175684
You have to fix the other problem I talked about also. :)
0
 

Author Comment

by:BeginToLearn
ID: 35175698
I see now. It only works with files inside current directory only. If any other directory exist there, it has segmentation core.
 So i only need to work on " the important comment" hihi
0
 

Author Comment

by:BeginToLearn
ID: 35175737
Did you mean this:

   Client side:
        // at this place: add send filename + filesize
        while (result= fread (buffer, 1, BUFFER_SEND, fp) > 0)
        {                
           
                BytesSent + send (sock, buffer, result, 0);
        }
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35175769
>> BytesSent + send (sock, buffer, result, 0);
makes no sense to me.

The serious problem is on the server side.

When you test, you should verify that the file created matches the file from which it is derived (length check is good; a diff is even more assuring - but I've found with your code that if the lengths are the same, the diff is also good.)
0
 

Author Comment

by:BeginToLearn
ID: 35175819
Server:
    while (result= fread (buffer, 1, BUFFER_SEND, fp) > 0)
        {      
            BlockLength = result;
                send( sock, &BlockLength, sizeof(int), 0); // sending info about size of sending packet
                        
                send (sock, buffer, result, 0);
        }

Client:

while( size >0)
                {
                        read( newsockfd,&BlockLength, sizeof(int));// get size of packet receiving
 
                  result= read(newsockfd,buffer,BUFFER_SEND);
                        if( result <BlockLength)
                        {
                       error("reading trunk is failed\n");
                  }
                        result= fwrite (buffer,1,BUFFER_SEND,fp);
                        if( result != BUFFER_SEND)
                        {      
                             error("writing error\n");
                        }
                        memset(buffer,'\0',BUFFER_LEN);
                        size= size - BUFFER_SEND;
                }        

Is that what you mean the server need to verify how many bytes it need to receive?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35175858
Heh, you labelled the Server: and Client: backwards.

I see what you are doing, and I think it is beginning to capture the spirit of what kdo was saying; namely, to include a message header that informs gives the length of the body of the message sent (not "packet" since that term is used in tcp/ip).

With this approach, you wouldn't even need to keep track of size since the client could then send a special EOF message.

But, this isn't the approach you or I were taking before. And this approach still suffers from the same problem that is in the server. You need to do some debugging. It isn't obvious (obviously, or you would have seen the problem); but once you figure out what the problem is, then you will have a good chance of fixing it.

So, first identify the basic problem - the segment fault is just a symptom.

Perhaps, start off with no sub-folders and just have two files in the current folder. That should not work.
0
 

Author Comment

by:BeginToLearn
ID: 35175948
ouch !!!. right now my current directory has 4 files: client, client.c, server,server.c. It works . can u pls tell me which part has potential/hidden bugs?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35175971
I'll be happy to test the scenario you described. But I'll need your version - one that has no warnings when using -Wall -ggdb options.
0
 

Author Comment

by:BeginToLearn
ID: 35175996
let me fix those warnings with -Wall  option. give me some minutes. tks.
0
 

Author Comment

by:BeginToLearn
ID: 35176048
here you are :)
client.c
server.c
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35176275
Here is server error:
filesize is : 5605
size is 5605
reading trunk is failed
: Invalid or incomplete multibyte or wide character

Open in new window

No error messages from client.

As kdo said, there are timing issues to be considered. Try this. Set a breakpoint in the server at
     for( int i = 0; i <totalfile;i++)
Then run the client without any breaks.
Then step through the server looking at every variable change in value. See if the program still works for you.
0
 

Author Comment

by:BeginToLearn
ID: 35176288
Let me debug now
0
 

Author Comment

by:BeginToLearn
ID: 35176434
I can see some thing  weird:
   + BlockLength always  equa 0  afterread( newsockfd,&BlockLength, sizeof(int))
   + result is alway equal 1 after ead(newsockfd,buffer,BUFFER_SEND)

however, result equal 2048 after e (buffer,1,BUFFER_SEND,fp);
 I need your explanation here.
  Server crashes because of strncpy().
0
 

Author Comment

by:BeginToLearn
ID: 35176446
I can see some thing  weird:
   + BlockLength always  equa 0  after read( newsockfd,&BlockLength, sizeof(int))
   + result is alway equal 1 after read(newsockfd,buffer,BUFFER_SEND)

however, result equal 2048 after fwrite (buffer,1,BUFFER_SEND,fp);
 I need your explanation here.
  Server crashes because of strncpy().
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35176502
So, you're saying that this code works well in some scenarios and not in others. There are multiple problems and then multiple questions.

We should wait for kdo since I think he first hit the nail on the head (although the issue of timing was implicitly discussed in your previous question).

Given the difficulties in that sometimes you have no problems and sometimes you do, then I guess you should not take any of my shortcuts, but instead start modifying your code using kdo's methodical approach for every message. Right now, you are handling some messages correctly; but others are not (and yet you say they give you the correct results sometimes).

One thing about subfolders.. After I got your code running, then it doesn't matter if there are subfolders. The files in them transferred OK in the single subfolder that I had. What went wrong was that the subfolder itself appeared as a file (with space 0 appended to the name). But since the folders existed in my test environment, then the transferred filename fopen'd correctly.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35176539
I would read kdo's text carefully in http:#35169917 and ask questions about it if there is something that you do not understand.

You started adopting the idea, but not completely. After you adopt the idea for every data transfer, let's see if the problem goes away.
0
 

Author Comment

by:BeginToLearn
ID: 35176675
Sorry . I just came back from supermarket. I read kdo comments and asked about :

  send (sock, &BlockType, sizeof (int), 0);  << why do we need to send BlockType second time?
  send (sock, &BlockType, sizeof (int), 0);  <<<< why do we need to send BlockType second time?


 in http:#35169917
I think we need to find another way because the code is unstable.Let's wait kdo comeback.

Too much problems :) It's so complicated than I thought.  
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35176933
So, when is your deadline?
Would it help if you didn't send any BlockType at all. Usually we need to distinguish between one message and another when any message type can come in. In your case, the messaging between client and server is lock-stepped. It is true however that sending the BlockType does add an extra measure of sanity checking to verify that you really are in lock-step. Given the issues you are having (and the fact that it doesn't take much to get your program working), maybe for the first phase, you leave out the BlockType.

So, would you find it easier to proceed if you just dealt with BlockLength? If so, then use that for every message that you send.
0
 

Author Comment

by:BeginToLearn
ID: 35176947
deadline is 24. I am going to work and come back at 1 am. I will think about it. my cellphone can access internet so i can read whenever you post :). appreciated your  patient and help.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35177004
How do I use "ubuntu" without having to change your code to "localhost"?

Is that 24 midnight?
Do you ever sleep?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35177012
Don't forget, if you have a specific problem that you can describe simply, then if you ask a new question, it will be easy for others to help 24 x 7.
0
 

Author Comment

by:BeginToLearn
ID: 35177182
Ioften go to sleep at 1::300 or 2am n wake up at 7 am.im very exhausted but I saw u online early.i dont want to lose time.it is due 24  at midnight
0
 

Author Comment

by:BeginToLearn
ID: 35177192
I gonna open new question about this toniye bc it is too long now
0
 

Author Comment

by:BeginToLearn
ID: 35177229
I just run clienr n server on ubuntu as guess of virtur box on win 7.so I have no idea
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35177257
Ok, try to make the question complete as possible, as self-contained as possible, AND as simple as possible.

I tried to install Oracle's VirtualBox on XP, but I got a warning saying that the software was uncertified and could mess up my system; so I figured I better learn more about the dangers more.

I also was going to install a CD Clone so that I could install an ISO without having to burn a CD, and got the same message.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35177286
I checked on kdo. It looks like he is inactive today on EE - maybe his day off.
You are actually very close to a fix. In the meantime, you can define your tree hierarchy message.
0
 

Author Comment

by:BeginToLearn
ID: 35177422
I let netbook instaling same ubuntu on virtualbox on windows xp.so farso good.its running at home for 6hrs already.
very cool bc we cab have same system.tks for ur time and understanding.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35177474
I hear a power nap is rejuvenating.
    http://en.wikipedia.org/wiki/Power_nap
0
 

Author Comment

by:BeginToLearn
ID: 35177638
I defintely need nap. I also need to do research on many stuffs.that is y u see I ask multiple questions hihi
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now