import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer 
{
public static void main(String[] args)
{
Server server=new Server(100,200,400,200,"Server");
server.startServer();
}
}class Server extends Frame
{
public int height;
public int weight;
public int top;
public int right;
public TextField tf;
public TextArea ta;
ServerSocket ss;
Socket client;
PrintStream ps;   //为什么PrintStream一定要定义为属性?


public Server(int height,int weight,int top,int right,String title)
{
this.setBounds(top,right,height,weight);
this.setTitle(title);
ta=new TextArea();
tf=new TextField();
 tf.addActionListener(new TFListner());
this.setLayout(new BorderLayout());
this.add(ta,BorderLayout.NORTH);
this.add(tf,BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
}   
public void startServer()  
{
try 
{
ss=new ServerSocket(9999);
while(true)
{
client=ss.accept();
ta.append("有客户端连接上来了"+"\n");

InputStream in=client.getInputStream();
InputStreamReader isr=new InputStreamReader(in);
BufferedReader br=new BufferedReader(isr);

while(true)
{
String line=br.readLine();
if(line!=null)
{
ta.append(client.getInetAddress()+"说:"+line+"\n");
}
}  
}

catch (IOException e)
{
ta.append("客户端已经推出了"+"\n");
}
}


class TFListner implements ActionListener   
{
public void actionPerformed(ActionEvent arg0)
{
String str=tf.getText();
ta.append("我说:"+str+"\n");  
tf.setText("");

try 
{
PrintStream ps = new PrintStream(client.getOutputStream());  //就是这句了,为什么PrintStream放在这里不行?

catch (IOException e) 
{
e.printStackTrace();
}
send s=new send(ps,str);                       
Thread thread =new Thread(s);
thread.start();
}
}
}class send implements Runnable   
{
public PrintStream pr;  
public String str;        

public send(PrintStream pr,String str) 
   {
   try 
   {
   this.str=str;
   this.pr=pr;
   } 
   catch (Exception e) 
   {
e.printStackTrace();
   }
   }
public void run()
{
pr.println(str);
        pr.flush();
}}

解决方案 »

  1.   

    try 

    PrintStream ps = new PrintStream(client.getOutputStream());  //就是这句了,为什么PrintStream放在这里不行? 
    } 这样写,在try块之外你就访问不到ps这个引用了,比如你想在finally块中关闭流对象ps.close(),就做不到了
      

  2.   

    import java.awt.BorderLayout;
    import java.awt.Frame;
    import java.awt.TextArea;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class ChatServer 
    {
    public static void main(String[] args)
    {
    Server server=new Server(100,200,400,200,"Server");
    server.startServer();
    }
    }class Server extends Frame
    {
    public int height;
    public int weight;
    public int top;
    public int right;
    public TextField tf;
    public TextArea ta;
    ServerSocket ss;
    Socket client;
       //PrintStream ps;   //为什么PrintStream一定要定义为属性?


    public Server(int height,int weight,int top,int right,String title)
    {
    this.setBounds(top,right,height,weight);
    this.setTitle(title);
    ta=new TextArea();
    tf=new TextField();
     tf.addActionListener(new TFListner());
    this.setLayout(new BorderLayout());
    this.add(ta,BorderLayout.NORTH);
    this.add(tf,BorderLayout.SOUTH);
    this.pack();
    this.setVisible(true);
    }   
    public void startServer()  
    {
    try 
    {
    ss=new ServerSocket(9999);
    while(true)
    {
    client=ss.accept();
    ta.append("有客户端连接上来了"+"\n");

    InputStream in=client.getInputStream();
    InputStreamReader isr=new InputStreamReader(in);
    BufferedReader br=new BufferedReader(isr);

    while(true)
    {
    String line=br.readLine();
    if(line!=null)
    {
    ta.append(client.getInetAddress()+"说:"+line+"\n");
    }
    }  
    }

    catch (IOException e)
    {
    ta.append("客户端已经推出了"+"\n");
    }
    }


    class TFListner implements ActionListener   
    {
    public void actionPerformed(ActionEvent arg0)
    {
    String str=tf.getText();
    ta.append("我说:"+str+"\n");  
    tf.setText("");

    try 
    {
    PrintStream ps = new PrintStream(client.getOutputStream());  //就是这句了,为什么PrintStream放在这里不行?
           //定义在此处为何不能访问了啊?

    catch (IOException e) 
    {
    e.printStackTrace();
    }
    send s=new send(ps,str);                       
    Thread thread =new Thread(s);
    thread.start();
    }
    }
    }class send implements Runnable   
    {
    public PrintStream pr;  
    public String str;        

    public send(PrintStream pr,String str) 
       {
       try 
       {
       this.str=str;
       this.pr=pr;
       } 
       catch (Exception e) 
       {
    e.printStackTrace();
       }
       }
    public void run()
    {
    pr.println(str);
            pr.flush();
    }}
     
     这样就出错了呢,,为什么啊?
      麻烦给我说清楚点下,,Thank you!
      

  3.   

    日本人哦!原来这样,,怎么出了这个小错。。Thank you!1
      

  4.   

    因为一个对象的有效作用期间是一对{ }之内,在try之内定义的 当然不能在整个类中生效
    所以才会定义全局变量  将用的属性都定义在类的开始部分