GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Mar-2005, 15:40
XProject XProject is offline
New Member
 
Join Date: Mar 2005
Posts: 1
XProject is on a distinguished road

C: Winsock's 'send()' not blocking


I am currently trying to write a programin C on Windows 2000 that will send a file between two computers. The code I have right now seems to work, but there is a substanial problem with it. When sending the binary data from the server to the client, sometimes packets will mesh together. This will cause data to be lost because I have predetermined a size for each chunk of the file. Once the data arrives, I have the client only get the normal packet size. To help explain this, here is an example of the server:
CPP / C++ / C Code:
#define CHUNK 256 //define the size of each chunk
char buff[CHUNK];
FILE *fp = fopen("example.exe", "rb");
while(!feof(fp))
{
     fread(&buff, sizeof(char), CHUNK, fp);
     send(sock, buff, CHUNK, 0);
}
And here's how the client looks:
CPP / C++ / C Code:
#define CHUNK 256
char buff[CHUNK];
FILE *fp = fopen("example.exe", "wb");
unsigned long total_bytes = //get file size from server
unsigned long current_byte = 0;
while(1)
{
     recv(sock, buff, CHUNK, 0);
     fwrite(&buff, sizeof(char), CHUNK, fp);
     current_byte += CHUNK;
     if(current_byte >= total_bytes) break;
}
Using Ethreal, here is an example of what happens, supposing CHUNK was set to 10:
First Packet: "this is an" 10 bytes
Second Packet: " example o" 10 bytes
Third Packet: "f the problem. blah." 20 bytes
Normally, this will occur right around the 8 KB mark for some reason. If anyone could expain to me how to fix this problem, it would be greatly appreciated.
  #2  
Old 22-Mar-2005, 16:53
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Assuming you are using TCP to transfer files between client and server, I would suggest following. This is not tested.

Server side
CPP / C++ / C Code:
#define CHUNK 256 //define the size of each chunk
char buff[CHUNK];
int bytes_read;
FILE *fp = fopen("example.exe", "rb");
while(!feof(fp))
{
     if ( (bytes_read = fread(&buff, sizeof(char), CHUNK, fp)) > 0 )
     {
        send(sock, buff, bytes_read, 0);
     }
}

Client Side:
CPP / C++ / C Code:
#define CHUNK 256
char buff[CHUNK];
FILE *fp = fopen("example.exe", "wb");
unsigned long total_bytes = //get file size from server
unsigned long current_byte = 0;
int bytes_received;

while ((bytes_received=recv(sock, buff, CHUNK, 0)) != -1)
{
    if (bytes_received == 0 ) break;  //TCP Connection closed.
    fwrite(&buff, sizeof(char), bytes_received, fp);
}

 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Do you want to send this error to Microsoft? priyanka Open Discussion Forum 11 29-Oct-2004 15:58
Receive from HTML Form, add some then send it to other server imlek MySQL / PHP Forum 0 21-Aug-2004 22:33
send pc image to tv screen?! Hood Computer Hardware Forum 5 05-May-2004 11:51
Blocking Spambots by .htaccess BobbyDouglas Apache Web Server Forum 2 16-Jan-2004 10:32
send email via local intranet skyloon MySQL / PHP Forum 1 30-Jun-2003 07:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:30.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.