You are creating Socket
, which is a TCP/IP Stream and creates statefull connection between you (client) and given IP
, PORT
.
I assume, this program stop while creating first Socket
.
If you look at the API of Socket
class:
* If UDP socket is used, TCP/IP related socket options will not apply.
*
* @param host the IP address.
* @param port the port number.
* @param stream if {@code true}, create a stream socket;
* otherwise, create a datagram socket.
@Deprecated
public Socket(InetAddress host, int port, boolean stream) throws IOException {
this(host != null ? new InetSocketAddress(host, port) : null,
new InetSocketAddress(0), stream);
}
This is @Deprecated
version of creating UDP Socket, but what you REALLY need is DatagramPacket
from java.net
, which creates stateless connection, and may be timed out if packet didn't come to the end.
Look here for some example code of writing DatagramPacket
:
https://github.com/awadalaa/Socket-Programming-Java/blob/master/UDP-Pinger/PingClient.java
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…