KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > socket > raw > RawClient


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.transport.socket.raw;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInputStream JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.net.Socket JavaDoc;
15
16 /**
17  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
18  */

19 public class RawClient
20 {
21    protected String JavaDoc address = "localhost";
22    protected int port = 6700;
23
24    public boolean enableTcpNoDelay = false;
25    public int timeout = 60000;
26
27    private Socket JavaDoc socket = null;
28
29    private ObjectOutputStream JavaDoc oos;
30    private ObjectInputStream JavaDoc objInputStream;
31
32    public void startClient()
33    {
34       while(true)
35       {
36 // try
37
// {
38
// Thread.sleep(1000);
39
// }
40
// catch(InterruptedException e)
41
// {
42
// e.printStackTrace();
43
// }
44

45          try
46          {
47             getSocket();
48
49             oos.writeObject("This is the request");
50
51             //oos.flush();
52

53             oos.reset();
54             // to make sure stream gets reset
55
// Stupid ObjectInputStream holds object graph
56
// can only be set by the client/server sending a TC_RESET
57
oos.writeObject(Boolean.TRUE);
58             oos.flush();
59             oos.reset();
60
61
62             Object JavaDoc obj = objInputStream.readObject();
63
64             objInputStream.readObject(); // for stupid ObjectInputStream reset
65

66             System.out.println("response: " + obj);
67
68          }
69          catch(IOException JavaDoc e)
70          {
71             e.printStackTrace();
72          }
73          catch(ClassNotFoundException JavaDoc e)
74          {
75             e.printStackTrace();
76          }
77       }
78    }
79
80    public void getSocket() throws IOException JavaDoc
81    {
82       if(socket == null)
83       {
84          try
85          {
86             socket = new Socket JavaDoc(address, port);
87             socket.setTcpNoDelay(enableTcpNoDelay);
88 // socket.setSoTimeout(timeout);
89

90             BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(socket.getOutputStream());
91 // out.flush();
92
BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(socket.getInputStream());
93
94             oos = new ObjectOutputStream JavaDoc(out);
95             objInputStream = new ObjectInputStream JavaDoc(in);
96
97          }
98          catch(IOException JavaDoc e)
99          {
100             e.printStackTrace();
101          }
102       }
103       else
104       {
105          oos.reset();
106          oos.writeByte(1);
107          oos.flush();
108          oos.reset();
109          objInputStream.readByte();
110 // objInputStream.reset();
111
}
112    }
113
114    public static void main(String JavaDoc[] args)
115    {
116       RawClient client = new RawClient();
117       client.startClient();
118    }
119 }
Popular Tags