473,769 Members | 5,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sending binary data over sockets

Greetings, since there was no reponse to my previous post about an
existing FastCGI server in python, I've taken to writing my own. (which
of course I'll share--*if* there's something to share ;)

My problem now, is that I need to send certain binary data over a
socket. That is, I want to make some bytes, and stuff them in a TCP
packet, send them down the pipe, and then listen for a response.

socket.send, as best I can tell, will only send strings. I've read on
the list other conversations where the recommendation was to use xdrlib
or struct. But it appears that is only useful when you control the
client and the server. PLEASE correct me if I'm wrong, but using struct
just encodes the binary data as a string, right?

# Example sending binary data as a string
s = socket.socket()
s.connect(("127 .0.0.1", 8000))
packet = struct.pack('4B ', 1, 2, 3, 4)
s.send(packet)

In my understaing the above just sends the string '\x01\x02\x03\x 04',
not raw binary data. I've looked at Billy the Kid and pcap, which are
cool but not what I need.

Do I have to build my own packets from scratch and encode all the
TCP/IP headers myself to get this to work?

Solutions and Corrections welcome and appreciated.
Thanks very much

--
matthew

Jul 3 '06 #1
9 18050
th*****@gmail.c om schrieb:
Greetings, since there was no reponse to my previous post about an
existing FastCGI server in python, I've taken to writing my own. (which
of course I'll share--*if* there's something to share ;)

My problem now, is that I need to send certain binary data over a
socket. That is, I want to make some bytes, and stuff them in a TCP
packet, send them down the pipe, and then listen for a response.

socket.send, as best I can tell, will only send strings. I've read on
the list other conversations where the recommendation was to use xdrlib
or struct. But it appears that is only useful when you control the
client and the server. PLEASE correct me if I'm wrong, but using struct
just encodes the binary data as a string, right?
WHich is perfectly alright, as
Jul 3 '06 #2
Premature sending syndrome...

Diez B. Roggisch schrieb:
th*****@gmail.c om schrieb:
>Greetings, since there was no reponse to my previous post about an
existing FastCGI server in python, I've taken to writing my own. (which
of course I'll share--*if* there's something to share ;)

My problem now, is that I need to send certain binary data over a
socket. That is, I want to make some bytes, and stuff them in a TCP
packet, send them down the pipe, and then listen for a response.

socket.send, as best I can tell, will only send strings. I've read on
the list other conversations where the recommendation was to use xdrlib
or struct. But it appears that is only useful when you control the
client and the server. PLEASE correct me if I'm wrong, but using struct
just encodes the binary data as a string, right?
Python strings are binary data and can contain
- in oppostion to e.g. C-strings - null-bytes.

So it is perfectly alright to send "only" strings over a socket.

Diez
Jul 3 '06 #3
th*****@gmail.c om wrote:
Greetings, since there was no reponse to my previous post about an
existing FastCGI server in python, I've taken to writing my own. (which
of course I'll share--*if* there's something to share ;)

My problem now, is that I need to send certain binary data over a
socket. That is, I want to make some bytes, and stuff them in a TCP
packet, send them down the pipe, and then listen for a response.

socket.send, as best I can tell, will only send strings. I've read on
the list other conversations where the recommendation was to use xdrlib
or struct. But it appears that is only useful when you control the
client and the server. PLEASE correct me if I'm wrong, but using struct
just encodes the binary data as a string, right?

# Example sending binary data as a string
s = socket.socket()
s.connect(("127 .0.0.1", 8000))
packet = struct.pack('4B ', 1, 2, 3, 4)
s.send(packet)

In my understaing the above just sends the string '\x01\x02\x03\x 04',
not raw binary data. I've looked at Billy the Kid and pcap, which are
cool but not what I need.
It will send the 4 bytes, binary, and not the string as you assumed. If
you want to satisfy yourself, run tcpdump (or ethereal) to observe what
is being sent.

Do I have to build my own packets from scratch and encode all the
TCP/IP headers myself to get this to work?

Solutions and Corrections welcome and appreciated.
Thanks very much

--
matthew
Jul 3 '06 #4
On 2006-07-03, th*****@gmail.c om <th*****@gmail. comwrote:
My problem now, is that I need to send certain binary data over a
socket. That is, I want to make some bytes, and stuff them in a TCP
packet, send them down the pipe, and then listen for a response.

socket.send, as best I can tell, will only send strings. I've read on
the list other conversations where the recommendation was to use xdrlib
or struct. But it appears that is only useful when you control the
client and the server. PLEASE correct me if I'm wrong, but using struct
just encodes the binary data as a string, right?
Right. Strings are binary data.
# Example sending binary data as a string
s = socket.socket()
s.connect(("127 .0.0.1", 8000))
packet = struct.pack('4B ', 1, 2, 3, 4)
s.send(packet)

