send a binary message to a tcp server in ruby

To send a binary message to a TCP server in Ruby, you can use the send method of a TCPSocket object. Here's an example:

main.rb
require 'socket'

# connect to server
socket = TCPSocket.new('example.com', 1234)

# send binary message
message = "\x00\x01\x02\x03" # your binary message as a string
socket.send(message, 0)

# close connection
socket.close
225 chars
12 lines

In this example, we connect to a server at example.com on port 1234. We then define our binary message as a string, using hexadecimal escape sequences (e.g. \x00 for the byte with value 0). Finally, we send the message using the socket's send method, with the 0 flag indicating that no special behavior is required.

Note that the server should be expecting a binary message and should know how to parse it.

related categories

gistlibby LogSnag