KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jhttptunnel > InBoundSocket


1 /* -*-mode:java; c-basic-offset:2; -*- */
2 /*
3 Copyright (c) 2004 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29
30 package com.jcraft.jhttptunnel;
31
32 import java.net.*;
33 import java.io.*;
34
35 public class InBoundSocket extends InBound{
36   static final private byte[] _rn="\r\n".getBytes();
37
38   private Socket socket=null;
39   private OutputStream out=null;
40   private InputStream in=null;
41
42   public void connect() throws IOException{
43     close();
44
45     String JavaDoc host=getHost();
46     int port=getPort();
47     String JavaDoc request="/index.html?crap=1 HTTP/1.0";
48 // String request="/index.html?crap=1 HTTP/1.1";
49
Proxy p=getProxy();
50
51     if(p==null){
52       socket=new Socket(host, port);
53       request="GET "+request;
54     }
55     else{
56       String JavaDoc phost=p.getHost();
57       int pport=p.getPort();
58       socket=new Socket(phost, pport);
59       request="GET http://"+host+":"+port+request;
60     }
61     socket.setTcpNoDelay(true);
62
63     out=socket.getOutputStream();
64     out.write(request.getBytes());
65     out.write(_rn);
66 // if(p==null){out.write(("Connection: keep-alive").getBytes());}
67
// else{out.write(("Proxy-Connection: keep-alive").getBytes());}
68
// out.write(_rn);
69
// out.write(("Host: "+host+":"+port).getBytes());
70
// out.write(_rn);
71

72     out.write(_rn);
73     out.flush();
74
75     in=socket.getInputStream();
76
77     byte[] tmp=new byte[1];
78     while(true){
79       int i=in.read(tmp, 0, 1);
80       if(i>0){
81     if(tmp[0]!=0x0d){ continue; }
82       }
83       i=in.read(tmp, 0, 1);
84       if(i>0){
85     if(tmp[0]!=0x0a){ continue; }
86       }
87       i=in.read(tmp, 0, 1);
88       if(i>0){
89     if(tmp[0]!=0x0d){ continue; }
90       }
91       i=in.read(tmp, 0, 1);
92       if(i>0){
93     if(tmp[0]!=0x0a){ continue; }
94       }
95       break;
96     }
97   }
98
99   public int receiveData(byte[] foo, int s, int l) throws IOException{
100     if(l<=0){ System.out.println("receivdEdaa: "+l); }
101     if(l<=0) return -1;
102     while(true){
103 // if(closed) return -1;
104
try{
105     if(foo==null){
106       if(l<=0) return -1;
107       long bar=in.skip((long)l);
108       l-=bar;
109       continue;
110     }
111     int i=in.read(foo, s, l);
112     if(i>0){
113       return i;
114     }
115 //System.out.println("1$ i="+i+" close="+closed);
116
connect();
117       }
118       catch(SocketException e){
119     throw e;
120       }
121       catch(IOException e){
122 System.out.println("2$ "+e);
123 throw e;
124 // connect();
125
}
126     }
127   }
128
129   public void close() throws IOException{
130     if(socket!=null){
131       if(out!=null){ try{out.close();}catch(IOException e){} }
132       if(in!=null){ try{in.close();}catch(IOException e){} }
133       socket.close();
134       socket=null;
135     }
136   }
137 }
138
Popular Tags