LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2007, 01:48 PM   #1
Barca
LQ Newbie
 
Registered: Nov 2004
Location: Poland
Distribution: Slackware - best and only ;]
Posts: 29

Rep: Reputation: 15
How to send 00 (aka "binary zero") in C?


Hi I send data using socket like this:

Code:
    strcpy(send, "\x2f\xcd\xd3\xbe"); 
    tmp_file = fdopen(socket, "w"); 
    fprintf(file, send); 
    fclose(file);
And it works fine -> it sends data specified in hex and Wireshark shows in hex: 2fcdd3be.
But I need to send "00". I tried many ways: Code:

Code:
"\x2f\xcd\xd3\xbe\x000" 
"\x2f\xcd\xd3\xbe\x00" 
"\x2f\xcd\xd3\xbe\x0" 
"\x2f\xcd\xd3\xbe\0"

etc.

Nothing works. I want that Wireshark show: 2fcdd3be00 but every time it stops after be and doesn't show any more data. Thanks in advance.
 
Old 02-08-2007, 02:00 PM   #2
anon276
LQ Newbie
 
Registered: Feb 2007
Posts: 11

Rep: Reputation: Disabled
Can you check the string for a certain value before you read it into where it needs to go? If you can, try a strcmp(<string>, "00\0"); for instance.
 
Old 02-08-2007, 02:04 PM   #3
Barca
LQ Newbie
 
Registered: Nov 2004
Location: Poland
Distribution: Slackware - best and only ;]
Posts: 29

Original Poster
Rep: Reputation: 15
Ha I just solved that problem. I used fwrite():

Code:
    unsigned char test[] = "\xbe\x6c\xcd\x2f\x00\xf2";
    file = fdopen(socket, "w");
    fwrite(test, 1, 6, file);
    fflush(file);
    fclose(file);
Thank you for your post
 
Old 02-08-2007, 02:12 PM   #4
KenJennings
LQ Newbie
 
Registered: Mar 2005
Location: FL, USA
Distribution: SuSE
Posts: 28

Rep: Reputation: 15
Quote:
Originally Posted by Barca
Hi I send data using socket like this: ...

Nothing works. I want that Wireshark show: 2fcdd3be00 but every time it stops after be and doesn't show any more data. Thanks in advance.
It doesn't work they way you expect , because you're sending data using the higher level stdio functions which recognize a 0 byte as the end of string flag which is then ignored/not sent.

If you must do it this way, you may have to write the 0 byte by itself, say with fputc() or something.

Ideally you should deal with a socket at a lower level and use the send() and recv() functions. Beej's Super Fantastic Guide to Networks page is the best explanation of socket programming: http://beej.us/guide/bgnet/
 
Old 02-09-2007, 05:19 PM   #5
Barca
LQ Newbie
 
Registered: Nov 2004
Location: Poland
Distribution: Slackware - best and only ;]
Posts: 29

Original Poster
Rep: Reputation: 15
I'm back :?
I still wants to easily sends hexadecimal data by socket. I already has this piece of code:

Code:
file = fdopen(socket, "w");
fwrite("\x15\x00", 1, 2, file);
char data_hex[] = "\x20\x30\x40\x50";
fwrite(data_hex, 1, 4, file);
fwrite("\x14\x24\xbc", 1, 3, file);
fclose(tmp_file);
Code works fine, I see in my Wireshark that it's send exactly as I want, except that data_hex variable. It's sent not as hexadecimal value but mhm "plain text".
And it's my exact question: How can I pass to fwrite() some variable and cause fwrite() to send it as hexadecimal? In other words:
fwrite("\x20\x30\x40\x50", 1, 4, file); -> WORKS
fwrite(data_hex, 1, 3, file); -> DOESN'T WORKS
data_hex will have different values so I can't pass it as "\x20\x30\x40\x50"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to send and receive "Net Send" messages in Linux? Avatar Linux - Networking 16 07-11-2010 09:42 PM
How to add Gnome style "drawer" (ie sub-menu) to KDE 3.1 kicker (aka KDE Panel) quaestor Linux - Software 7 10-17-2006 09:55 AM
Proposed solution for "status" (aka "problem solved") indicator demerson3 LQ Suggestions & Feedback 12 04-08-2006 02:15 PM
invalid binary "/usr/bin/postgres" while running postgresql ukrainet Linux - Newbie 1 12-01-2004 08:36 AM
Can "minimize all windows" aka <windows>-m be emulated in Fedora Linux? jdruin Linux - Software 4 04-19-2004 08:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration