Problem sending binary file via socket.
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Problem sending binary file via socket.




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Problem sending binary file via socket.  
Hon Seng Phuah


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-03-04 02:23 AM

Hi all,

My client code are:

send(fd, argv[2], strlen(argv[2]) + 1, 0);
fp = fopen(argv[2], "rb");
while (bytes_read = fread(buf, sizeof(char), MAX_DATA_SIZE, fp) > 0)
{
bytes_read = send(fd, buf, strlen(buf), 0);
}
fclose(fp);

my server code:

if (bytes_read = recv(fd2, buf, MAX_DATA_SIZE, 0) == -1)
{
/* calls recv() */
printf("1.recv() error\n");
exit(-1);
}

fp = fopen(buf, "wb");
bytes_read = recv(fd2, buf, MAX_DATA_SIZE, 0);
printf("%d", bytes_read);
while (bytes_read != 0)
{
fwrite(buf, 1, bytes_read, fp);
bytes_read = recv(fd2, buf, MAX_DATA_SIZE, 0) == -1;
}

When client sends the buffer to server, the server receives the data
correctly.
When client sends the file data to server, the server fails to receive
any data. The printf statement byte_read is zero.

What wrong is my code? Any idea? Thanks in advance.





[ Post a follow-up to this message ]



    Re: Problem sending binary file via socket.  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-03-04 02:23 AM

Hon Seng Phuah <hsphuah@usa.com> wrote:
> My client code are:

> send(fd, argv[2], strlen(argv[2]) + 1, 0);
> fp = fopen(argv[2], "rb");
> while (bytes_read = fread(buf, sizeof(char), MAX_DATA_SIZE, fp) > 0)
> {
>    bytes_read = send(fd, buf, strlen(buf), 0);

Point 1: the name 'bytes_read' seems rather misleading, shouldn't
that be 'bytes_send'?
Point 2: How is 'buf' supposed to be a '\0' character terminated
string? Without that you can't use strlen(). Especially if the
data are binary data a '\0' character has quite a good chance to
be embedded in the data, which strlen will then take to be the
end of the "string" - it might even be the very first byte in the
file, resulting in 0 bytes getting send. Why don't you just use
the value that fread() returned as the number of bytes you're
going to send?

> }
> fclose(fp);

> my server code:

> if (bytes_read = recv(fd2, buf, MAX_DATA_SIZE, 0) == -1)
> {
>       /* calls recv() */
>       printf("1.recv() error\n");
>       exit(-1);
> }

> fp = fopen(buf, "wb");
> bytes_read = recv(fd2, buf, MAX_DATA_SIZE, 0);
> printf("%d", bytes_read);
> while (bytes_read != 0)

recv() can also return -1, indicting an error (or just a signal)...

> {
>     fwrite(buf, 1, bytes_read, fp);
>     bytes_read = recv(fd2, buf, MAX_DATA_SIZE, 0) == -1;
> }

Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Re: Problem sending binary file via socket.  
Hon Seng Phuah


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-04-04 08:22 AM

Jens.Toerring@physik.fu-berlin.de wrote in message news:<2kkp4kF3fdhuU1@uni-berlin.de>...[vb
col=seagreen]
> Hon Seng Phuah <hsphuah@usa.com> wrote: 
> 
>
> Point 1: the name 'bytes_read' seems rather misleading, shouldn't
> that be 'bytes_send'?
> Point 2: How is 'buf' supposed to be a '\0' character terminated
> string? Without that you can't use strlen(). Especially if the
> data are binary data a '\0' character has quite a good chance to
> be embedded in the data, which strlen will then take to be the
> end of the "string" - it might even be the very first byte in the
> file, resulting in 0 bytes getting send. Why don't you just use
> the value that fread() returned as the number of bytes you're
> going to send?
> 
> 
> 
> 
>
> recv() can also return -1, indicting an error (or just a signal)...
> 
>
>                                Regards, Jens[/vbcol]


Thanks for pointing out my problem.

First, you are right. It should use bytes_sent.

Second, when I read my binary file, JPEG formatted photo, my program
only read one byte for fread.

When I change this code,

bytes_read = send(fd, buf, strlen(buf), 0);

to

bytes_sent = send(fd, buf, bytes_read, 0);

The bytes_sent is only 1.

For server portion, I was referring to the second print statement. It
displays zero. My program does not cause any error when I receive
bytes from the client program. I really do not know where my error is.
If anyone knows my problem, please let me know. I am running above
programs on HP-UX os in case you want to about it. Thanks.





[ Post a follow-up to this message ]



    Re: Problem sending binary file via socket.  
Hon Seng Phuah


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-04-04 08:22 AM

Jens.Toerring@physik.fu-berlin.de wrote in message news:<2kkp4kF3fdhuU1@uni-berlin.de>...[vb
col=seagreen]
> Hon Seng Phuah <hsphuah@usa.com> wrote: 
> 
>
> Point 1: the name 'bytes_read' seems rather misleading, shouldn't
> that be 'bytes_send'?
> Point 2: How is 'buf' supposed to be a '\0' character terminated
> string? Without that you can't use strlen(). Especially if the
> data are binary data a '\0' character has quite a good chance to
> be embedded in the data, which strlen will then take to be the
> end of the "string" - it might even be the very first byte in the
> file, resulting in 0 bytes getting send. Why don't you just use
> the value that fread() returned as the number of bytes you're
> going to send?
> 
> 
> 
> 
>
> recv() can also return -1, indicting an error (or just a signal)...
> 
>
>                                Regards, Jens[/vbcol]


Thanks for pointing out my problem.

First, you are right. It should use bytes_sent.

Second, when I read my binary file, JPEG formatted photo, my program
only read one byte for fread statement.

When I change this code,

bytes_read = send(fd, buf, strlen(buf), 0);

to

bytes_sent = send(fd, buf, bytes_read, 0);

The bytes_sent is only 1.

For server portion, I was referring to the second print statement. It
displays zero which it does not receive any data from the client. My
program does not cause any error when it recv statement is called from
the server program. I really do not know where my error is. If anyone
knows my problem, please let me know. I am running above programs on
HP-UX os in case you want to about it. Thanks.





[ Post a follow-up to this message ]



    Re: Problem sending binary file via socket.  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-04-04 01:15 PM

Hon Seng Phuah <hsphuah@usa.com> wrote:
> Second, when I read my binary file, JPEG formatted photo, my program
> only read one byte for fread.
 

No, it doesn't. I hadn't spotted this the first time. But you're
missing a pair of parenthesis here, you need

while ( ( bytes_read = fread(buf, sizeof(char), MAX_DATA_SIZE, fp) ) > 0)

otherwise you assign to 'bytes_read' the result of the comparison between
the result of fread() and 0 - which is always 1 unless less than a single
byte got read.
[vbcol=seagreen]
> For server portion, I was referring to the second print statement. It
> displays zero. My program does not cause any error when I receive
> bytes from the client program. I really do not know where my error is.
> If anyone knows my problem, please let me know. I am running above
> programs on HP-UX os in case you want to about it. Thanks.

Since you show only a small part of your server code (actually, that
looks more like a client...) it's basically impossible to figure out
what is going wrong. How do you set up the connection between both
the client and the server?
[vbcol=seagreen] 

Here you store the comparison between the number of bytes read and -1
in 'bytes_read', looks a bit fishy, especially when you later use
this value to figure out if you need to continue to read ('bytes_read'
will be not equal 0 when recv() returned an error, and that's just the
case were it doesn't make sense to try to read further data).

Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:51 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register