-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP Network
43 lines (39 loc) · 1.22 KB
/
UDP Network
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
import java.net.*; public class dataReceiver { public static void main(String[] args) { try
{
DatagramSocket ds=new DatagramSocket(3003);
DatagramPacket dp; String msg; while(true)
{ byte[] receive= new byte[65535]; dp=new DatagramPacket(receive, receive.length); ds.receive(dp); msg= new String(dp.getData(), 0, dp.getLength()); if(msg.equals("quit"))
{
System.out.println("Session Terinated"); break;
}
System.out.println("New message received : ");
System.out.println(msg);
receive=null;
}
ds.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}
b) Client:
import java.net.*; import java.util.Scanner; public class dataTransmitter { public static void main(String[] args) { try
{
Scanner s=new Scanner(System.in); DatagramSocket ds=new DatagramSocket();
InetAddress ip=InetAddress.getLocalHost(); byte buf[]=null;
System.out.println("Enter your messages, type 'quit' to end the se ssion : "); while(true)
{
String msg=s.nextLine(); buf=msg.getBytes();
DatagramPacket dp=new DatagramPacket(buf,buf.length,ip,3003); ds.send(dp);
if(msg.equals("quit"))
{
System.out.println("Session Terminated"); break;
} }
s.close(); ds.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}