KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > test > core > remote > other > TestSocketServer


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.test.core.remote.other;
25
26 import org.objectweb.jalisto.se.exception.JalistoException;
27
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.ObjectOutputStream JavaDoc;
31 import java.net.ServerSocket JavaDoc;
32 import java.net.Socket JavaDoc;
33 import java.net.SocketException JavaDoc;
34 import java.util.Random JavaDoc;
35
36 public class TestSocketServer implements Runnable JavaDoc {
37
38     public TestSocketServer(int port) {
39         random = new Random JavaDoc();
40         try {
41             this.ss = new ServerSocket JavaDoc(port);
42         } catch (IOException JavaDoc ioe) {
43             throw new JalistoException(ioe);
44         }
45     }
46
47     public void run() {
48         System.out.println("Run server");
49         while (true) {
50             String JavaDoc socketInfos = null;
51             try {
52                 socketInfos = connect();
53                 try {
54                     while (true) {
55                         Object JavaDoc message = readObject();
56                         System.out.println("\nread : " + message.toString() + "\n");
57                         sendReply();
58                     }
59                 } catch (IOException JavaDoc ioe) {
60                     if (ioe instanceof SocketException JavaDoc) {
61                         throw ioe;
62                     } else {
63                         System.out.println(ioe.getMessage());
64                     }
65                 } catch (ClassNotFoundException JavaDoc cnfe) {
66                     cnfe.printStackTrace();
67                     return;
68                 }
69             } catch (SocketException JavaDoc se) {
70                 if (se.getMessage().equals("Connection reset")) {
71                     System.out.println("socket " + socketInfos + " closed");
72                 } else {
73                     throw new RuntimeException JavaDoc(se);
74                 }
75             } catch (IOException JavaDoc ioe) {
76                 ioe.printStackTrace();
77                 return;
78             }
79         }
80     }
81
82     public void sendReply() throws IOException JavaDoc {
83         int delay = random.nextInt(900) + 100;
84         try {
85             Thread.sleep(delay);
86         } catch (InterruptedException JavaDoc e) {
87         }
88         toClient.writeObject("ok");
89         toClient.flush();
90     }
91
92     public String JavaDoc connect() throws IOException JavaDoc {
93         socket = ss.accept();
94         System.out.println("Connect with " + socket.toString());
95         fromClient = new ObjectInputStream JavaDoc(socket.getInputStream());
96         toClient = new ObjectOutputStream JavaDoc(socket.getOutputStream());
97         System.out.println("socket connected : " + socket.toString());
98         return socket.toString();
99     }
100
101     public Object JavaDoc readObject() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
102         Object JavaDoc message = null;
103         while (message == null) {
104             message = fromClient.readObject();
105         }
106         return message;
107     }
108
109
110     private ServerSocket JavaDoc ss;
111     private Socket JavaDoc socket;
112     private ObjectInputStream JavaDoc fromClient;
113     private ObjectOutputStream JavaDoc toClient;
114
115     private Random JavaDoc random;
116
117     public static void main(String JavaDoc[] args) {
118         TestSocketServer server = new TestSocketServer(6000);
119         server.run();
120     }
121 }
122
Popular Tags