-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomNetworking2.java
More file actions
64 lines (47 loc) · 1.8 KB
/
CustomNetworking2.java
File metadata and controls
64 lines (47 loc) · 1.8 KB
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
56
57
58
59
60
61
62
63
64
import java.io.*;
import java.net.*;
import java.util.Scanner;
class CustomNetworking2{
public static void main(String[] args){
Thread t=new Thread(new QuoteServerRunnable());
t.start();
}
public static class QuoteServerRunnable implements Runnable{
@Override
public void run(){
BufferedReader in=null;
try{
in=new BufferedReader(new FileReader("quotes.txt"));
}catch(Exception e){
}
while(true){
try{
DatagramSocket socket=new DatagramSocket(4243);
byte[] buf=new byte[256];
DatagramPacket packet=new DatagramPacket(buf, buf.length);
String quote;
quote=in.readLine();
if(quote==null){
System.out.println("End of file");
in.close();
in=new BufferedReader(new FileReader("quotes.txt"));
socket.close();
continue;
}
socket.receive(packet);
buf=quote.getBytes();
InetAddress address=packet.getAddress();
int port=packet.getPort();
packet=new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
System.out.println("Data sent to port number - "+port);
socket.close();
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}