Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * ServerMultifir.java | ||
3 | */ | ||
4 | |||
5 | /** | ||
6 | * Class created by @author Mihai HULEA at Feb 23, 2005. | ||
7 | * | ||
8 | * This class is part of the laborator2_sockettest project. | ||
9 | * | ||
10 | */ | ||
11 | package lab.scd.net.socket; | ||
12 | import java.io.*; | ||
13 | import java.net.*; | ||
14 | |||
15 | public class ServerMultifir | ||
16 | { | ||
17 | public static final int PORT = 1900; | ||
18 | void startServer() | ||
19 | { | ||
20 | ServerSocket ss=null; | ||
21 | try | ||
22 | { | ||
23 | |||
24 | ss = new ServerSocket(PORT); | ||
25 | while (true) | ||
26 | { | ||
27 | //astept conexiuni | ||
28 | Socket socket = ss.accept(); | ||
29 | //un client s-a conectat...starteaza firul de tratare a clientului | ||
30 | new TratareClient(socket); | ||
31 | } | ||
32 | |||
33 | }catch(IOException ex) | ||
34 | { | ||
35 | System.err.println("Eroare :"+ex.getMessage()); | ||
36 | } | ||
37 | finally | ||
38 | { | ||
39 | try{ss.close();}catch(IOException ex2){} | ||
40 | } | ||
41 | } | ||
42 | |||
43 | public static void main(String args[]) | ||
44 | { | ||
45 | ServerMultifir smf = new ServerMultifir(); | ||
46 | smf.startServer(); | ||
47 | } | ||
48 | |||
49 | |||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Class created by @author Mihai HULEA at Mar 13, 2005. | ||
54 | * | ||
55 | * This class is part of the laborator2_net project. | ||
56 | * | ||
57 | * Se foloseste cate un fir de executie pentru tratarea fiecarui client. | ||
58 | */ | ||
59 | class TratareClient extends Thread | ||
60 | { | ||
61 | private Socket socket; | ||
62 | private BufferedReader in; | ||
63 | private PrintWriter out; | ||
64 | |||
65 | /** | ||
66 | * Initializeaza firul de executie | ||
67 | * @param socket | ||
68 | * @throws IOException | ||
69 | */ | ||
70 | TratareClient(Socket socket)throws IOException | ||
71 | { | ||
72 | this.socket = socket; | ||
73 | |||
74 | //init flux de intrare prin care se vor citi datele de la client | ||
75 | in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
76 | |||
77 | //inti flux de iesire prin care datele vor fi trimise catre client | ||
78 | out = new PrintWriter( | ||
79 | new BufferedWriter( | ||
80 | new OutputStreamWriter( socket.getOutputStream()))); | ||
81 | |||
82 | //sunt folosite doua fluxuri orientate pa caracter | ||
83 | |||
84 | } | ||
85 | |||
86 | public void run() | ||
87 | { | ||
88 | try { | ||
89 | |||
90 | while (true) | ||
91 | { | ||
92 | //asteapta receptionarea unui mesaj de la client | ||
93 | String str = in.readLine(); | ||
94 | |||
95 | //daca mesajul este egal cu "END" opreste firul ce trateaza clientul | ||
96 | if (str.equals("END")) break; | ||
97 | //afiseaza mesajul pe ecran | ||
98 | System.out.println("Echoing: " + str); | ||
99 | |||
100 | //trimte catre client mesajul receptionat, adaugand in fata sirul "echo" | ||
101 | out.println("echo "+str); | ||
102 | out.flush(); | ||
103 | |||
104 | }//.while | ||
105 | System.out.println("closing..."); | ||
106 | |||
107 | } | ||
108 | catch(IOException e) {System.err.println("IO Exception");} | ||
109 | finally { | ||
110 | try { | ||
111 | socket.close(); | ||
112 | }catch(IOException e) {System.err.println("Socket not closed");} | ||
113 | } | ||
114 | }//.run | ||
115 | } |