473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Socket Send Binary (Jpeg)

I writing a program that will send both a text/html document as well
as jpegs using winsock. My code works find for the text, but will not
work with jpegs. My code is below. What am I missing? Can someone
point me in the right direction.

ifstream file;
file.open(r.c_str(), ios::binary);
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
while( !file.eof() ) {
file.read(buffer, 1024);
int BytesLeft = strlen(buffer);
while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
cout << buffer << endl;
}
}

Feb 22 '07 #1
11 6613
* iwasinnihon:
I writing a program that will send both a text/html document as well
as jpegs using winsock. My code works find for the text, but will not
work with jpegs. My code is below. What am I missing? Can someone
point me in the right direction.

ifstream file;
file.open(r.c_str(), ios::binary);
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
while( !file.eof() ) {
file.read(buffer, 1024);
int BytesLeft = strlen(buffer);
Here.

while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
cout << buffer << endl;
}
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 22 '07 #2
Am I missing something? What are you telling me?

On Feb 21, 7:45 pm, "Alf P. Steinbach" <a...@start.nowrote:
* iwasinnihon:
I writing a program that will send both a text/html document as well
as jpegs using winsock. My code works find for the text, but will not
work with jpegs. My code is below. What am I missing? Can someone
point me in the right direction.
ifstream file;
file.open(r.c_str(), ios::binary);
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
while( !file.eof() ) {
file.read(buffer, 1024);
int BytesLeft = strlen(buffer);

Here.
while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
cout << buffer << endl;
}
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Feb 22 '07 #3
iwasinnihon wrote:

Please don't top-post.
On Feb 21, 7:45 pm, "Alf P. Steinbach" <a...@start.nowrote:
>>* iwasinnihon:

>>>I writing a program that will send both a text/html document as well
as jpegs using winsock. My code works find for the text, but will not
work with jpegs. My code is below. What am I missing? Can someone
point me in the right direction.
>>>ifstream file;
file.open(r.c_str(), ios::binary);
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
while( !file.eof() ) {
file.read(buffer, 1024);
int BytesLeft = strlen(buffer);

Here.
Am I missing something? What are you telling me?
Look up strlen paying particular attention to what it expects as its
parameter.

--
Ian Collins.
Feb 22 '07 #4

"iwasinnihon" <iw*********@hotmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Am I missing something? What are you telling me?

On Feb 21, 7:45 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* iwasinnihon:
I writing a program that will send both a text/html document as well
as jpegs using winsock. My code works find for the text, but will not
work with jpegs. My code is below. What am I missing? Can someone
point me in the right direction.
ifstream file;
file.open(r.c_str(), ios::binary);
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
while( !file.eof() ) {
file.read(buffer, 1024);
int BytesLeft = strlen(buffer);

Here.
strlen determines the length of the string by the position of the first NULL
characiter it finds \0 or ascii 0. Binary data, including jpegs, can have
NULL bytes as valid data. You will need some other way to determine how
much data to send (look at the return value of file.read for example).
while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
cout << buffer << endl;
}
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Feb 22 '07 #5
Thank you for your help thus far. I have changed it from strlen() to
gcount(). Now I am getting part of the picture. Why can't I get the
entire picture?

Feb 22 '07 #6
iwasinnihon wrote:
Thank you for your help thus far. I have changed it from strlen() to
gcount(). Now I am getting part of the picture. Why can't I get the
entire picture?
Please retain the context of the part of the message you are replying to.

Have a think about what happens to BytesIndex each time something is read.

--
Ian Collins.
Feb 22 '07 #7
On Feb 21, 9:23 pm, Ian Collins <ian-n...@hotmail.comwrote:
iwasinnihon wrote:
Thank you for your help thus far. I have changed it from strlen() to
gcount(). Now I am getting part of the picture. Why can't I get the
entire picture?

Please retain the context of the part of the message you are replying to.

Have a think about what happens to BytesIndex each time something is read.

--
Ian Collins.
You guys have really been helpful. I have altered my code to be as
seen below. It works fine for text documents and jpegs. But, it
won't work for Gifs or PNGs. It only displays part of the picture.
Any ideas why?

while( !file.eof() ) {
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
file.read(buffer, 1024);
int BytesLeft = file.gcount();
while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
}
}

Feb 22 '07 #8

"iwasinnihon" <iw*********@hotmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
On Feb 21, 9:23 pm, Ian Collins <ian-n...@hotmail.comwrote:
>iwasinnihon wrote:
Thank you for your help thus far. I have changed it from strlen() to
gcount(). Now I am getting part of the picture. Why can't I get the
entire picture?

Please retain the context of the part of the message you are replying to.

Have a think about what happens to BytesIndex each time something is
read.

--
Ian Collins.

You guys have really been helpful. I have altered my code to be as
seen below. It works fine for text documents and jpegs. But, it
won't work for Gifs or PNGs. It only displays part of the picture.
Any ideas why?

