473,473 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Socket Receive Binary

I am writing a simple program to receive over http using windows
sockets. my program does fine receiving text files. But returns the
incorrect data when receiving image files. (The size of the files are
different). My code is below. The header has already been read using
the same Receive() function. What have I done wrong.

ofstream os("file.dat", ios::binary);

//Retrieve the body
try {
while (1) {
l = s.Receive();
if (l.empty()) break;

os.write(l.c_str(), l.length());

//cout << l;
//length += l.length();
//cout.flush();
}
}
catch (const char* s) {
cerr << s << endl;
}
catch (string s) {
cerr << s << endl;
}
catch (...) {
cerr << "unhandled exception\n";
}
os.close();

string Socket::Receive() {
string ret;
while (1) {
char r;

switch(recv(sock, &r, 1, 0)) {
case 0:
return "";
case -1:
if (errno == EAGAIN)
return ret;
else
return "";
}

ret += r;
if (r == '\n') return ret;
}
}

Jan 30 '07 #1
2 4316
"iwasinnihon" <iw*********@hotmail.comwrote in
news:11**********************@a34g2000cwb.googlegr oups.com:
I am writing a simple program to receive over http using windows
sockets. my program does fine receiving text files. But returns the
incorrect data when receiving image files. (The size of the files are
different). My code is below. The header has already been read using
the same Receive() function. What have I done wrong.
Out of curiosity.. have you tried this code with a text file that doesn't
end in a newline? (Hint: I suspect you have a problem when recv returns a
0.... what happens to all of the data that you have accumulated since the
last newline?)
ofstream os("file.dat", ios::binary);

//Retrieve the body
try {
while (1) {
l = s.Receive();
if (l.empty()) break;

os.write(l.c_str(), l.length());

//cout << l;
//length += l.length();
//cout.flush();
}
}
catch (const char* s) {
cerr << s << endl;
}
catch (string s) {
cerr << s << endl;
}
catch (...) {
cerr << "unhandled exception\n";
}
os.close();

string Socket::Receive() {
string ret;
while (1) {
char r;

switch(recv(sock, &r, 1, 0)) {
case 0:
return "";
case -1:
if (errno == EAGAIN)
return ret;
else
return "";
}

ret += r;
if (r == '\n') return ret;
}
}

Jan 30 '07 #2
"iwasinnihon" <iw*********@hotmail.comwrote in message
news:11**********************@a34g2000cwb.googlegr oups.com...
>I am writing a simple program to receive over http using windows
sockets. my program does fine receiving text files. But returns the
incorrect data when receiving image files. (The size of the files are
different). My code is below. The header has already been read using
the same Receive() function. What have I done wrong.

ofstream os("file.dat", ios::binary);

//Retrieve the body
try {
while (1) {
l = s.Receive();
if (l.empty()) break;
If found that while receiving binary files it is possible to not receive a
packet during a read, yet the file is still transmitting. In my own code to
receive a binary file via HTTP I don't quit until I reach the file size:

if ( TallyBytesDownloaded >= TotalFileSize )

Since my code is totally different than yours, you probably wouldn't use and
if statement but put it in the while statement.

while ( BytesDownloaded < FileSize )
{
// ...
}
>
os.write(l.c_str(), l.length());

//cout << l;
//length += l.length();
//cout.flush();
}
}
catch (const char* s) {
cerr << s << endl;
}
catch (string s) {
cerr << s << endl;
}
catch (...) {
cerr << "unhandled exception\n";
}
os.close();

string Socket::Receive() {
string ret;
while (1) {
char r;

switch(recv(sock, &r, 1, 0)) {
case 0:
return "";
case -1:
if (errno == EAGAIN)
return ret;
else
return "";
}

ret += r;
if (r == '\n') return ret;
This is a binary file you're downloading, don't expect carrige returns or
line feeds. Just return whatever you received (which is why you may have a
blank packet)
}
}

Jan 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Richard | last post by:
Level: Java newbie, C experienced Platform: Linux and Win32, Intel Another programmer and I are working on a small project together. He's writing a server process in Java that accepts input...
2
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send...
1
by: djc | last post by:
the system.net.sockets.socket class has the 'sendfile' method. What do you do on the other end to 'receive' the file? I know the regular socket.receive will receive the data into a buffer (a byte...
6
by: Sharon | last post by:
Hi all. I'm trying first time async socket connection. In all the examples i've seen, the server connection is closed when the message is complete. Is it common to close the connection after...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
6
by: billie | last post by:
Hi all. I'm writing a TCP-based application that I will use to trasfer binary files through the network. This piece of code represents how do I get a file from a remote peer and save it on my local...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
4
by: pradqdo | last post by:
Hi folks, I have a very strange problem when I try to port my client/server program to cygwin. It is a simple shell program where the server executes client's commands + it can send and receive...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.