Java实现多个客户端聊天程序

复习了一天Java。晚上写了一个HeartChat 0.1,实现多个客户端相互聊天的机制。代码如下:
Java代码 复制代码 收藏代码
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.lang.*;
  5. import java.net.*;
  6. public class HeartClient extends Frame {
  7. /*
  8. * 成员方法出场...
  9. */
  10. private TextField tfText;
  11. private TextArea taContent;
  12. private Socket s;
  13. private DataOutputStream dos;
  14. private DataInputStream dis;
  15. /**
  16. * 注意,入口... ^^
  17. * @param args
  18. */
  19. public static void main(String[] args) {
  20. new HeartClient().launchFrame();
  21. }
  22. /**
  23. * Loading GU
  24. */
  25. public void launchFrame(){
  26. tfText = new TextField();
  27. taContent = new TextArea();
  28. this.setSize(300,300);
  29. this.setLocation(300,300);
  30. this.tfText.addActionListener(new TFListener());
  31. this.add(tfText,BorderLayout.SOUTH);
  32. this.add(taContent,BorderLayout.NORTH);
  33. this.addWindowListener(new WindowAdapter(){
  34. @Override
  35. public void windowClosing(WindowEvent e) {
  36. System.exit(0);
  37. }});
  38. this.pack();
  39. this.connect();
  40. this.setVisible(true);
  41. }
  42. /**
  43. * 我在努力地连接服务器中...
  44. */
  45. public void connect() {
  46. try {
  47. s = new Socket("127.0.0.1",1720);
  48. dos = new DataOutputStream(s.getOutputStream());
  49. dis = new DataInputStream(s.getInputStream());
  50. new Thread(new SendThread()).start();
  51. // dos.writeUTF("Hello,i find u!");
  52. } catch (UnknownHostException e) {
  53. System.out.println("UnknownHostException");
  54. e.printStackTrace();
  55. } catch (IOException e) {
  56. System.out.println("IOException");
  57. e.printStackTrace();
  58. }finally{
  59. //关闭啥尼...
  60. }
  61. }
  62. /**
  63. * 额不会傻等着tfText(TextField tfText的监听器类)
  64. */
  65. class TFListener implements ActionListener{
  66. private String str;
  67. @Override
  68. public void actionPerformed(ActionEvent e) {
  69. str = tfText.getText().trim();
  70. tfText.setText("");
  71. try {
  72. dos.writeUTF(str);
  73. } catch (IOException e1) {
  74. System.out.println("IOException");
  75. e1.printStackTrace();
  76. }
  77. }
  78. }
  79. /**
  80. * 客户端接收消息的线程呦...
  81. *
  82. */
  83. class SendThread implements Runnable{
  84. private String str;
  85. private boolean iConnect = false;
  86. public void run(){
  87. iConnect = true;
  88. recMsg();
  89. }
  90. /**
  91. * 消息,看招,哪里跑...(客户端接收消息的实现)
  92. * @throws IOException
  93. */
  94. public void recMsg() {
  95. try {
  96. while(iConnect){
  97. str = dis.readUTF();
  98. taContent.setText(str);
  99. }
  100. } catch (IOException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. }
  105. }
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.net.*;

public class HeartClient extends Frame {

	/*
	 *  成员方法出场...
	 */
	private TextField tfText;
	private TextArea taContent;
	private Socket s;
	private DataOutputStream dos;
	private DataInputStream dis;
	
	/**
	 * 注意,入口... ^^
	 * @param args
	 */
	public static void main(String[] args) {
		new HeartClient().launchFrame();
		
	}
	
	/**
	 * Loading GU
	 */
	public void launchFrame(){
		tfText = new TextField();
		taContent = new TextArea();
		this.setSize(300,300);
		this.setLocation(300,300);
		this.tfText.addActionListener(new TFListener());
		this.add(tfText,BorderLayout.SOUTH);
		this.add(taContent,BorderLayout.NORTH);
		this.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}});
		this.pack();
		this.connect();
		this.setVisible(true);
	}

	/**
	 * 我在努力地连接服务器中...
	 */
	public void connect() {
		try {
			s = new Socket("127.0.0.1",1720);
			dos = new DataOutputStream(s.getOutputStream());
			dis = new DataInputStream(s.getInputStream());
			new Thread(new SendThread()).start();
//			dos.writeUTF("Hello,i find u!");
		} catch (UnknownHostException e) {
			System.out.println("UnknownHostException");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IOException");
			e.printStackTrace();
		}finally{
			//关闭啥尼...
		}
		
	}

	/**
	 * 额不会傻等着tfText(TextField tfText的监听器类)
	 */
	class TFListener implements ActionListener{
		private String str;
		@Override
		public void actionPerformed(ActionEvent e) {
			str = tfText.getText().trim();
			tfText.setText("");
			try {
				dos.writeUTF(str);
			} catch (IOException e1) {
				System.out.println("IOException");
				e1.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 客户端接收消息的线程呦...
	 *
	 */
	class SendThread implements Runnable{
		private String str;
		private boolean iConnect = false;
		
		public void run(){
			iConnect = true;
			recMsg();
			
		}
		
		/**
		 * 消息,看招,哪里跑...(客户端接收消息的实现)
		 * @throws IOException 
		 */
		public void recMsg() {
			try {
				while(iConnect){
					str = dis.readUTF();
					taContent.setText(str);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
	}
	
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值