KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echoserver > TestEchoServerReadLine


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package echoserver;
16
17 import java.io.*;
18 import java.net.*;
19
20 /**
21  * EchoServer - readLine() testing under non-blocking mode.
22  * @author Akshathkumar Shetty
23  */

24 public class TestEchoServerReadLine {
25     private Socket socket;
26     private BufferedReader in;
27     private PrintWriter out;
28     
29     public boolean testEchoServer(String JavaDoc host, int port, String JavaDoc newLine) {
30         try {
31             socket = new Socket(host, port);
32             in = new BufferedReader(
33                     new InputStreamReader(socket.getInputStream()));
34             out = new PrintWriter(
35                     new BufferedWriter(new OutputStreamWriter(
36                     socket.getOutputStream())));
37             
38             //banner
39
for(int i=0;i<5;i++)
40                 in.readLine();
41             
42             //username
43
in.readLine();
44             out.print("user");
45             out.print(newLine);
46             out.flush();
47             
48             //password
49
in.readLine();
50             out.print("user1");
51             out.flush();
52             Thread.sleep(2000);
53             out.print(newLine);
54             out.flush();
55             
56             //Auth Failed
57
in.readLine();
58             
59             //username
60
in.readLine();
61             out.print("user");
62             out.print(newLine);
63             out.flush();
64             
65             //password
66
in.readLine();
67             out.print("user");
68             Thread.sleep(200);
69             out.print(newLine);
70             out.flush();
71             
72             //ok
73
in.readLine();
74             
75             out.print("test string");
76             out.flush();
77             Thread.sleep(6000);
78             out.print(newLine);
79             out.flush();
80             String JavaDoc data = in.readLine();
81             if(data.equals("Echo : test string")==false)
82                 System.err.println("Not equals!");
83             
84             out.print("test string1");
85             out.print(newLine);
86             out.print("test string2");
87             out.print(newLine);
88             out.flush();
89             data = in.readLine();
90             if(data.equals("Echo : test string1")==false)
91                 System.err.println("Not equals=1!");
92              data = in.readLine();
93             if(data.equals("Echo : test string2")==false)
94                 System.err.println("Not equals=2!");
95              
96             //quit
97
out.print("quit");
98             out.print(newLine);
99             out.flush();
100             data = in.readLine();
101             if(data.equals("Bye ;-)")==false)
102                 System.err.println("bye not got!");
103             
104              return true;
105         } catch(Exception JavaDoc e) {
106             System.err.println("Socket failed : "+e);
107             return false;
108         } finally {
109             try {
110                 if(socket!=null) socket.close();
111             } catch(IOException e) {
112                 System.err.println("Socket not closed: "+e);
113             }
114         }
115     }
116     
117     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
118         TestEchoServerReadLine main = new TestEchoServerReadLine();
119
120         int port = 4123;
121         String JavaDoc host = "localhost";
122
123         if(args.length > 0) {
124             try {
125                 port = Integer.parseInt(args[0]);
126             } catch(NumberFormatException JavaDoc nfe) {}
127         }
128         
129         if(args.length>=2) {
130             host = args[1];
131         }
132
133         System.out.print("\\r\\n Test: ");
134         System.out.println(main.testEchoServer(host, port, "\r\n"));
135         Thread.sleep(30000);
136
137         System.out.print("\\n Test: ");
138         System.out.println(main.testEchoServer(host, port, "\n"));
139
140         Thread.sleep(30000);
141         System.out.println("\\r Test: ");
142         System.out.println(main.testEchoServer(host, port, "\r"));
143     }
144     
145 }
146
Popular Tags