Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * SerialTest.java | ||
3 | */ | ||
4 | package lab.scd.net.serializare; | ||
5 | |||
6 | /** | ||
7 | * Class created by @author Mihai HULEA at Feb 23, 2005. | ||
8 | * | ||
9 | * This class is part of the laborator2_sockettest project. | ||
10 | * | ||
11 | * 1. In cadrul clasei Persoana adaugati cuvantul cheie transient in fata declaratiei | ||
12 | * variabile String nume;. Observati modificarile care apar in executia programului. | ||
13 | * Care este rolul cuvantului cheie transient. | ||
14 | * | ||
15 | * 2. Modificati aplicatia astfel incat dupa receptionarea obiectului serverul sa scrie | ||
16 | * obiectul receptionat pe disc intr-un fisier. | ||
17 | * | ||
18 | */ | ||
19 | import java.io.*; | ||
20 | import java.net.*; | ||
21 | |||
22 | public class SerialTest extends Thread{ | ||
23 | |||
24 | public void run(){ | ||
25 | |||
26 | try{ | ||
27 | ServerSocket ss = new ServerSocket(1977); | ||
28 | Socket s = ss.accept(); | ||
29 | ObjectInputStream ois = new ObjectInputStream(s.getInputStream()); | ||
30 | Pers p = (Pers)ois.readObject(); | ||
31 | System.out.println("Serverul a receptionat obiectul:"+p); | ||
32 | s.close(); | ||
33 | ss.close(); | ||
34 | }catch(Exception e){e.printStackTrace();} | ||
35 | |||
36 | } | ||
37 | |||
38 | public static void main(String[] args) throws Exception{ | ||
39 | |||
40 | //starteaza serverul | ||
41 | (new SerialTest()).start(); | ||
42 | |||
43 | //conectare la server | ||
44 | Socket s = new Socket(InetAddress.getByName("localhost"),1977); | ||
45 | |||
46 | //construieste fluxul de iesire | ||
47 | ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); | ||
48 | Pers p = new Pers("Alin",14); | ||
49 | //trimite un obiect prin fluxul de iesire | ||
50 | System.out.println("Clientul trimite obiectul: "+p); | ||
51 | oos.writeObject(p); | ||
52 | //inchide conexiunea/ | ||
53 | s.close(); | ||
54 | |||
55 | } | ||
56 | } | ||
57 | |||
58 | class Pers implements Serializable{ | ||
59 | String nume; | ||
60 | //transient String nume; | ||
61 | int varsta; | ||
62 | |||
63 | Pers(String n, int v){ | ||
64 | nume = n; varsta = v; | ||
65 | } | ||
66 | |||
67 | public String toString(){ | ||
68 | return "Persoana: "+nume+" vasrta: "+varsta; | ||
69 | } | ||
70 | } |