电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

简易聊天室JAVA代码

简易聊天室JAVA代码_第1页
1/9
简易聊天室JAVA代码_第2页
2/9
简易聊天室JAVA代码_第3页
3/9
import java.awt.*;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.*;public class ChatClient extends Frame {private boolean connected = false;private DataOutputStream dos = null;private DataInputStream dis = null;private static final long serialVersionUID = 1L;private Socket s = null;private TextField tfWords = null;private TextArea taShow = null;public static void main(String args[]) {ChatClient cc = new ChatClient();cc.launchFrame();cc.Listen();}public void launchFrame() {try {s = new Socket(InetAddress.getByName("localhost"), 8888);connected = true;dos = new DataOutputStream(s.getOutputStream());dis = new DataInputStream(s.getInputStream());} catch (IOException ioe) {System.out.println("服务器连接失败!");ioe.printStackTrace();}this.tfWords = new TextField();this.taShow = new TextArea();this.setTitle("Chatroom Client");this.setLocation(700, 50);this.add(tfWords, BorderLayout.SOUTH);this.add(taShow, BorderLayout.NORTH);this.setSize(200, 250);this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {try {connected = false;dos.writeUTF("###Exit###");dos.flush();close();System.exit(0);} catch (IOException ioe) {System.out.println("消息发送出错!");ioe.printStackTrace();}}});this.setVisible(true);this.tfWords.addActionListener(new TFWordsListener());}public void Listen() {new Thread(new ListenThread()).start();}public void close() {try {if (dos != null)dos.close();if (s != null)s.close();} catch (IOException ioe) {ioe.printStackTrace();System.out.println("客户端断开时出错!");System.exit(-1);}}public class ListenThread implements Runnable{public void run(){while (connected) {try {String back = dis.readUTF(); //堵塞...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

简易聊天室JAVA代码

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部