-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDP_S.java
54 lines (32 loc) · 905 Bytes
/
UDP_S.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
import java.io.*;
import java.net.*;
class UDP_S
{
public static void main(String args[])
{
BufferedReader xx=new BufferedReader(new InputStreamReader(System.in));
DatagramSocket socket;
String outMessage,inMessage;
int port=7777;
try
{
socket=new DatagramSocket(port);
System.out.println("Listening to port : "+port);
while(true)
{
DatagramPacket packet=new DatagramPacket(new byte[100],100);
socket.receive(packet);
inMessage=new String(packet.getData());
InetAddress clientIP=packet.getAddress();
System.out.println("The Client "+packet.getSocketAddress()+"says :"+ inMessage);
System.out.println("Enter a message to send :");
outMessage=xx.readLine();
socket.send(new DatagramPacket(outMessage.getBytes(),outMessage.length(),clientIP,packet.getPort()));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}