1 2 29 30 package com.jcraft.jhttptunnel; 31 32 import java.io.*; 33 import javax.microedition.io.*; 34 35 public class OutBoundConnector extends OutBound{ 36 private InputStream in=null; 37 private OutputStream out=null; 38 private HttpConnection con=null; 39 private final byte[] _TUNNEL_DISCONNECT={(byte)0x47}; 40 41 private int count=0; 42 43 public void connect() throws IOException{ 44 47 String host=getHost(); 48 int port=getPort(); 49 50 con=(HttpConnection)Connector.open("http://"+host+":"+port+"/index.html?crap=1"+"&count="+count); 51 count++; 52 con.setRequestMethod(HttpConnection.POST); 53 out=con.openOutputStream(); 54 sendCount=getContentLength(); 55 } 56 57 public synchronized void sendData(byte[] foo, int s, int l, boolean flush) throws IOException{ 58 if(l<=0) return; 60 61 if(con==null){ 62 connect(); 63 } 64 65 if(sendCount<=0){ 66 connect(); 67 } 68 69 int retry=2; 70 while(retry>0){ 71 try{ 72 out.write(foo, s, l); 73 sendCount-=l; 74 if(true){ 75 out.flush(); 79 return; 81 } 82 return; 83 } 84 catch(IOException e){ 90 connect(); 93 } 94 retry--; 95 } 96 } 97 98 public void close() throws IOException{ 99 if(con!=null){ 101 if(out!=null){ try{out.close(); out=null;}catch(IOException e){} } 102 if(in!=null){ 103 try{ 104 112 in.close(); in=null;}catch(IOException e){} } 113 try{con.close();}catch(Exception e){System.out.println(e);} 114 con=null; 115 } 117 } 119 } 120 | Popular Tags |