Biblioteca Java - Blame information for rev 3

Subversion Repositories:
Rev:
Rev Author Line No. Line
3 mihai 1 /*
2  * MulticastClient.java
3  */
4 package lab.scd.net.broadcast;
5  
6 import java.io.BufferedReader;
7 import java.io.InputStreamReader;
8 import java.net.*;
9  
10 /**
11  * Class created by @author Mihai HULEA at Feb 26, 2005.
12  *
13  * This class is part of the laborator2_net project.
14  *
15  * 1. Modificati aplicatia client astfel incat adresa IP a serverului sa fie citita
16  * ca si argument la lansarea in executie a programului.
17  *
18  */
19 public class MulticastClient extends Thread{
20  
21     boolean alive=true;
22     int port;
23  
24  
25  
26     /**
27      * @param port
28      */
29     public MulticastClient(int port) {
30         this.port = port;
31     }
32  
33     public void run(){
34         try{
35  
36         MulticastSocket socket = new MulticastSocket(port);
37         //pregateste aplicatia client pentru a putea receptiona mesaje multicast
38         InetAddress group = InetAddress.getByName("230.0.0.1");
39         socket.joinGroup(group);
40  
41         DatagramPacket packet;
42         while(alive){
43                 byte[] buf = new byte[256];
44             packet = new DatagramPacket(buf, buf.length);
45             //asteapta receptionarea de pachete
46             socket.receive(packet);
47  
48             //extrage continultul mesajlui din pachet
49             String received = new String(packet.getData());
50  
51             //afiseaza mesajul
52             System.out.println("rcv: " + received);
53         }
54         socket.leaveGroup(group);
55         socket.close();
56         }catch(Exception e){
57             System.err.println("Error on client : "+e.getMessage());
58             e.printStackTrace();
59         }
60     }
61  
62     public void sendMessage(String msg, String serverIP) throws Exception{
63         DatagramSocket socket = new DatagramSocket();
64         byte[] buf = new byte[256];
65         InetAddress address = InetAddress.getByName(serverIP);
66         DatagramPacket packet = new DatagramPacket(buf, buf.length,
67                                                    address, port);
68  
69         packet.setData(msg.getBytes());
70  
71         socket.send(packet);
72     }
73  
74  
75  
76     public static void main(String[] args) throws Exception{
77  
78  
79  
80         //start listening for brodcasted messages
81         MulticastClient mc = new MulticastClient(4446);
82         mc.start();
83  
84  
85         //intr-o bucla while citeste de la tastatura linii de text
86         String msg="";
87         while(!msg.equals("exit")){
88  
89                 BufferedReader sb = new BufferedReader(new InputStreamReader(System.in));
90                 System.out.print("snd:");
91                 //citeste o linie de la tastatura
92                 msg = sb.readLine();
93                 try{
94                  //trimte mesajul catre serverul multicast  
95                  mc.sendMessage(msg,"127.0.0.1");
96                 }catch(Exception e){
97                     System.err.println("Error sending message:"+e.getMessage());
98                 }
99             }
100         System.exit(0);
101       }
102 }