请问各位:这个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();
}
}
}
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();
}
}
}
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("");
}
}
}
----------------------
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();
}
}
}