|
-
August 13th, 2004, 01:46 PM
#1
CSocket data lost
I've been on the project that using CSocket to send file from client to server. When i test the program, only part of data send through CSocket(about one third of the original data). I use CSocket as a SOCK_STREAM. Can anyone figure out the problem on my code?
thnx
-
August 13th, 2004, 06:26 PM
#2
I did a quick google search on csocket data loss.......
and hit this page that offers a possible explanation and a solution in vb.
http://www.vbip.com/forum/topic.asp?id=4030
now I know this is a vb forum link I am posting, but it seemed that it may offer some insight to your problem.
HTH
-
August 14th, 2004, 04:00 AM
#3
-
August 14th, 2004, 06:50 AM
#4
I don't know about CSockets, ISTR reading somewhere that the send data length should be less than 8K. You can try and see it for yourself.
Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.
* While posting code sections please use CODE tags
* Please check the codeguru FAQ and do a little search to see if your question have been answered before.
* Like a post, Rate The Post
* I blog: Network programming, Bible
I do all things thru CHRIST who strengthens me
-
August 14th, 2004, 07:49 AM
#5
Well one cannot recieve whole data from one Receive Call , so one will have to call Receive multiple times to be sure that all data is successfully fetched.
But as CSocket is a Blocking Socket so one should know the incoming data size prior calling receive so that one could terminte the loop at right time.
Unmanaged in a .NET world
-
August 16th, 2004, 12:11 AM
#6
 Originally Posted by atif_ilm
But as CSocket is a Blocking Socket...
Technically, CSocket uses a non-blocking socket, but only fakes blocking.
Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.
* While posting code sections please use CODE tags
* Please check the codeguru FAQ and do a little search to see if your question have been answered before.
* Like a post, Rate The Post
* I blog: Network programming, Bible
I do all things thru CHRIST who strengthens me
-
August 16th, 2004, 06:43 AM
#7
 Originally Posted by Mathew Joy
Technically, CSocket uses a non-blocking socket, but only fakes blocking.
yehh yehh whatever but atleast it pretends to be a Blocking Socket.
Unmanaged in a .NET world
-
August 16th, 2004, 06:27 PM
#8
i divide the file into small chunks of 8kb.
but when i sent the chunks length prior to sending the chunks the program crashed.
I thought the cause is on the OnAccept() notification which is on the receiver side,after call CSocket::Receive(filelength...), i call CSocket::Send(notify) to notify the sender to begin send the file. Are we not suppose to call receive and send on the same function.
Could anyone suggest some idea how to send filelength be4 send actual file.
thnx
-
August 16th, 2004, 11:55 PM
#9
you have to develop your own protocole for sending file into small chunks eg
Code:
when socket is accepted then
Receiver Sender
// Send Ready message for
// Start receiving file data
1 ReceiverSocket.Send(Ready)
1 SenderSocket.OnReceive()
{ // when receive ready notification then
// send total file length
SenderSocket.Send(FileLength)
}
ReceiverSocket.OnReceive()
{
// When receive total file length
// then send request for chunk length
// and store total file length in variable
2 ReceiverSocket.Send(SendChunkLength)
}
2 SenderSocket.OnReceive()
{ // when receive request for chunk
// length then send chunk length
// eg 8kb
SenderSocket.Send(Length8kb)
}
ReceiverSocket.OnReceive()
{
// When receive chunk length
// divide fileLength into number of chunks
// just like FileLength/chunkLength and store
// in a variable and send request to send
// first chunk
3 ReceiverSocket.Send(SendFirstChunk)
}
3 SenderSocket.OnReceive()
{// when receive request of
// chunk with chunk number then
// send that chunk with its number
SenderSocket.Send(1stChunk+Data)
}
ReceiverSocket.OnReceive()
{
// First chunk received write it into file
// Send request for next chunk
4 ReceiverSocket.Send(SendSecondChunk)
}
4 SenderSocket.OnReceive()
{
SenderSocket.Send(2ndChunk+Data)
}
just like that you can receive all chunks
hope this will help u .
A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.
NAUMAAN
-
August 17th, 2004, 12:07 AM
#10
Take a look at File Transfer Using CSockets.
Also, don't forget to read this
Last edited by Ejaz; August 17th, 2004 at 12:10 AM.
-
August 17th, 2004, 12:50 AM
#11
the following code is from the link that someone earlier in this thread
//////////////////////////////////////////////////////////
////////this code is used to send large binary file
Code:
do
{
dwRead = myFile.Read(data, 4096);
sockRecv.Send(data, dwRead);
}
while (dwRead > 0);
/////////////////////////////////////////////
//////////and this code for receive that file////
Code:
byte* data = new byte[4096];
do
{
dwRead = sockClient.Receive(data, 4096);
destFile.Write(data, dwRead); // Write it
}
while (dwRead > 0);
//////////////////////////////////////////
THE PROBLEM IS: we are not sure about the order of each sub data. We may in the situation that connection is slow, therefore the previous data not yet write to the file, but coming new data due to the while loop.
-
August 17th, 2004, 02:07 AM
#12
-
August 17th, 2004, 08:34 AM
#13
I think you should maintain , two connection , Control Connecton and Data connection.
Control connection is to send the controls signals such as Incoming File Size , Error Messages and Data connection for transfer of original bytes.
but keep in mind the blocking nature of CSocket for example
if incoming File Size was 502 bytes
and u do things like this
Receive(buffer,1024);//CSocket would block here unless it gets complete 1024 data , which in this case will not be there.
also it is not necessary that if you call "Receive(buffer,1024); " you will get the whole data at once , may be you get 500 bytes of total 1024 so u will also have to keep track of actual received bytes (return value from Receive).
so receive all data in loop and as actual read bytes =File Size break that loop.
another very famouse problem with CSocket is its Fake FD_READ notification while receiving the data in OnReceive event handler , so its better at the start of OnReceive disable FD_READ using AsyncSelect and enable it efter receiving whole data.
I hope i addressed some important issues regarding CSocket .
If someone could add to this information , he is welcome.
Unmanaged in a .NET world
-
September 4th, 2004, 03:20 AM
#14
Re: CSocket data lost
For my server/client that i wrote in CSocket i could only send 1000 bytes at a time...I just kept sending 1000 bytes at a time until i reached the end of the file. I also told my program to Sleep(1000); after sending each byte in order to prevent problems. So far, it has been working flawlessly.
-
September 12th, 2004, 10:41 AM
#15
Re: CSocket data lost
 Originally Posted by Drummer16161616
For my server/client that i wrote in CSocket i could only send 1000 bytes at a time...I just kept sending 1000 bytes at a time until i reached the end of the file. I also told my program to Sleep(1000); after sending each byte in order to prevent problems. So far, it has been working flawlessly.
Well, that doesn't sound good. You are sending less than 1MB at a time and you sleep 1 sec at times. Your usage of winsock has been restricted for the sake of just using, not for its usefullness. You get far better results if you can use just the basic blocking socket using winsock APIs. And programming is easier that using CSocket.
Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.
* While posting code sections please use CODE tags
* Please check the codeguru FAQ and do a little search to see if your question have been answered before.
* Like a post, Rate The Post
* I blog: Network programming, Bible
I do all things thru CHRIST who strengthens me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|