accepting binary chars in socket methods

Hey experts,

 When you use File IO in C, you should open the file in binary format (ios::binary) in order to read/write in binary format, i.e. for example process the null character as a normal char., and not as a terminating character. However, when I am passing data via sockets using send & recv functions, the null char. is being taken as a terminating char., and I can't seem to know what kind of initialization I should do for the socket to allow it to process binary characters...any help??

P.S.: I am using Windows Platform
LVL 3
mte01Asked:
Who is Participating?
 
brettmjohnsonCommented:
Use read() and write() instead of send() and recv().
0
 
mte01Author Commented:
>>brettmjohnson

When I used write (with the socket descriptor, data to be written, and length of data as parameters), it returned -1 (the write was not successfull); while when I was sending data using send, I was able to send the data before the null char...any idea why??
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi mte01,

After the write() fails, check the value of ERRNO.


Good Luck!
Kent
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
NopiusCommented:
Try this, before read/write:

setmode(fd, O_BINARY);

where fd is an open socket descriptor.
0
 
mte01Author Commented:
Well it turns out that that socket descriptor (returned by the socket) function cannot be manipulated like a file descriptor (it will give bad file no. when trying to use read, write, or setmode)
0
 
NopiusCommented:
It can, I guess. Please paste your code snippet here.
0
 
brettmjohnsonCommented:
>  Well it turns out that that socket descriptor (returned by the socket) function cannot be manipulated
> like a file descriptor (it will give bad file no. when trying to use read, write, or setmode)

Do you have a broken OS?
0
 
mte01Author Commented:
>>brettmjohnson

What do you mean by broken OS?

Guys,

I was able to do the transfer, by incrementing the asciis of the chars by one at the server, and then decrementing them by one at the client, and it worked :)...thanks for your help anyways!!
0
 
brettmjohnsonCommented:
> What do you mean by broken OS?

I mean an OS with a crappy sockets implementation, like Windows.
[Windows also suffers from a broken device driver design, so I can
imagine that socket vs file descriptor usage is not transparent.]
0
 
NopiusCommented:
brettmjohnson, I checked setmode() in MinGW+Windows on sockets, it was working. But there where problems with char-by-char read, only int-by-int.
0
 
NopiusCommented:
I mean int-by-int was the only method to read binary streams :-)
0
 
mte01Author Commented:
>> brettmjohnson, Nopius

Ahaa I see...thx for your lookup!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.