In my understaing the above just sends the string '\x01\x02\x03\x 04',
not raw binary data.
The string '\x01\x02\x03\x 04' consists of four octets having
the values 0x01, 0x02, 0x03, 0x04.

Are those not the four octets you wanted to send?

--
Grant Edwards grante Yow! I want EARS! I
at want two ROUND BLACK
visi.com EARS to make me feel warm
'n secure!!
Jul 3 '06 #5
Python strings are binary data and can contain
- in oppostion to e.g. C-strings - null-bytes.

So it is perfectly alright to send "only" strings over a socket.
Agreed. I wasn't trying to imply it was 'wrong' just that the receiving
application wouldn't interpret strings correctly.

Jul 3 '06 #6
It will send the 4 bytes, binary, and not the string as you assumed. If
you want to satisfy yourself, run tcpdump (or ethereal) to observe what
is being sent.
Thanks very much for the prompt reply. I'll take your word for it. I
actually tried ethereal to verify my hypothesis before posting, but
wasn't able to see the raw data bytes. That's probably just a
deficiancy in my ability to steer ethereal.

thanks again
--
matthew

Jul 3 '06 #7
Are those not the four octets you wanted to send?

Yes. My understanding of struct was broken. Sukanta corrected it for
me.

thank you
--
matthew

Jul 3 '06 #8
th*****@gmail.c om wrote:
Greetings, since there was no reponse to my previous post about an
existing FastCGI server in python, I've taken to writing my own. (which
of course I'll share--*if* there's something to share ;)

My problem now, is that I need to send certain binary data over a
socket. That is, I want to make some bytes, and stuff them in a TCP
packet, send them down the pipe, and then listen for a response.
As everyone else has already told you, you have a misconception about
strings and struct and so on. However, if what you want is mutability
in a pattern, read the array module documentation as well.

--Scott David Daniels
sc***********@a cm.org
Jul 3 '06 #9
Grant Edwards wrote:
On 2006-07-03, th*****@gmail.c om <th*****@gmail. comwrote:
My problem now, is that I need to send certain binary data over a
socket. That is, I want to make some bytes, and stuff them in a TCP
packet, send them down the pipe, and then listen for a response.
....
In my understaing the above just sends the string '\x01\x02\x03\x 04',
not raw binary data.

The string '\x01\x02\x03\x 04' consists of four octets having
the values 0x01, 0x02, 0x03, 0x04.

Are those not the four octets you wanted to send?

--
Grant Edwards
i.e.
>>len('\x01\x02 \x03\x04')
4

Jul 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6983
by: Tim Black | last post by:
My application requires sending a large piece (~2MB) of data to several devices on a network via TCP sockets. I have experimented with different methods for doing this and this has raised some questions about the implementation of Python sockets. (both methods use blocking sockets) Method 1: Calls socket.sendall(data) for each device in sequence, all in a single thread.
5
4579
by: Glenn Wilson | last post by:
I am writing a network system for project I am working on. How would I send a class or structure to the clients and recieve one back. Or how would I go about building a packet with a header and other information and then be able to break it apart when I recieve it at the other end. These packets can be of Varible length but would always have the same header. I want to build someting simular to the DirectPlay System of packets and...
4
2246
by: Robert McNally | last post by:
Hello, I'm currently learning c# and have been trying to write a simple program with sockets. The problem is i'm trying to send an email with an attachment, (which is the program itself) by using base64. When i run it it sends the email ok, but and attchment doesn't work i get an error when i run it. The attchment seems to be bigger (20k) then the orginal (16k) i've included the source which i have been using. Can someone tell me what...
4
5413
by: Abubakar | last post by:
Hi, I am writing a server in C# and client in C++ (pure, not managed). The communication goes on through sockets. In C# I am using NetworkStream to send text data (by converting it to byte array) to the c++ client. In c++ client I have "recv" (winsock) function through which I receive everything. Now there is a need to pass a "struct" of C# through the "write" function of network stream. I want to know how this can be done. Also I would...
7
2686
by: D. Patrick | last post by:
I need to duplicate the functionality of a java applet, and how it connects to a remote server. But, I don't have the protocol information or the java source code which was written years ago. So, I used a packet sniffer and saw the protocol (TCP), the port, IP address, etc. All is good. I tried 2 different versions of .NET code to duplicate the requests to the remote server. Again, I used the packet sniffer and my packets seemed...
9
4927
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim strReturnData As String = _ System.Text.Encoding.ASCII.GetString(receiveBytes)
3
4311
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3168
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
4
3741
by: David Hirschfield | last post by:
I have a pair of programs which trade python data back and forth by pickling up lists of objects on one side (using pickle.HIGHEST_PROTOCOL), and sending that data over a TCP socket connection to the receiver, who unpickles the data and uses it. So far this has been working fine, but I now need a way of separating multiple chunks of pickled binary data in the stream being sent back and forth. Questions:
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.