How can i send a udp packet




















Ok, you are right, let me just make it explicit that this won't work with ZSH. This is way better than mucking with netcat. Thanks for the tip! This may be the most amazing thing I've ever seen. For some reason, I needed to use It's only a file name interpreted specially by bash. Show 10 more comments. Below is the bash script I did to send an udp packet to any host and port:!

On a current netcat v0. Simon Simon 1 1 gold badge 5 5 silver badges 10 10 bronze badges. You should use this command : echo -n "hello" nc -4u -w0 localhost Sajjad Baeidi Sajjad Baeidi 92 1 1 silver badge 2 2 bronze badges.

Error: invalid wait-time 0 — palik. I was fooling around with Wireshark when I realized this. Don't know if it helps. Evan Evan 81 1 1 silver badge 7 7 bronze badges. I think this is only correct for TCP. With UDP, it will send multiple lines in one packet if they fit. Sign up or log in Sign up using Google. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. TCPConn end-point. However, what is happening in this scenario is that, under the hood, sendto has effectively become an alias for send and, despite calling sendto , the data sent is actually being transported in a TCP packet and not a UDP packet at all.

TCPConn end-point while the latter did. The correct way to do this is for the client to open a TCP and a UDP socket to the server on the same port at the same time while the server, likewise, listens simultaneously for TCP and UDP "connections" the UDP connection is not a connection at all of course, it is simply an association with a local port number on the same port.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 3 years, 7 months ago. Active 3 years, 7 months ago. Viewed 4k times. Improve this question. Rob Rob 5 5 silver badges 18 18 bronze badges. There are two syscalls used for that, recvfrom and sendto , I don't know how Go implemented this but I'm pretty sure net. Read won't work for UDP at all. I know that UDP is connectionless but it is perfectly valid sockets to send a UDP packet down a connected socket; you just leave the destination address off the sendto and it figures out what you mean.

I know this because this is exactly what the other end of the link, coded in C, is doing in this case. Surely Golang has allowed for what is a quite normal sockets operation in its net implementation!? It is often useful for the remote IP address to default to the loopback address, particularly for services such as databases where there is a good chance of the client and server being run on the same machine. Alternatively, it may be preferable to require that the destination be specified explicitly.

For most purposes the best way to construct the remote address is by calling getaddrinfo. This takes a string containing either a hostname or an IP address, and a second string containing either a service name or a port number. The hints argument contains additional information to help guide the conversion. In this example:. The res argument is used to return a linked list of addrinfo structures containing the address or addresses that were found.

If multiple records are returned then the recommended behaviour from RFC is to try each address in turn, stopping when a successful outcome is achieved. This assumes that you have some way to distinguish success from failure, which may not always be the case, but if you are able to do this then you should.

If not then an acceptable alternative is to use the first result and discard the remainder. The memory occupied by the result list should be released by calling freeaddrinfo once it is no longer needed, however this cannot be done until after the datagram has been sent. The socket that will be used to send the datagram should be created using the socket function.

This takes three arguments:. It is equally acceptable for the protocol to be deduced in this manner or specified explicitly. Assuming you previously used getaddrinfo to construct the remote address then the required values can be obtained from the addrinfo structure:. Datagrams can be sent using any function that is capable of writing to a file descriptor, however unless you have connected the socket to a particular remote address as described below it is necessary to use either sendto or sendmsg so that a destination address can be specified.

Of these sendmsg is the more flexibile option, but at the cost of a signficiantly more complex interface. Details for each function are given below. Regardless of which function you choose, each function call will result in a separate datagram being sent. To call sendto you must supply the content of the datagram and the remote address to which it should be sent:.



0コメント

  • 1000 / 1000