-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDP1.java
More file actions
25 lines (22 loc) · 764 Bytes
/
UDP1.java
File metadata and controls
25 lines (22 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.net.*;
//Works alongside UDP2.java which acts as a server
class UDP1{
public static void main(String[] args) throws Exception{
DatagramSocket socket=new DatagramSocket();
System.out.println("datagramsocket is connected to port no" + socket.getPort());
socket.setSoTimeout(30000);
InetAddress host=InetAddress.getLocalHost();
//the packet to be sent
byte[] data="Hello world".getBytes();
DatagramPacket request=new DatagramPacket(data, data.length, host, 4242);
try{
socket.send(request);
}catch(Exception e){
e.printStackTrace();
}finally{
if(socket!=null){
socket.close();
}
}
}
}