 |
|
 |
|
|
 |
Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 12:23 AM
Hi,
I'm new to unix programming and am trying to create a file transfer
application. The server reads binary data and sends it over to the
client when file download is requested. My problem is that the data
gets through to the client but when I take a look at the file it comes
across garbled. Eg. See the *.java file below:
<REP GET BEGIN>
< OP61Prec3
Diriteclnterr plen\365\270^B^Y^\\377\377\377\273FDSGR~
\376\376\377\201^A^A\377\377\377<REP GET BEGIN>
< OP61Prec3
=============================
On server side:
I'm using an integer array and fread() to read the data from the file
and send() to send the data to client.
On client side:
Again I'm using recv() and integer array to receive the data from the
server and fwrite() to write to a file.
And the file on client side shows up garbled as I showed above.
Please help.
Thanks.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 12:23 AM
ashish.lohra@gmail.com wrote:
> I'm new to unix programming and am trying to create a file transfer
> application. The server reads binary data and sends it over to the
> client when file download is requested. My problem is that the data
> gets through to the client but when I take a look at the file it comes
> across garbled. Eg. See the *.java file below:
> <REP GET BEGIN>
> < OP61Prec3
> Diriteclnterr plen\365\270^B^Y^\\377\377\377\273FDSGR~
> \376\376\377\201^A^A\377\377\377<REP GET BEGIN>
> < OP61Prec3
> =============================
> On server side:
> I'm using an integer array and fread() to read the data from the file
> and send() to send the data to client.
> On client side:
> Again I'm using recv() and integer array to receive the data from the
> server and fwrite() to write to a file.
> And the file on client side shows up garbled as I showed above.
Not much information to go by - posting the code itself instead of
some rough description might be better. First question: why do you
use an integer array instead of an (insigned char) array? That seems
to be strange. And are you sure that you use the return value of
recv() correctly? It's the number of bytes that you've received,
not the number of integers.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 12:23 AM
On Feb 7, 4:25 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> ashish.lo...@gmail.com wrote:
>
> Not much information to go by - posting the code itself instead of
> some rough description might be better. First question: why do you
> use an integer array instead of an (insigned char) array? That seems
> to be strange. And are you sure that you use the return value of
> recv() correctly? It's the number of bytes that you've received,
> not the number of integers.
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ j...@toerring.de
> \__________________________ http://toerring.de- Hide quoted text -
>
> - Show quoted text -
I earlier was using char array to send over the data and the
application was only able to send .txt, .java .c etc. files. When I
tried sending over jpg or binary or compressed files then it wouldn't
work.
The recv() is working correctly as it returns the number of bytes.
My basic problem is I want to transfer all types of files.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 12:23 AM
ashish.lohra@gmail.com writes:
> I earlier was using char array to send over the data and the
> application was only able to send .txt, .java .c etc. files. When I
> tried sending over jpg or binary or compressed files then it wouldn't
> work.
>
> The recv() is working correctly as it returns the number of bytes.
In C, char is an integer type, so it makes no difference.
Well, if you used a wider integer type, you might get bytesex errors,
but you'd still see the ASCII characters on the other side, only in a
different order.
You could try to use tcpdump to see what's transmitted "on the line".
tcpdump -X -s 4096 port $YOUR_PORT
(or use a bigger size if you use bigger packets).
--
__Pascal Bourguignon__ http://www.informatimago.com/
WARNING: This product warps space and time in its vicinity.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 12:23 AM
Ok I switched back to unsigned char array and again I'm being able to
send *.txt,*.java,*.c etc files ok
For files like *.jpg *.jar etc the data is being sent over but for
some reason it is not being able to send the whole file. Also, when I
open the file I received it look like having content like:
\377\377\377\273FDSGR~
\376\376\377\201^A^A\377\377\377
Why is that happening? Please note that I'm using vi editor.
Any clues why it stops before sending over the whole file?
> In C, char is an integer type, so it makes no difference.
>
> Well, if you used a wider integer type, you might get bytesex errors,
> but you'd still see the ASCII characters on the other side, only in a
> different order.
>
> You could try to use tcpdump to see what's transmitted "on the line".
>
> tcpdump -X -s 4096 port $YOUR_PORT
>
> (or use a bigger size if you use bigger packets).
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> WARNING: This product warps space and time in its vicinity.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 12:23 AM
ashish.lohra@gmail.com writes:
> Ok I switched back to unsigned char array and again I'm being able to
> send *.txt,*.java,*.c etc files ok
>
> For files like *.jpg *.jar etc the data is being sent over but for
> some reason it is not being able to send the whole file. Also, when I
> open the file I received it look like having content like:
> \377\377\377\273FDSGR~
> \376\376\377\201^A^A\377\377\377
> Why is that happening? Please note that I'm using vi editor.
>
> Any clues why it stops before sending over the whole file?
No, you're not giving us any clue.
Are you using TCP or UDP?
--
__Pascal Bourguignon__ http://www.informatimago.com/
ATTENTION: Despite any other listing of product contents found
herein, the consumer is advised that, in actuality, this product
consists of 99.9999999999% empty space.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 12:23 AM
I'm using TCP. I tired to use the tcpdump command but I don't have
appropriate permissions for that.
Also, I tried transmitting *.gif file. It is total 3580 bytes. Out of
whihc server is only ebing able to send 2898 and the client receives
1642 bytes.
So I'm trying to see why the whole thing is not getting transmitted
from the server side.
I use recv() and send() to receive and send data and unsigned char
array.
Please advise if possible.
On Feb 7, 6:47 pm, Pascal Bourguignon <p...@informatimago.com> wrote:
> ashish.lo...@gmail.com writes:
>
>
>
> No, you're not giving us any clue.
>
> Are you using TCP or UDP?
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> ATTENTION: Despite any other listing of product contents found
> herein, the consumer is advised that, in actuality, this product
> consists of 99.9999999999% empty space.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 12:23 AM
ashish.lohra@gmail.com writes:
> I'm using TCP. I tired to use the tcpdump command but I don't have
> appropriate permissions for that.
It might be a good idea to debug your programs on a system where you
have root access. A solution could be user-mode-linux or Qemu.
> Also, I tried transmitting *.gif file. It is total 3580 bytes. Out of
> whihc server is only ebing able to send 2898 and the client receives
> 1642 bytes.
What do you mean by "the server is only able to send 2898 bytes"?
> So I'm trying to see why the whole thing is not getting transmitted
> from the server side.
>
> I use recv() and send() to receive and send data and unsigned char
> array.
>
> Please advise if possible.
I'd read again man 2 recv and man 2 send
in particular, the RETURN VALUE and the ERRORS sections.
You could make use of perror(3).
--
__Pascal Bourguignon__ http://www.informatimago.com/
ATTENTION: Despite any other listing of product contents found
herein, the consumer is advised that, in actuality, this product
consists of 99.9999999999% empty space.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 06:29 AM
ashish.lohra@gmail.com wrote:
> I'm using TCP. I tired to use the tcpdump command but I don't have
> appropriate permissions for that.
>
> Also, I tried transmitting *.gif file. It is total 3580 bytes. Out of
> whihc server is only ebing able to send 2898 and the client receives
> 1642 bytes.
>
> So I'm trying to see why the whole thing is not getting transmitted
> from the server side.
>
> I use recv() and send() to receive and send data and unsigned char
> array.
In regards to your integer issue:
man ntohl
man ntohs
man htonl
man htons
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Trouble sending binary data over Sockets (using C) |
 |
 |
|
|
02-08-07 12:21 PM
Hello,
> I'm using TCP. I tired to use the tcpdump command but I don't have
> appropriate permissions for that.
>
> Also, I tried transmitting *.gif file. It is total 3580 bytes. Out of
> whihc server is only ebing able to send 2898 and the client receives
> 1642 bytes.
>
> So I'm trying to see why the whole thing is not getting transmitted
> from the server side.
>
> I use recv() and send() to receive and send data and unsigned char
> array.
>
> Please advise if possible.
Is the platform architecture where your client and your server run
exactly the same? I mean, same endianess, etc?
You may need to use XDR or roll you own data exchange protocol using
the ntoh* / hton* functions as mentioned by another poster.
HTH,
Loic.
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 06:06 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
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
|
 |
|
 |
|