这是server端
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.List;
import java.util.ArrayList;
public class ChatServer {
boolean start = false;
ServerSocket ss = null;
List<Client> clist = new ArrayList<Client>();
public static void main (String args[]) {
new ChatServer().start();
}
public void start () {
ServerSocket ss = null;
try {
ss = new ServerSocket(5688);
start = true;
}catch(BindException e) {
System.out.println("服務器已關閉");
System.exit(0);
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
try{
while(start) {
Socket s = ss.accept();
System.out.println("a client connect!");
Client c = new Client(s);
new Thread(c).start();
clist.add(c);
}
} catch (IOException e) {
System.out.println("server closed");
}finally {
try {
ss.close();
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("guanbi");
}
}
}
class Client implements Runnable {
Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
boolean bin = false;
public Client (Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bin = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while (bin){
String str = dis.readUTF();
for(int i = 0;i<clist.size();i++) {
Client c = clist.get(i);
c.send(str);
}
}
} catch (SocketException e) {
System.out.println("对方退出了!");
} catch (EOFException e) {
System.out.println("已关闭");
} catch (IOException e) {
e.printStackTrace();
}//注意 finally!!!!!!!!!!!!!!!!
finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
//s = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void send (String str){
try {
dos.writeUTF(str);
}catch (IOException e) {
clist.remove(this);
//e.printStackTrace();
}
/*finally {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
}
// cuocuoucocuo
}
}
client端
package wl.chat;
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.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
/*
* frameclient 实现多个窗口聊天
*/
public class ChatClient extends Frame {
TextField tf = new TextField();
TextArea ta = new TextArea();
DataOutputStream dos = null;
DataInputStream dis = null;
RecvThread rt = new RecvThread();
boolean bconnect = false;
Socket s = null;
public static void main(String[] args) {
new ChatClient().launchFrame();
}
private void launchFrame() {
this.setLocation(300, 300);
this.setSize(300, 200);
this.add(tf,BorderLayout.SOUTH);
this.add(ta,BorderLayout.NORTH);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
tf.addActionListener(new TFListener());
pack();
setVisible(true);
Connect();
new Thread(rt).start();
}
public void Connect (){
try {
s = new Socket("127.0.0.1",5688);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
bconnect = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect () {
try { //加入 join()方法 视频 1.2fix
s.close();
dos.close();
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText().trim();
//ta.setText(str);
tf.setText("");
try {
dos.writeUTF(str); //写到最后 为什么不行?????????????? 会抛 EOFException 这是runtimeexception 不用逮 直接结束!!!
dos.flush();
//dos.close();
} catch (SocketException e1) {
System.out.println("退出了,bye!");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while(bconnect) {
String str = dis.readUTF();
System.out.println(str);
ta.setText(ta.getText() + str + '\n');
}
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|