By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Socket Send Binary (Jpeg)

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.