package org.com.url.server;import java.io.*;
import java.net.*;public class ProxyServer {
public static void mian(String[] args){
try{
if((args.length ==0)||(args.length % 3!=0))
throw new IllegalArgumentException("wrong number of args");
Server s = new Server(null,12);
int i = 0;
while(i<args.length){
String host = args[i++];
int remoteport = Integer.parseInt(args[i++]);
int localport = Integer.parseInt(args[i++]);
s.addService(new Proxy(host,remoteport),localport);
}
}catch(Exception e){
System.err.println(e);
System.err.println("USAGE:java ProxyServer" +
"<host><remoteport><localport>....");
System.exit(1);
}
}
public static class Proxy implements Server.Service{
String host;
int port;
public Proxy(String host,int port){
this.host = host;
this.port = port;
}
public void serve(InputStream in, OutputStream out){
final InputStream from_client = in;
final OutputStream to_client = out;
final InputStream from_server;
final OutputStream to_server;
final Socket server;
try{
server =new Socket(host,port);
from_server = server.getInputStream();
to_server = server.getOutputStream();
}catch(Exception e){
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
pw.print("Proxy server could not connection to"+host+" and "+port+"\n");
pw.flush();
pw.close();
try{
in.close();
}catch(IOException ex){
return;
}
final Thread[] threads = new Thread[2];
Thread c2s = new Thread(){
public void run(){
byte[] buffer = new byte[2048];
int bytes_read;
try{
while((bytes_read = from_client.read(buffer))!=-1){
to_server.write(buffer,0,bytes_read);
to_server.flush();
}
}
catch(IOException e){
}
finally{
try{
server.close();
to_client.close();
from_client.close();
}catch(IOException e){}
}
}
};
Thread s2c = new Thread(){
public void run(){
byte[] buffer = new byte[2048];
int bytes_read;
try{
while((bytes_read=from_server.read(buffer))!=-1){
to_client.write(buffer,0,bytes_read);
to_client.flush();
}
}
catch(IOException e){}
finally{
try{
server.close();
to_client.close();
from_client.close();
}catch(IOException e ){}
}
}
};
threads[0] = c2s;
threads[1]= s2c;
c2s.start();
s2c.start();
try{
c2s.join();
s2c.join();
}
catch(InterruptedException err){}
}
}
}}红色下画线的变量,编辑器报告如下错误:
The local variable to_server may not have been initialized不知道什么问题。
final InputStream from_client = in;
final OutputStream to_client = out;
final InputStream from_server;
final OutputStream to_server;//这里应改为:
//OutputStream to_server = null;
final Socket server;
try{
server =new Socket(host,port);
from_server = server.getInputStream();
to_server = server.getOutputStream(); //to_server在这里获得一个值
//但catch块看不到
}catch(Exception e){
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
pw.print("Proxy server could not connection to"+host+" and "+port+"\n");
pw.flush();
pw.close();
try{
in.close();
}catch(IOException ex){
return;
}
final Thread[] threads = new Thread[2];
Thread c2s = new Thread(){
public void run(){
byte[] buffer = new byte[2048];
int bytes_read;
try{
while((bytes_read = from_client.read(buffer))!=-1){
to_server.write(buffer,0,bytes_read); //所以这里编译器
//就认为这个变量可能没有初始化
to_server.flush();
}
}
catch(IOException e){
}
finally{
try{
server.close();
to_client.close();
from_client.close();
}catch(IOException e){}
}
}
};
if(new Object().equals(new Object())){
str = "";
}
str.charAt(1);//The local variable to_server may not have been initialized