while( !file.eof() ) {
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
file.read(buffer, 1024);
int BytesLeft = file.gcount();
while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
}
}
What is gcount? If you read a full block of data, it would be 1024 bytes.
How does gcount reflect this? Does gcount reflect, somehow, how much data
you actually read?
Feb 22 '07 #9
Jim Langston wrote:
"iwasinnihon" <iw*********@hotmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
>>On Feb 21, 9:23 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>>iwasinnihon wrote:

Thank you for your help thus far. I have changed it from strlen() to
gcount(). Now I am getting part of the picture. Why can't I get the
entire picture?

Please retain the context of the part of the message you are replying to.

Have a think about what happens to BytesIndex each time something is
read.

--
Ian Collins.

You guys have really been helpful. I have altered my code to be as
seen below. It works fine for text documents and jpegs. But, it
won't work for Gifs or PNGs. It only displays part of the picture.
Any ideas why?

while( !file.eof() ) {
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
file.read(buffer, 1024);
int BytesLeft = file.gcount();
while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
}
}


What is gcount? If you read a full block of data, it would be 1024 bytes.
How does gcount reflect this? Does gcount reflect, somehow, how much data
you actually read?

Of course gcount reflects how many bytes are actually read. Are you
trying to say it doesn't?

I can't see any problem with the code. I think the OP needs to use a
debugger to find out what is actually going wrong.

john
Feb 22 '07 #10
"John Harrison" <jo*************@hotmail.comwrote in message
news:w7*******************@newsfe3-win.ntli.net...
Jim Langston wrote:
>"iwasinnihon" <iw*********@hotmail.comwrote in message
news:11**********************@t69g2000cwt.googleg roups.com...
>>>On Feb 21, 9:23 pm, Ian Collins <ian-n...@hotmail.comwrote:

iwasinnihon wrote:

>Thank you for your help thus far. I have changed it from strlen() to
>gcount(). Now I am getting part of the picture. Why can't I get the
>entire picture?

Please retain the context of the part of the message you are replying
to.

Have a think about what happens to BytesIndex each time something is
read.

--
Ian Collins.

You guys have really been helpful. I have altered my code to be as
seen below. It works fine for text documents and jpegs. But, it
won't work for Gifs or PNGs. It only displays part of the picture.
Any ideas why?

while( !file.eof() ) {
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
file.read(buffer, 1024);
int BytesLeft = file.gcount();
while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
}
}


What is gcount? If you read a full block of data, it would be 1024
bytes. How does gcount reflect this? Does gcount reflect, somehow, how
much data you actually read?

Of course gcount reflects how many bytes are actually read. Are you trying
to say it doesn't?

I can't see any problem with the code. I think the OP needs to use a
debugger to find out what is actually going wrong.
No, I was asking if it does, because I didn't know. If it does, in fact,
reflect the number of bytes read by file.read then the program should
perform as expected.
Feb 22 '07 #11
On Feb 21, 8:59 pm, "iwasinnihon" <iwasinni...@hotmail.comwrote:
On Feb 21, 9:23 pm, Ian Collins <ian-n...@hotmail.comwrote:
iwasinnihon wrote:
Thank you for your help thus far. I have changed it from strlen() to
gcount(). Now I am getting part of the picture. Why can't I get the
entire picture?
Please retain the context of the part of the message you are replying to.
Have a think about what happens to BytesIndex each time something is read.
--
Ian Collins.

You guys have really been helpful. I have altered my code to be as
seen below. It works fine for text documents and jpegs. But, it
won't work for Gifs or PNGs. It only displays part of the picture.
Any ideas why?

while( !file.eof() ) {
char buffer[1024] = "";
int BytesSent = 0;
int BytesIndex = 0;
file.read(buffer, 1024);
int BytesLeft = file.gcount();
while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
}

}- Hide quoted text -

- Show quoted text -
This code should work, but I would definitely clean it up some:
* You don't need to initialize a char array you'll be using for
binary data
* Good to move the re-initialization of BytesIndex into your (!
eof) loop; bad to declare all your variables in that loop
* Find a better name ("BytesToSend"?) than BytesLeft, since this
is very ambiguous

I don't know the insides of graphics files, but if you're sending to a
different type of machine, and the files use 16 or 32-bit values, you
may want to consider byte ordering on the other end, which may not
match what you have in your file.

Dana

Feb 22 '07 #12

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

Similar topics

6
by: Paul Fi | last post by:
i have little knowledge of socket programming, but what i know is that using sockets we exchange text between endpoints of the socket but first we convert the text to array of bytes and we send it...
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...
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...
3
by: Willy Stevens | last post by:
Hello, In my application I have to read sometimes quite big chunk of binary data. I have a buffer which default size is 32000 bytes. But how could I read binary data that exceeds 32000 bytes?...
4
by: gvijayaratnam | last post by:
Hi All, I have a fn prototype as follows void print ( char *Buffer, usigned long bufferSize, int chunkSize ); This fn will accept binary file buffer and the buffer size and the chunk size,...
1
by: keksy | last post by:
Hi every1, I am writing a small client/server application and in it I want to send an image asynchronous from the client to the server through a TCP socket. I found an example code on the MSDN...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.