请问各位:这个server程序的disconnect为什么执行不了。比较菜,见笑啦。
import java.net.*;
import java.io.*;
import java.awt.*;
public class chatServer {
ServerSocket ss = null;
Socket s = null;
static DataInputStream dis = null;

void Disconnect(String _str){

try {
dis = new DataInputStream(s.getInputStream());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(_str == "exit")
try {
dis.close();
s.close();
System.out.println("close socket and IO !");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   
}

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(8888);
while(true){
Socket s = ss.accept();
System.out.println("a client connected!");
DataInputStream dis = new DataInputStream(s.getInputStream());
while(true){
String str = dis.readUTF();
System.out.println(str);


}

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
try {
new chatServer().Disconnect(dis.readUTF());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
 }

解决方案 »

  1.   

    没提供客户端连接socket的代码?Socket s = ss.accept(); //这个是接收socket连接,如果没客户端连接它,它会一直在这里blocked不知道理解的对不对:你运行这个java后,还得在运行个连接8888这个SocketServer的客户端java?!
      

  2.   

    客户端程序如下import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;public class ChatClient extends Frame{
    TextField tfTxt = new TextField();
    TextArea taContent = new TextArea();

    DataInputStream in = null;
    DataOutputStream out = null;

    Socket mySocket = null;

    public void connect(){

    try {
    mySocket = new Socket("localhost",8888);
    System.out.print("connected to server!");

    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    /*public void sendData(){
    try {
    in = new DataInputStream(mySocket.getInputStream());
    out = new DataOutputStream(mySocket.getOutputStream());
    //out.writeUTF("hello Server!");
    out.writeUTF(taContent.getText());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }*/




    public static void main(String[] args) {


    // TODO Auto-generated method stub
    new ChatClient().launchFrame();
    //new ChatClient().connect();




    }


    public void launchFrame() {
    this.setSize(300,400);
    //this.setBackground(Color.blue);
    this.setLocation(300, 400);
    add(tfTxt,BorderLayout.SOUTH);
    add(taContent,BorderLayout.NORTH);
    pack();// 窗口匹配


    this.addWindowListener(new WindowAdapter(){ @Override
    public void windowClosing(WindowEvent arg0) {
    // TODO Auto-generated method stub
    System.exit(0);
    } });
    tfTxt.addActionListener(new Tflistener());

    this.setVisible(true);
    connect(); // setup connect to server
    //sendData();//send data to server


    }
    private class Tflistener implements ActionListener{@Override
    public void actionPerformed(ActionEvent e) {
    String str = tfTxt.getText().trim();
    taContent.setText(str);
    try {
    out = new DataOutputStream(mySocket.getOutputStream());
    out.writeUTF(str);

    System.out.println(str);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    tfTxt.setText("");
    }
    }


    }
      

  3.   

    我觉得你写的代码比较混乱,按照你的意思我帮你改造了下,本地测试可以:chatServer.java:
    ----------------------
    import java.net.*;
    import java.io.*;
    import java.awt.*;public class chatServer { static ServerSocket ss = null;
    static Socket s = null;
    static DataInputStream dis = null; static void Disconnect() {
    try {
    if(null!=dis)
    dis.close();
    if(null!=s)
    s.close();
    System.out.println("close socket and IO !");
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public static void main(String[] args) {

    try {
    ss = new ServerSocket(8888);
    while (true) {
    s = ss.accept();
    System.out.println("a client connected!");
    dis = new DataInputStream(s.getInputStream());
    while (true) {
    String str = dis.readUTF();
    System.out.println(str);
    if("exit".equals(str)) {
    Disconnect();
    break;
    }
    }
    if(s.isClosed())
    break;
    }
    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    }