|
Home > Archive > Unix Programming > July 2004 > Problem sending binary file via socket.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem sending binary file via socket.
|
|
| Hon Seng Phuah 2004-07-02, 9:23 pm |
| 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.
| |
| Jens.Toerring@physik.fu-berlin.de 2004-07-02, 9:23 pm |
| 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
| |
| Hon Seng Phuah 2004-07-04, 3:22 am |
| Jens.Toerring@physik.fu-berlin.de wrote in message news:<2kkp4kF3fdhuU1@uni-berlin.de>...
> 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
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.
| |
| Hon Seng Phuah 2004-07-04, 3:22 am |
| Jens.Toerring@physik.fu-berlin.de wrote in message news:<2kkp4kF3fdhuU1@uni-berlin.de>...
> 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
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.
| |
| Jens.Toerring@physik.fu-berlin.de 2004-07-04, 8:15 am |
| 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
|
|
|
|
|