Send and receive UDP Messages simultaneously

Can UDP messages be sent and received in the same bash script using netcat?

In Linux I want to send a UDP message and receive its output in a variable for further processing. Ideally I want to do this in bash using netcat but I'm open to other possibilities (Java should be easy but I dont want to install additional components if I can avoid it)

So far I can issue the command echo -ne 'some data' | nc -u 9760 and see the incoming signals by setting up a listener nc -ul 9761 in another window. But the question is how can this be done in one process, and have the output assigned to a variable?

Could the listener process be sent to the background at the start of a script? if so how is this destroyed when the process finishes?


Yes, you can background the listener, something like this:

nc -ul 9761 >/tmp/foo &
echo -ne 'some data' | nc -u 9760
wait # waits for the backgrounded command to complete

You can write short scripts using Socket Programming. You need to create a Socket and add UDP properties on one Device and on the other device wait for incoming UDP message. Principally the messages are received as a string but you can save it to a string variable and do further modifications.

s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) //create socket 
sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slength) //sender
recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slength) //receiver

Ofcourse you need to assign some other properties to the socket parameters. Take a look at this Website. It has been Explained in simple words how you can program a client server scenario that communicate through udp over IP.

Need Your Help

How to modify type slice inside method?

go slice

How to modify type slice inside method? I tried

Referencing JSF Component in DOM

ajax jsf dom

What is the best way to make a component permanently available in the DOM? I currently have the following: