-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathudpServer.java
37 lines (29 loc) · 1.12 KB
/
udpServer.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
/*
Write a program on datagram sockets for client/server to display message on client side,typed on server side.
*/
import java.io.*;
import java.net.*;
//First run the server program(this) on one terminal and then run the client on another terminal
class udpServer
{
public static void main(String[] args) throws IOException
{
DatagramSocket ds=new DatagramSocket();
InetAddress ip = InetAddress.getByName("localhost");//specify the IP Address
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int port =2345;//define port number
String msg;
while(true)
{
//read every line
msg=br.readLine();
//create a Datagram packet with specifications-> string as bytes, length of the message,IP address, Port Number
DatagramPacket dp=new DatagramPacket(msg.getBytes(),msg.length(),ip,port);
//if the message is quit , terminate the program
if(!msg.equals("quit"))
ds.send(dp);
else
break;
}
}
}