-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.java
47 lines (40 loc) · 1.39 KB
/
send.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;
public class send{
//max udp packet size 65527
//In practical safest packet size satisfying MTU size is approx 1400
public static final int BUFFER_SIZE=1400;
public static DatagramPacket packet(String s,int port) throws Exception{
byte[] bs=s.getBytes();
InetAddress ia = InetAddress.getLocalHost();
DatagramPacket dp = new DatagramPacket(bs,bs.length,ia, port);
return dp;
}
public static void main(String[] args) throws IOException,SocketException{
DatagramSocket ds = new DatagramSocket();
int port = 9989;
Scanner sc=null;
System.out.println("Sending to port 9989");
System.out.println("Enter the info to send ");
System.out.println("Send 'terminate' to stop the communication");
try {
sc = new Scanner(System.in);
while(true){
System.out.printf(">> ");
String input= sc.nextLine();
ds.send(packet(input, port));
if(input.equalsIgnoreCase("terminate")){
break;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}finally{
sc.close();
ds.close();
}
System.out.println("Done transmitting");
}
}