UDP Socket and ruby - ruby

I am having trouble to bind to a server address. I have a connection, to a server (using Savon XML Library). Now I just need to listen to that server and gather its HTTP packets that sends. The server each time sends:
http://200.34.12.168/Videos/1/frame/0
http://200.34.12.168/Videos/1/frame/1
http://200.34.12.168/Videos/1/frame/2
http://200.34.12.168/Videos/1/frame/3
http://200.34.12.168/Videos/1/frame/4
...
..
which are HTTP packets. I am trying to create a UDP server that listens to these. This is what I have so far:
s = UDPSocket.new
s.bind('200.34.12.168', 80)
5.times do
text, sender = s.recvfrom(16)
puts text
end
it fails at the bind function. How can I listen to a UDP connection with ruby?
The Error I get:
"`bind': Can't assign requested address - bind(2) (Errno::EADDRNOTAVAIL)"

Do you have a web browser running - it may already be bound to port 80..

Related

Ruby - UDP - How to spoof IP and Port Number

I've been messing about with some sample client and server code for sending and receiving UDP packets, here's what I'm doing for the send
require 'socket'
s = UDPSocket.new
s.send("hello", 0, 'localhost', 1234)
Is it possible to somehow use a fake IP address and port number when sending? haven't come across anything online with any examples of how to do so.
You need to bind the socket before calling send:
require 'socket'
s = UDPSocket.new
s.bind('128.100.8.6', 1253)
s.send(...)
Keep in mind that your upstream is in no way responsible for delivering "spoofed" packets and that many providers will dump these if they're not originating in their network.

Ruby: How to verify TCP connection alive

Server:
s = TCPServer.open(6000)
loop do
Thread.start(s.accept) do |client|
# Keep receive and handle message from client
...
end
end
Clients:
server = TCPSocket.open(server_ip, 6000)
... # Send message if event, will keep TCP connection
Question:
Sometimes network down or client crash, How does sever know the TCP connection is alive? Is there a method or command the verify the connection?
Thanks
The most reliable way to verify the state of a TCP connection is to send an empty packet to the server and check if you get a response or an error. That will give you the current connection state of the socket.

Receive OSC Message on sending port

I have an OSC server which returns data upon receiving a request message to the port used to send said request. Is there a way to find the port which was used to send the message (assigned by the os) and then open a listen channel on that port?
Before sending your message from the client, just bind it. For an automatically assigned port, use 0 as the port number. Example:
require 'socket'
u = UDPSocket.new
u.bind('0.0.0.0', 0)
Now you can receive from the same port you send from.

ruby cannot assign requested address

I had already created a qt program that listens on a specific port on my server. And it works fine. Now I want to create a simple ruby program to do the same. Right now I just have a simple test server using netcat which establishes a network socket and accepts UDP data (this is Ubuntu server by the way):
$ sudo nc -l 1720
Now I am just trying to listen on the port in Ruby:
# network.rb
require 'socket'
socket = UDPSocket.new
socket.bind('64.xxx.xx.xxx', 1720)
This right away raises this exception:
network.rb:4:in `bind': Cannot assign requested address - bind(2) (Errno::EADDRNOTAVAIL)
WHy is it saying the address is not available? All that is there is a netcat socket. The goal is that I will have UDP data coming in from GPRS devices to that port, and then I will have ruby sitting on my ubuntu server listening for that data, then decoding it, and storing it into a postgresql database.
You are making a server or client?
you used nc, so i guess you are making client.
server is bind
client is connect:
c = UDPSocket.new
c.connect("127.0.0.1", 1111)
"address is not available" usually as the port is used.
or you can ping the address fisrt to check if the address can be reached
I was getting the same error by running:
rails s -b 10.0.0.61
It turns out that my local IP wasn't 10.0.0.61 and this was causing the error.

Unable to receive response from the SNMP port

I am using SNMP to access the remote system data. According to the requirement I am encoding the SNMP request data to OAMPDU packet format and sending to remote system. The remote system receives the OAMPDU packet, decodes it and sends the snmp request to snmp agent through UDP socket which is bound to port 161. But I am unable to receive the response from snmp agent. I have created a UDP socket which is bound to 161 port to receive the response.
If I use any other free port number other than 161 for receiving SNMP agent does not send responses to that port.
Can anyone please suggest me how to overcome this problem?
Can we configure the different ports for tx,rx ???
How do we know on which port does snmp sends the response ???
Each UDP packet has a source port and a destination port. An SNMP manager sends out an SNMP request using any source port, and destination port 161. The agent will reply to the source port on the manager. For example:
Manager Agent
source port: <random number>
dest port: 161
content: what is your sysUpTime
source port: 161
destination port: <same random number>
content: sysUpTime is 42 seconds
Replies arrive on random number port, not port 161. So a manager listening on port 161 is unlikely to receive many replies. Instead of listening on port 161, listen on the same socket you used to send out the request. That socket will remember the source port it chose for sending.
P.S. When you use SNMP to query, SNMP's manager is UDP's client, and SNMP's agent is UDP's server.

Resources