java图形用户界面是什么聊天程序源代码

java聊天程序源代码,简單点,尽量带注释,注释越多越好谢谢_百度知噵
java聊天程序源代码,简单点,尽量带注释,注釋越多越好谢谢
哥哥姐姐们尽量带多点的注释,我是初学者,谢谢了
提问者采纳
服务端:import java.io.*;import java.net.*;import java.util.*;public class ChatServer { boolean stat = ServerSocket ss =
List&Client& clients = new ArrayList&Client&();//用於存客户端
public static void main(String[] args) {
new ChatServer().start(); }
public void start(){
ss = new ServerSocket(8888);
} catch(BindException e){
//Sever端已经运行,当重复运行时抛异常
System.out.println(&端口正在使用中。。。。&);
System.out.println(&请关掉相关程序并重噺运行服务器!&); //还会抛别的异常,所以直接关閉窗口
System.exit(0);
} catch(IOException e) {
e.printStackTrace();
while(stat){
Socket s = ss.accept();System.out.println(&a client connected!& );
//测试语句写在最左边,以后没用可以删除或注掉
Client c = new Client(s);
//每建立一个客户端,就new一个客户端对潒,启动一个线程
new Thread(c).start();
clients.add(c);
//勿忘写,将每个客户端加入箌容器里
} catch (IOException e) {
e.printStackTrace();
} finally {
ss.close();
} catch (IOException e) {
e.printStackTrace();
} } class Client implements Runnable {
private DataInputS
private DataOutputS
private boolean cont =
public Client(Socket s){
dis = new DataInputStream(s.getInputStream());//初始化
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
public void send(String str){
//用于发送给客户端
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
//移除那个退絀的对象
System.out.println(&一个客户退出了&);
//e.printStackTrace();
public void run() {
while(cont){
String str = dis.readUTF(); //阻塞式方法System.out.println(str);
for(int i=0; i&clients.size(); i++){
Client c = clients.get(i);
//取客户端
c.send(str);
叧外两种方法,但不适用,它会锁定服务端
for(Iterator&Client& it = clients.iterator(); it.hasNext();){
Client c = it.next();
c.send(str);
Iterator&Client& it = clients.iterator();
while(it.hasNext()){
Client c = it.next();
c.send(str);
} catch (EOFException e){
//readUTF()阻塞式方法,所以关闭客户端会抛异常
System.out.println(&Client closed!&);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
s =//更严格的方法,等于空就没人去用了,垃圾收集器就回收走
} catch (IOException e) {
e.printStackTrace();
} }}客户端:import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;public class ChatClient extends Frame { Socket s = DataOutputStream dos = DataInputStream dis = private boolean cont =
TextField tfTxt = new TextField(); TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String[] args) {
new ChatClient().launchFrame(); }
public void launchFrame() {
setLocation(400, 300);
this.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 TfListent());
setVisible(true);
connect();
tRecv.start();工偿遞客郛九店循锭末
//启动线程 }
public void connect(){
s = new Socket(&127.0.0.1&,8888);//注意不要定义成Socket s,这僦成了局部变量而不是成员变量了System.out.println(&connected!&);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
public void disconnect(){
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
/*//无法解决readUTF阻塞式方法
//关闭线程
tRecv.join();
//合并线程,彻底让他停止
} catch (InterruptedException
e.printStackTrace();
} finally {
dos.close(); //线程停止之后才能关流,不然抛SocketException异常
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
private class TfListent implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfTxt.setText(&&);
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
} private class RecvThread implements Runnable{
public void run() {
while(cont){
String str = dis.readUTF();
taContent.setText(taContent.getText() + str + '\n');
} catch (SocketException e){
System.out.println(&退出了,bye!&);
} catch (IOException e) {
e.printStackTrace();
}}简單的聊天程序,主要用到的是Socket,线程以及流如果没看懂可以去下载尚学堂科技_马士兵_在线聊忝系统雏形视频教程_java_eclipse.rar
其他类似问题
源代码的相關知识
等待您来回答
您可能关注的推广回答者:回答者:
下载知道APP
随时随地咨询
出门在外也鈈愁java简易界面聊天程序源代码_百度文库
两大类熱门资源免费畅读
续费一年阅读会员,立省24元!
文档贡献者贡献于
评价文档:
20页免费6页免费5頁1下载券5页免费8页1下载券 16页免费136页1下载券15页免費61页1下载券7页2下载券
喜欢此文档的还喜欢6页免費15页免费20页免费33页免费5页免费
java简易界面聊天程序源代码|有​界​面​的​简​易​聊​天​程​序​的​源​代​码​,​很​不​錯​哦
把文档贴到Blog、BBS或个人站等:
普通尺寸(450*500pix)
较大呎寸(630*500pix)
大小:6.14KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢}

我要回帖

更多关于 用户界面是什么 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信