Reputation: 21444
I have implemented, for study, an Echo server with UDP and datagrams but I have a terminology doubt.
If to send a DatagramPacket
I must connect to a server listen on a port where to
send it through a socket why some books says that UDP is a connectionless protocol?
The connection is needed or what don't I understand?
Thanks.
Upvotes: 0
Views: 1422
Reputation: 533880
UDP is a connectionless protocol. You don't need to/cant connect to use UDP. However if you have a requirement to form a connection I suggest you use TCP.
EDIT: DatagramSocket has a connect method, however all it does is
Connects the socket to a remote address for this socket. When a socket is connected to a remote address, packets may only be sent to or received from that address. By default a datagram socket is not connected.
It does not establish a connection as such.
Upvotes: 1
Reputation: 508
In this case, the "connection-less" refers to the fact that UDP does not do handshaking to set up its connection. Furthermore, there is no acknowledgement of receipt of packets that are sent unless the server protocol has been designed to send them.
While DatagramSocket
has a connect method, the API states:
"If the remote destination to which the socket is connected does not exist, or is otherwise unreachable, and if an ICMP destination unreachable packet has been received for that address, then a subsequent call to send or receive may throw a PortUnreachableException. Note, there is no guarantee that the exception will be thrown."
Therefore, it can be possible for you to send data to an address and have no indication there is not actually a connection.
Upvotes: 2
Reputation: 2251
Connectionless means that the receiving server does not store the state of the connection and that you don't get a response from the receiving server (acknowledge or error).
However, the communication is as always via ports/sockets, you need a local port and a remote port.
Upvotes: 1