1 2 29 30 package com.jcraft.jhttptunnel; 31 32 import java.net.*; 33 import java.io.*; 34 35 public class OutBoundSocket extends OutBound{ 36 static final private byte[] _rn="\r\n".getBytes(); 37 38 private Socket socket=null; 39 private InputStream in=null; 40 private OutputStream out=null; 41 42 public void connect() throws IOException{ 43 close(); 44 45 String host=getHost(); 46 int port=getPort(); 47 48 String request="/index.html?crap=1 HTTP/1.1"; 49 50 Proxy p=getProxy(); 51 if(p==null){ 52 socket=new Socket(host, port); 53 request="POST "+request; 54 } 55 else{ 56 String phost=p.getHost(); 57 int pport=p.getPort(); 58 socket=new Socket(phost, pport); 59 request="POST http://"+host+":"+port+request; 60 } 61 socket.setTcpNoDelay(true); 62 63 in=socket.getInputStream(); 64 out=socket.getOutputStream(); 65 out.write(request.getBytes()); 66 out.write(_rn); 67 out.write(("Content-Length: "+getContentLength()).getBytes()); 68 out.write(_rn); 69 out.write("Connection: close".getBytes()); 70 out.write(_rn); 71 out.write(("Host: "+host+":"+port).getBytes()); 72 out.write(_rn); 73 74 out.write(_rn); 75 out.flush(); 76 77 sendCount=getContentLength(); 78 79 } 81 82 public void sendData(byte[] foo, int s, int l, boolean flush) throws IOException{ 83 if(l<=0) return; 85 if(sendCount<=0){ 86 System.out.println("1#"); 87 connect(); 88 } 89 90 int retry=2; 91 while(retry>0){ 92 try{ 93 out.write(foo, s, l); 94 if(flush){ out.flush(); } 95 sendCount-=l; 96 return; 97 } 98 catch(SocketException e){ 99 throw e; 101 } 102 catch(IOException e){ 103 connect(); 105 } 106 retry--; 107 } 108 } 109 110 public void close() throws IOException{ 111 if(socket!=null){ 112 if(out!=null){ try{out.flush(); out.close();}catch(IOException e){} } 113 if(in!=null){ try{in.close();}catch(IOException e){} } 114 socket.close(); 115 socket=null; 116 } 117 } 118 } 119 | Popular Tags |