Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * ServerSimplu.java | ||
3 | */ | ||
4 | |||
5 | /** | ||
6 | * Class created by @author Mihai HULEA at Feb 23, 2005. | ||
7 | * | ||
8 | * This class is part of the laborator2_serverclientmonofir project. | ||
9 | * | ||
10 | * 1. Modificati aplicatia sever astfel incat dupa tratarea unui client acesta sa revina | ||
11 | * in astepatare pentru a procesa noi cereri. | ||
12 | * | ||
13 | * 2. Modificati aplicatia server astefl incat aceasta sa accepte conexiuni sosite | ||
14 | * numai de pe anumite IP-uri. | ||
15 | */ | ||
16 | package lab.scd.net.socket; | ||
17 | import java.io.*; | ||
18 | import java.net.InetAddress; | ||
19 | import java.net.InetSocketAddress; | ||
20 | import java.net.ServerSocket; | ||
21 | import java.net.Socket; | ||
22 | import java.net.SocketAddress; | ||
23 | |||
24 | |||
25 | |||
26 | public class ServerSimplu { | ||
27 | public static void main(String[] args) throws IOException{ | ||
28 | |||
29 | ServerSocket ss=null; | ||
30 | Socket s=null; | ||
31 | |||
32 | try{ | ||
33 | String line=""; | ||
34 | ss = new ServerSocket(1900); //creaza obiectul serversocket | ||
35 | System.out.println("Serverul asteapta conexiuni..."); | ||
36 | s = ss.accept(); //incepe asteptarea de conexiuni pe portul 1900 | ||
37 | //in momentul in care un client s-a conectat ss.accept() returneaza | ||
38 | //un obiect de tip Socket care identifica conexiunea | ||
39 | |||
40 | //creaza fluxurile de intrare iesire | ||
41 | BufferedReader in = new BufferedReader( | ||
42 | new InputStreamReader(s.getInputStream())); | ||
43 | |||
44 | PrintWriter out = new PrintWriter( | ||
45 | new BufferedWriter(new OutputStreamWriter( | ||
46 | s.getOutputStream())),true); | ||
47 | |||
48 | //extrage adresa de ip si portul de pe care clientul s-a conectat | ||
49 | InetSocketAddress remoteadr = (InetSocketAddress)s.getRemoteSocketAddress(); | ||
50 | String remotehost = remoteadr.getHostName(); | ||
51 | int remoteport = remoteadr.getPort(); | ||
52 | |||
53 | System.out.println("Client nou conectat: "+remotehost+":"+remoteport); | ||
54 | |||
55 | while(!line.equals("END")){ | ||
56 | line = in.readLine(); //citeste datele de la client | ||
57 | System.out.println("Server a receptionat:"+line); | ||
58 | out.println("ECHO "+line); //trimite date la client | ||
59 | out.flush(); | ||
60 | } | ||
61 | |||
62 | System.out.println("Aplicatie server gata."); | ||
63 | |||
64 | }catch(Exception e){e.printStackTrace();} | ||
65 | finally{ | ||
66 | ss.close(); | ||
67 | if(s!=null) s.close(); | ||
68 | } | ||
69 | } | ||
70 | } |