Socket Send Binary (Jpeg)

I

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);
while(BytesLeft != 0){
BytesSent = send(sock, &buffer[BytesIndex], BytesLeft, 0);
BytesLeft -= BytesSent;
BytesIndex +=BytesSent;
cout << buffer << endl;
}
}
 
A

Alf P. Steinbach

* 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;
}
}
 
I

iwasinnihon

Am I missing something? What are you telling me?

* 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?
 
I

Ian Collins

iwasinnihon wrote:

Please don't top-post.
* 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.
 
J

Jim Langston

iwasinnihon said:
Am I missing something? What are you telling me?

* 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?
 
I

iwasinnihon

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?
 
I

Ian Collins

iwasinnihon said:
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.
 
I

iwasinnihon

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.

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;
}
}
 
J

Jim Langston

iwasinnihon said:
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.

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?
 
J

John Harrison

Jim said:
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.

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
 
J

Jim Langston

John Harrison said:
Jim said:
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.
 
D

Dana Good

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top