-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatagramClient.java
55 lines (36 loc) · 1.26 KB
/
DatagramClient.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
48
49
50
51
52
53
54
55
package datagramclient;
import java.net.*;
public class DatagramClient
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(2001);
String msg="Hello World";
DatagramPacket dp=new DatagramPacket(msg.getBytes(),msg.length(),InetAddress.getByName("localhost"),2000);
ds.send(dp);
byte b[]=new byte[1024];
dp=new DatagramPacket(b,1024);
ds.receive(dp);
msg=new String(dp.getData()).trim();
System.out.println("From Server "+msg);
ds.close();
}
}
class Server
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(2000);
byte b[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(b,1024);
ds.receive(dp);
String msg=new String(dp.getData()).trim();
System.out.println("From Client "+msg);
StringBuilder sb=new StringBuilder(msg);
sb.reverse();
msg=sb.toString();
dp=new DatagramPacket(msg.getBytes(),msg.length(),InetAddress.getByName("localhost"),2001);
ds.send(dp);
ds.close();
}
}