JavaAWT简单Socket多人聊天程序(简单明了)

来源:互联网 发布:深圳龙华淘宝客服招聘 编辑:程序博客网 时间:2024/06/07 18:39

//-------------------------------------------Server-----------------------------------------------:

public class ChatServer {
//所有客户端对象List
List<Client> clients = new ArrayList<Client>();
//所有客户端对象Iterator
Iterator<Client> it;
public static void main(String[] args) {
new ChatServer().start();
}


public void start() {
boolean started = false;
ServerSocket ss = null;
try {
ss = new ServerSocket(5555);//端口号5555
started = true;
System.out.println("服务端已启动!");
} catch (SocketException e) {
System.out.println("端口正在被使用,请关闭相应程序!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while (started) {
Socket s = ss.accept();
//有客户端连了
Client c = new Client(s);
clients.add(c);
System.out.println("a client connected!");
new Thread(c).start();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


/**
* 每个请求过来启用一个线程来处理<br>
*/
private class Client implements Runnable {
DataInputStream dis = null;
DataOutputStream dos = null;
boolean connected = false;


Client(Socket s) {
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
connected = true;
}catch(EOFException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}


public void run() {
while (connected) {
try {
// System.out.println("---------------------"+clients.size()+"---------------------");
it= clients.iterator();
//把客户端发的信息读进来
String msg = dis.readUTF();
//把信息写出去发给每个客户端
while (it.hasNext()) {
Client c = it.next();
try {
c.send(msg);
} catch (SocketException e) {
//从服务端发送信息失败,此client可能已关闭,所以要把它从Iterator里remove(不要用clients.remove会报ConcurrentModificationException)
it.remove();
}
}
} catch (EOFException eof) {
connected = false;
System.out.println("服务端EOFException:客户端已关闭");
}catch(SocketException e){
connected = false;
System.out.println("服务端读取信息失败:SocketException");
}catch (IOException e) {
System.out.println("服务端读取信息失败:IOException");
}
}
}


private void send(String str) throws IOException {
dos.writeUTF(str);
}
}
}


//-------------------------------------------Client-----------------------------------------------:

public class ChatClient extends Frame {
private static final long serialVersionUID = 5798410085795768914L;
public TextField tf = new TextField();
public TextArea ta = new TextArea();


DataOutputStream dos = null;
DataInputStream dis = null;


public Socket socket = null;


boolean connected = false;


Thread receiver = new Thread(new ReceiverThread());


public static void main(String[] args) {
new ChatClient().launchFrame();
}


public void launchFrame() {
this.setLocation(250, 100);
this.setSize(500, 600);
this.add(tf, BorderLayout.SOUTH);
this.add(ta, BorderLayout.NORTH);
this.pack();
this.addWindowListener(new WindowMonitor());
tf.addKeyListener(new KeyMonitor());
connect();
receiver.start();
this.setVisible(true);
}


public void connect() {
try {
socket = new Socket("127.0.0.1", 5555);//port5555
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
connected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


public void disconnect() {
try {
dis.close();
dos.close();
socket.close();
}catch(EOFException e){
System.out.println("这是合法的,客户端已经关闭");
}catch (IOException e) {
e.printStackTrace();
}
}
public void closeWindow(){
dispose();
}
private class WindowMonitor extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
disconnect();
// System.exit(0);
closeWindow();
}
}


private class KeyMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (KeyEvent.VK_ENTER == keyCode) {
try {
dos.writeUTF(tf.getText().trim());
dos.flush();
} catch (IOException ioe) {
System.out.println("客户端发送信息失败:"+"\n");
ioe.printStackTrace();
}
tf.setText("");
}
}
}


private class ReceiverThread implements Runnable {


public void run() {
try {
while (connected) {
String str = dis.readUTF();
ta.setText(ta.getText() + str + '\n');
}
} catch (SocketException e) {
System.out.println("客户端断开连接!SokectException");
connected = false;
} catch (EOFException e) {
System.out.println("客户端断开连接!EOFException");
connected = false;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

0 0
原创粉丝点击