使用eclipse时,在一台机器上运行的好好的,ctrl+c到另一台上也中eclipse就不能运行,它就报错!但是eclipse不是一个版的!

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;public class ChatClient {
    public static void main(String[] args) {
    frameClient f = new frameClient();
    f.launchframe();
    }
    }class frameClient extends Frame { Socket client;
    TextField typeIn = new TextField();
    TextArea printOut = new TextArea();
    DataOutputStream toServer ;
    DataInputStream fromserver;
    boolean connected = false;

    public void launchframe() {
    this.setLocation(300, 400);
    setSize(300, 400);
    this.setBackground(Color.orange);
    printOut.setBackground(Color.orange);
    BorderLayout bl = new BorderLayout(2, 1);
    setLayout(bl);
    this.add(typeIn, BorderLayout.SOUTH);
    add(printOut, BorderLayout.NORTH);
    pack();
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    disconnect();
    System.exit(0);
    }
    } );
    typeIn.addActionListener(new typeInListener());
    this.setVisible(true);
    this.connect();
    new Thread(new printArea()).start();



    private class typeInListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    String s = typeIn.getText().trim();
    //printOut.setText(s);
    typeIn.setText("");
    printToserver(s);
    }
    }

    public void connect() {
    try {
    client = new Socket("169.254.64.49", 8888);
    toServer = new DataOutputStream(client.getOutputStream());
    fromserver = new DataInputStream(client.getInputStream());
    connected = true;
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void disconnect() {
    try {
    toServer.close();
    client.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void printToserver (String s) {
    try {
    toServer.writeUTF(s);
    toServer.flush();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private class printArea implements Runnable {
    public void run() {
    String str;
    try {
    while(connected) {
    str = fromserver.readUTF();
    printOut.setText(str + "\n" +printOut.getText());
    }
    } catch (IOException e) {
    //e.printStackTrace();
    System.out.println("The client is closed");
    } finally {
    try {
    if(fromserver != null) fromserver.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
      

  2.   

    答:与Eclipse没有关系。是与
    toServer = new DataOutputStream(client.getOutputStream()); 
    fromserver = new DataInputStream(client.getInputStream()); 
    这两个流的读写有关。当服务器的数据还没有发送过来时,客户端有时会有空引用异常。
      

  3.   

    null
    那就找使用null的引用的位置。