473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to send an int over a socket

Hi,

I have what seems to be a simple problem. But I can not for the life of me
find a way to send an integer over a socket. The send method will only accept
strings. Here is what I am trying to do:

testmessage = 'test message'
msglen = len(testmessage)
sock.send(msglen)
sock.send(testmessage)

Now, I know I can do this:

testmessage = 'test message'
sock.send('\xC') # send hard coded length of testmessage in hex in string
sock.send(testmessage)

However, in my actual program I will not know the length of testmessage in
advance. So how do I convert msglen into a suitable format for the send
method?

Thanks,
Tom
Jul 18 '05 #1
2 19525
Tom Brown <br***@esteem.com> writes:
However, in my actual program I will not know the length of testmessage in
advance. So how do I convert msglen into a suitable format for the send
method?

str(123)

'123'

You might also look at

http://cr.yp.to/proto/netstrings.txt

which describes some issues you should pay attention to, and suggests
a standardized format.
Jul 18 '05 #2
Use string formatting :

msglen = '%16s' %len(testmessage)

will return a 16-byte string, beginning with spaces. Send it over your
connection and use int() to get the message length

Pierre
Jul 18 '05 #3

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

Similar topics

0
by: Gregory Hassett | last post by:
Hello, I want to periodically send a TCP packet to a peer, always from the same source port. That is, each packet will come from my local ip address, port 8801, and go to the peer's ip address,...
13
by: Manfred Braun | last post by:
Hi All, I am trying to understand the blocking method socket.Send(). The call blocks as expected, but does this mean, it returnes after the underlying TCP layer got a confirmation, that the send...
1
by: Hasan O | last post by:
hi , I have a problem with socket.send mesajlar = Encoding.ASCII.GetBytes("UGIR"); socketResultValue = socket.Send(mesajlar,0,mesajlar.Length, SocketFlags.None); socketResultValue =...
1
by: Hasan O | last post by:
hi , I have a problem with socket.send mesajlar = Encoding.ASCII.GetBytes("UGIR"); socketResultValue = socket.Send(mesajlar,0,mesajlar.Length, SocketFlags.None); socketResultValue =...
1
by: Laura T. | last post by:
Hi, I've this kind a program, using sockets to communicate with the clients. One of the clients can be a web browser (like IE). When using IE as a client, the transmission blocks completely, and...
11
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running...
2
by: yvan | last post by:
Hi, Here is my client/server scenario: Step1: Client connects to server and sends data sucessfully (using Socket.Send()). Step2: Server gracefully exists (calls Socket.Shutdown() and...
3
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...
0
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...
1
by: danfolkes | last post by:
Hey Everyone, I am trying to send repeated messages from a "Node" to a "Server". It works the first time I send the from the Node to Server, but after that it either errors, or does not do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.