package text;

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.*;
import java.net.*;

public class ChatClient extends Frame {
	Socket s = null;
	DataOutputStream dos = null;
	DataInputStream dis = null;
	private boolean bconnection = false;
	private static final long serialVersionUID = 1L;
	TextField tf = new TextField();
	TextArea ta = new TextArea();
	Thread trece = new Thread(new ReceThread());

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

	public ChatClient() {
		add(tf, BorderLayout.SOUTH);
		add(ta, BorderLayout.NORTH);
		this.setSize(400, 300);
		pack();
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				disconnect();
				System.exit(0);
			}
		});
		tf.addActionListener(new TFlistener());
		this.setTitle("在线聊天");
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		connect();
		trece.start();

	}

	public void connect() {
		try {
			s = new Socket("localhost", 8888);
			dos = new DataOutputStream(s.getOutputStream());
			dis = new DataInputStream(s.getInputStream());
			System.out.println("connect");
			bconnection = true;
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void disconnect() {

		try {
			dos.close();
			dis.close();
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		/*
		 * try { bconnection = false; trece.join();
		 * 
		 * 
		 * }catch (InterruptedException e) { e.printStackTrace(); } finally{ try
		 * { dos.close(); dis.close(); s.close(); } catch (IOException e) {
		 * e.printStackTrace(); } }
		 */
	}

	private class TFlistener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String str = tf.getText().trim();
			tf.setText("");
			try {
				dos.writeUTF(str);
				dos.flush();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

	}

	public class ReceThread implements Runnable {

		public void run() {
			try {
				while (bconnection) {
					String str = dis.readUTF();
					ta.setText(ta.getText() + str + '\n' + '\n');
				}
			} catch (SocketException e) {
				System.out.println("客户端已经退出");
			}catch (EOFException e){
				System.out.println("退出了――――――baybay!");
			}
			
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
最近下载更多
zeng1206  LV5 2023年12月28日
Oriamphei  LV1 2023年11月23日
www123ww  LV1 2022年12月13日
heweimin  LV12 2022年7月18日
shan454  LV3 2022年1月24日
胡萝北呀  LV3 2021年11月26日
ComeDebug  LV6 2021年9月6日
datlric  LV1 2021年7月5日
忧麦紫  LV18 2021年6月26日
q啦啦啦a  LV2 2021年3月17日
最近浏览更多
taoshen95  LV14 1月18日
zeng1206  LV5 2023年12月21日
Oriamphei  LV1 2023年11月23日
yangxb2  LV10 2023年7月11日
林间听风  LV10 2023年6月5日
belike 2023年5月26日
暂无贡献等级
另类清晨  LV8 2023年5月15日
CL200228  LV4 2023年5月5日
1721281527  LV2 2023年2月12日
hhdbzz  LV1 2022年12月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友