ChatClient (20)

/**
*Title:ChatClient
*Description:创建一个客户端
*@Copyright:
*@Company:
*@autor:firefly
*@version:1.0
*@time:2012.10.3
*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ChatClient extends Frame {
 
 Socket s =null;
 TextField tfTxt = new TextField();
 TextArea taContent = new TextArea();
 DataOutputStream dos = null;
 DataInputStream dis = null;
 private boolean bConnected = false;
 Thread tRect = new Thread(new RecvTread());
 
 public static void main(String[] args) {
  new ChatClient().launchFrame();
 }

 public void launchFrame() {
  setLocation(300, 200);
  setSize(300, 300);
  add(tfTxt, BorderLayout.SOUTH);
  add(taContent, BorderLayout.NORTH);
  pack();
  this.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    disconnect();
    System.exit(0);
   }
  });
  tfTxt.addActionListener(new TFListener());
  setVisible(true);
  connect();
  tRect.start();  //每打开一个客户端就打开一个新的线程。
 }
 
 public void connect() {  //可以写一个方法,也可以直接写try,catch里面的内容。
  try {
   s = new Socket("122.207.171.222", 8888);  //创建一个流套接字并将其连接到指定 IP 地址的指定端口号。
System.out.println("connected!");
   dos = new DataOutputStream(s.getOutputStream());
   dis = new DataInputStream(s.getInputStream());
   bConnected = true;
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public void disconnect() {
  try {
   bConnected = false;
   dos.close();
   dis.close();//要先把dis关闭了之后才能把s给关了,否则在关了ChatClient窗口之后公报错。
   s.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  /*    //以后想让线程停止的话,要先用joint方法让线程正常停止。如果线程里面有阻塞性的方法停止不了,报Exception
        //可以考虑让他报Exception后停止。
  try{
   bConnected  = false;
   tRect.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 = tfTxt.getText();
   tfTxt.setText("");
   try {
    dos.writeUTF(str);
    dos.flush();
    //dos.close();
   } catch (IOException e1) {
    e1.printStackTrace();
   }
   
  }
 }
 
 private class RecvTread implements Runnable {
  public void run() { 
   try {
    while(bConnected) {
     String str = dis.readUTF();
System.out.println(str);
     taContent.setText(taContent.getText() + str + '\n');
     //要让原来的东西保留下来,就不能只写一个str,要写为taContent.getText() + str + '\n',这种形式。
    }
   } catch (SocketException e) {            //这里实际上放的是一个烟雾弹。
    System.out.println("退出了,bye!");
   } catch(IOException e) {
    e.printStackTrace();
   }
  }

 }
 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值