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不知道什么问题。

解决方案 »

  1.   

    java不是可以自动初始化一个变量或类么?  这里是由于内部匿名类无法访问变量造成的么?
      

  2.   

    Java要求变量必须初始化,其中基本类型变量有默认的初始值。
      

  3.   

    楼主请看注解:  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;//这里应改为:
                                                   //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){}
                            }
                        }
                    };
      

  4.   

    变量没有初始化,如果try块出现异常,会导致to_server未初始化,也就是你的代码不能完全保证to_server被初始化.和下面的代码类似:String str;
    if(new Object().equals(new Object())){
        str = "";
    }
    str.charAt(1);//The local variable to_server may not have been initialized