Receive OSC Message on sending port - ruby

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.

Related

Getting FIN and RST flags when attempting TCP handshake in OS X

Trying to establish a tcp connection between a client / server. Both machines are Macs and are on the same LAN. Server's app listens on port 12345. After receiving "SYN, ACK" from Server, I send "ACK" but then my client automatically sends a "FIN, ACK" followed by "RST, ACK". So the TCP Flow ends up being:
Client sends SYN.
SVR sends SYN, ACK.
Client sends ACK.
Client sends FIN, ACK.
Client sends RST, ACK.
SVR sends ACK.
SVR sends ACK.
Client sends RST.
Client sends RST.
From reading other posts with similar issues it sounds like this could be happening because I'm trying to manually create the handshake on the user level and the Unix kernel (operating at the system level) sees the "SYN, ACK" far before anything on the user level can respond and moves to close the connection, seeing it as open for no reason. A similar problem to what this Linux user experienced: Unwanted RST TCP packet with Scapy
Whereas iptables worked for the Linux user should I use something like pf in Mac OS X to block / drop the FIN and RST msgs? My client is running 10.9.5 and my server 10.10.3.
Here's a flow graph of the tcp communication Server is 10.0.100.5 and client is 10.0.100.4:

UDP Socket and 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..

Windows UDP sending problems

I have a weird behaviour on windows. I have 2 processes that are talking to each other on UDP protocol.
Scenario: 1 of the proceeses is up and the other is not. The process try to send udp message towards the one that is down. The one that is up gets from OS or soemthing else a signal for the socket to read as it got message from the other process.
How come ?
It sends a signal on the same port, but it not a real message that was sent from the other application. When trying to read it u get an excpetion of number 10054, connection reset.

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.

Receiving datagrams using Udp connection

In order to receive datagrams through an UDP connection I have created an object of type UDPClient.
receivedNotificationSock = new UdpClient();
However once done and on using the receive method:
receivedHostNameBuffer=receivedNotificationSock.Receive(ref receivedNotificationIP);
I am getting an exception saying that I must call the bind method.
But there is no bind method in the UDPClient class.
Could You guys please provide me with the code if possible as to what should be done to overcome this exception.
You need I think to know some more about sockets.
All sockets possess a port number. First, you create a socket - which is almost useless on its own. It just floats there. But then you bind it - you assign it a port number. Now it's useful - now you can send and receive data on it.
Remember, all UDP communications are defined by the quad data set of the IP and port of the source and the IP and port of the destination. A freshly created socket doesn't have an IP address or port; binding gives it an IP address and port.
Unfortunately, I'm not a C# programmer, so I can't properly answer your question. But at least you know why it's important.
Pass the port number into the constructor of your UDP client.
receivedNotificationSock = new UdpClient(21000);
You may need to change firewall settings to allow the bind, though a popup window normally opens when you first run this on your dev machine.
For Socket proramming you need to know the sequence of syscalls you need to do on client side and on the server side.
If you are writting a client :
you open a socket with a socket call.
you then connect to the server port with a connect call
once connect is successful
then you send the request to the server using either a send or sendto or a write
which results in reception of data that you can read using a receive or read
On Server Side
you create a socket
bind it to a port
start listening on the socket for incoming connections from various clients using a listen.
There is a non blocking way of listening for connections as well with a select syscall.
Once the you establish a connection you can essentially read the request and start processing it.
Here's an example in C# that may be useful to you.
http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/

Resources