Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | package lab.scd.net.chatsimple; | ||
6 | |||
7 | import java.net.*; | ||
8 | import java.util.ArrayList; | ||
9 | import java.util.Iterator; | ||
10 | import java.io.*; | ||
11 | |||
12 | public class CServer extends Thread{ | ||
13 | ArrayList clients = new ArrayList(); | ||
14 | |||
15 | CServer(){ | ||
16 | System.out.println("Starting!"); | ||
17 | } | ||
18 | |||
19 | @Override | ||
20 | public void run() { | ||
21 | ServerSocket ss; | ||
22 | try { | ||
23 | ss = new ServerSocket(1999); | ||
24 | |||
25 | while(true){ | ||
26 | Socket s = ss.accept(); | ||
27 | System.out.println("Client nou conectat!"); | ||
28 | clients.add(new TratareClient(s)); | ||
29 | } | ||
30 | } catch (IOException e) { | ||
31 | e.printStackTrace(); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | public void sendToAll(String msg){ | ||
36 | for (Iterator iterator = clients.iterator(); iterator.hasNext();) { | ||
37 | TratareClient t = (TratareClient) iterator.next(); | ||
38 | t.sendMessage(msg); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | |||
43 | |||
44 | class TratareClient extends Thread{ | ||
45 | BufferedReader in; | ||
46 | PrintWriter out; | ||
47 | |||
48 | TratareClient(Socket s) throws IOException{ | ||
49 | out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true); | ||
50 | in = new BufferedReader(new InputStreamReader(s.getInputStream())); | ||
51 | start(); | ||
52 | } | ||
53 | |||
54 | public void sendMessage(String msg) { | ||
55 | out.println(msg); | ||
56 | } | ||
57 | |||
58 | @Override | ||
59 | public void run() { | ||
60 | try{ | ||
61 | while(true){ | ||
62 | String msg = in.readLine(); | ||
63 | sendToAll(msg); | ||
64 | } | ||
65 | } | ||
66 | catch(Exception e){e.printStackTrace();} | ||
67 | } | ||
68 | } | ||
69 | |||
70 | |||
71 | public static void main(String[] args) { | ||
72 | CServer s = new CServer(); | ||
73 | s.start(); | ||
74 | } | ||
75 | |||
76 | } | ||
77 |