Skip to content

Commit 7751aa2

Browse files
committed
Multicast Datagram Quotes threads
1 parent ce9d63f commit 7751aa2

6 files changed

+166
-0
lines changed

MulticastDatagram/Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
javac ./*.java
2+
3+
clean:
4+
rm ./*.class
5+
6+
run:
7+
java MulticastServer &
8+
java MulticastClient localhost
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
import java.io.*;
3+
import java.net.*;
4+
5+
public class MulticastClient {
6+
public static void main(String[] args) throws IOException {
7+
8+
//create a MulticastSocket
9+
MulticastSocket multiSocket = new MulticastSocket(4446);
10+
InetAddress address = InetAddress.getByName("230.0.0.1");
11+
multiSocket.joinGroup(address);
12+
13+
DatagramPacket packet;
14+
15+
for(int i = 0; i < 5; i++){
16+
byte[] buf = new byte[256];
17+
packet = new DatagramPacket(buf, buf.length);
18+
multiSocket.receive(packet);
19+
20+
String received = new String(packet.getData(),0,packet.getLength());
21+
System.out.println("Cita del momento: " + received);
22+
}
23+
multiSocket.leaveGroup(address);
24+
multiSocket.close();
25+
}
26+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
import java.io.*;
3+
4+
public class MulticastServer {
5+
public static void main(String[] args) throws IOException {
6+
new MulticastServerThread().start();
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
import java.net.*;
3+
import java.util.Date;
4+
import java.io.*;
5+
6+
public class MulticastServerThread extends QuoteServerThread {
7+
8+
private long FIVE_SECONDS = 5000;
9+
10+
public MulticastServerThread() throws IOException{
11+
super("MulticastServerThread");
12+
}
13+
14+
public void run(){
15+
while(moreQuotes){
16+
try {
17+
byte[] buf = new byte[256];
18+
19+
//resolver y enviar respuesta
20+
String dString = null;
21+
if(in == null){
22+
dString = new Date().toString();
23+
} else{
24+
dString = getNextQuote();
25+
}
26+
27+
buf = dString.getBytes();
28+
29+
//debemos enviar la respuesta a los cliente
30+
// se debe saber dirección del cliente y puerto desde donde llegó
31+
// la request
32+
InetAddress groupAddress = InetAddress.getByName("230.0.0.1");
33+
34+
DatagramPacket packet = new DatagramPacket(buf, buf.length,groupAddress,4446);
35+
socket.send(packet);
36+
try {
37+
sleep((long)(Math.random() * FIVE_SECONDS));
38+
} catch (InterruptedException e) {
39+
e.printStackTrace();
40+
}
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
moreQuotes = false;
44+
}
45+
}
46+
}
47+
48+
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
import java.net.*;
3+
import java.util.Date;
4+
import java.io.*;
5+
6+
public class QuoteServerThread extends Thread {
7+
protected DatagramSocket socket = null;
8+
protected BufferedReader in = null;
9+
protected Boolean moreQuotes = true;
10+
11+
public QuoteServerThread() throws IOException{
12+
this("QuoteServerThread");
13+
}
14+
15+
public QuoteServerThread(String name) throws IOException {
16+
super(name);
17+
socket = new DatagramSocket(4445);
18+
19+
try {
20+
in = new BufferedReader(new FileReader("one-liniers.txt"));
21+
} catch (FileNotFoundException e) {
22+
System.err.println("No podemos abrir el archivo. Server time en cambio.");
23+
}
24+
}
25+
26+
public void run(){
27+
while(moreQuotes){
28+
try {
29+
byte[] buf = new byte[256];
30+
//recibir datagrama
31+
DatagramPacket packet = new DatagramPacket(buf, buf.length);
32+
socket.receive(packet);
33+
34+
//resolver y enviar respuesta
35+
String dString = null;
36+
if(in == null){
37+
dString = new Date().toString();
38+
} else{
39+
dString = getNextQuote();
40+
}
41+
42+
buf = dString.getBytes();
43+
44+
//debemos enviar la respuesta al cliente
45+
// se debe saber dirección del cliente y puerto desde donde llegó
46+
// la request
47+
InetAddress iAddress = packet.getAddress();
48+
int port = packet.getPort();
49+
packet = new DatagramPacket(buf, buf.length,iAddress,port);
50+
socket.send(packet);
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
moreQuotes = false;
54+
}
55+
}
56+
}
57+
58+
protected String getNextQuote(){
59+
String returnValue = null;
60+
try {
61+
if((returnValue = in.readLine()) == null){
62+
in.close();
63+
moreQuotes = false;
64+
returnValue = "No more Quotes, Good Bye";
65+
}
66+
} catch (IOException e)
67+
{
68+
returnValue = "IOException en obtener nueva cita en servidor";
69+
}
70+
return returnValue;
71+
}
72+
73+
}

MulticastDatagram/one-liniers.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Buena compare'
2+
single quote element
3+
jaulas

0 commit comments

Comments
 (0)