1 2 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 host=getHost(); 46 int port=getPort(); 47 String request="/index.html?crap=1 HTTP/1.0"; 48 Proxy p=getProxy(); 50 51 if(p==null){ 52 socket=new Socket(host, port); 53 request="GET "+request; 54 } 55 else{ 56 String 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 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 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 connect(); 117 } 118 catch(SocketException e){ 119 throw e; 120 } 121 catch(IOException e){ 122 System.out.println("2$ "+e); 123 throw e; 124 } 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 |