激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java聊天室的實現(xiàn)代碼

java聊天室的實現(xiàn)代碼

2021-05-20 13:37北漂程序員-阿力 Java教程

這篇文章主要為大家詳細(xì)介紹了java聊天室的實現(xiàn)代碼,一個多客戶端聊天室,支持多客戶端聊天,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現(xiàn)聊天室的具體代碼,供大家參考,具體內(nèi)容如下

聊天室界面:

java聊天室的實現(xiàn)代碼

源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
public class clientframe extends frame {
 
 private textfield textfieldcontent = new textfield();
 private textarea textareacontent = new textarea();
 private socket socket = null;
 private outputstream out = null;
 private dataoutputstream dos = null;
 private inputstream in = null;
 private datainputstream dis = null;
 private boolean flag = false;
 
 /**
  * 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午9:19:51 功能:啟動客戶端程序
  *
  * @param args
  */
 public static void main(string[] args) {
  new clientframe().init();
 }
 
 /**
  * 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午9:20:43 功能:對窗口進(jìn)行初始化
  */
 private void init() {
  this.setsize(300, 300);
  setlocation(250, 150);
  setvisible(true);
  settitle("wechatroom");
 
  // 添加控件
  this.add(textareacontent);
  this.add(textfieldcontent, borderlayout.south);
  textareacontent.setfocusable(false);
  pack();
 
  // 關(guān)閉事件
  addwindowlistener(new windowadapter() {
   public void windowclosing(windowevent e) {
    system.out.println("用戶試圖關(guān)閉窗口");
    disconnect();
    system.exit(0);
   }
 
  });
  // textfieldcontent添加回車事件
  textfieldcontent.addactionlistener(new actionlistener() {
   public void actionperformed(actionevent e) {
    onclickenter();
   }
  });
 
  // 建立連接
  connect();
  new thread(new recivemessage()).start();
 }
 
 private class recivemessage implements runnable {
  @override
  public void run() {
   flag = true;
   try {
    while (flag) {
     string message = dis.readutf();
     textareacontent.append(message + "\n");
    }
   } catch (eofexception e) {
    flag = false;
    system.out.println("客戶端已關(guān)閉");
    // e.printstacktrace();
   } catch (socketexception e) {
    flag = false;
    system.out.println("客戶端已關(guān)閉");
    // e.printstacktrace();
   } catch (ioexception e) {
    flag = false;
    system.out.println("接受消息失敗");
    e.printstacktrace();
   }
  }
 
 }
 
 /**
  * 功能:當(dāng)點擊回車時出發(fā)的事件 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午9:49:30
  */
 private void onclickenter() {
  string message = textfieldcontent.gettext().trim();
  if (message != null && !message.equals("")) {
   string time = new simpledateformat("h:m:s").format(new date());
   textareacontent.append(time + "\n" + message + "\n");
   textfieldcontent.settext("");
   sendmessagetoserver(message);
  }
 }
 
 /**
  * 功能:給服務(wù)器發(fā)送消息 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:13:48
  *
  * @param message
  */
 private void sendmessagetoserver(string message) {
  try {
   dos.writeutf(message);
   dos.flush();
  } catch (ioexception e) {
   system.out.println("發(fā)送消息失敗");
   e.printstacktrace();
  }
 }
 
 /**
  * 功能:申請socket鏈接 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:00:38
  */
 private void connect() {
  try {
   socket = new socket("localhost", 8888);
   out = socket.getoutputstream();
   dos = new dataoutputstream(out);
   in = socket.getinputstream();
   dis = new datainputstream(in);
  } catch (unknownhostexception e) {
   system.out.println("申請鏈接失敗");
   e.printstacktrace();
  } catch (ioexception e) {
   system.out.println("申請鏈接失敗");
   e.printstacktrace();
  }
 }
 
 /**
  * 功能:關(guān)閉流和鏈接 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:01:32
  */
 private void disconnect() {
  flag = false;
  if (dos != null) {
   try {
    dos.close();
   } catch (ioexception e) {
    system.out.println("dos關(guān)閉失敗");
    e.printstacktrace();
   }
  }
  if (out != null) {
   try {
    out.close();
   } catch (ioexception e) {
    system.out.println("dos關(guān)閉失敗");
    e.printstacktrace();
   }
  }
  if (socket != null) {
   try {
    socket.close();
   } catch (ioexception e) {
    system.out.println("socket關(guān)閉失敗");
    e.printstacktrace();
   }
   ;
  }
 }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.chat;
 
import java.io.datainputstream;
import java.io.dataoutputstream;
import java.io.eofexception;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.net.bindexception;
import java.net.serversocket;
import java.net.socket;
import java.net.socketexception;
import java.util.arraylist;
import java.util.list;
 
public class chatserver {
 
 private list<client> clients = new arraylist<>();
 
 /**
  * 功能:啟動chatsever 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:26:41
  *
  * @param args
  */
 public static void main(string[] args) {
  new chatserver().init();
 }
 
 /**
  * 功能:對chatserver初始化 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:27:09
  */
 private void init() {
  system.out.println("服務(wù)器已開啟");
  // bindexception
 
  serversocket ss = null;
  socket socket = null;
  try {
   ss = new serversocket(8888);
  } catch (bindexception e) {
   system.out.println("端口已被占用");
   e.printstacktrace();
  } catch (ioexception e1) {
   e1.printstacktrace();
  }
  try {
   client client = null;
   while (true) {
    socket = ss.accept();
    system.out.println("客戶駕到");
    client = new client(socket);
    clients.add(client);
    new thread(client).start();
   }
  } catch (ioexception e) {
   e.printstacktrace();
  }
 }
 
 private class client implements runnable {
  private socket socket = null;
  inputstream in = null;
  datainputstream din = null;
  outputstream out = null;
  dataoutputstream dos = null;
  boolean flag = true;
 
  public client(socket socket) {
   this.socket = socket;
   try {
    in = socket.getinputstream();
    din = new datainputstream(in);
   } catch (ioexception e) {
    system.out.println("接受消息失敗");
    e.printstacktrace();
   }
 
  }
 
  public void run() {
 
   string message;
   try {
    while (flag) {
     message = din.readutf();
     // system.out.println("客戶說:" + message);
     forwordtoallclients(message);
    }
   } catch (socketexception e) {
    flag = false;
    system.out.println("客戶下線");
    clients.remove(this);
    // e.printstacktrace();
   } catch (eofexception e) {
    flag = false;
    system.out.println("客戶下線");
    clients.remove(this);
    // e.printstacktrace();
   } catch (ioexception e) {
    flag = false;
    system.out.println("接受消息失敗");
    clients.remove(this);
    e.printstacktrace();
   }
 
   if (din != null) {
    try {
     din.close();
    } catch (ioexception e) {
     system.out.println("din關(guān)閉失敗");
     e.printstacktrace();
    }
   }
   if (in != null) {
    try {
     in.close();
    } catch (ioexception e) {
     system.out.println("din關(guān)閉失敗");
     e.printstacktrace();
    }
   }
   if (socket != null) {
    try {
     socket.close();
    } catch (ioexception e) {
     system.out.println("din關(guān)閉失敗");
     e.printstacktrace();
    }
   }
 
  }
 
  /**
   * 功能:轉(zhuǎn)發(fā)給所有客戶端 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午11:11:59
   *
   * @param message
   * @throws ioexception
   */
  private void forwordtoallclients(string message) throws ioexception {
   for (client c : clients) {
    if (c != this) {
     out = c.socket.getoutputstream();
     dos = new dataoutputstream(out);
     forwordtoclient(message);
    }
   }
  }
 
  /**
   * 功能:發(fā)送給一個客戶端 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午11:16:12
   *
   * @throws ioexception
   */
  private void forwordtoclient(string message) throws ioexception {
   dos.writeutf(message);
   dos.flush();
   system.out.println("轉(zhuǎn)發(fā)成功!");
  }
 
 }
}

源碼下載:java聊天室代碼

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/qq_24082497/article/details/51347426

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: a在线视频| 久久精品国产99国产精品澳门 | 亚洲国产综合在线观看 | 国产自在自线午夜精品视频在 | 日韩精品中文字幕在线观看 | 成人午夜免费观看 | 成人毛片在线 | 国产免费最爽的乱淫视频a 毛片国产 | 91视频精选| 特级毛片a级毛片100免费 | 欧美中文在线 | 性欧美xxxx免费岛国不卡电影 | 中国漂亮护士一级a毛片 | 国产一级aaa全黄毛片 | 欧美a视频在线观看 | 中文日韩欧美 | 91精品国产91热久久久做人人 | 中文字幕一二三区芒果 | 4480午夜 | 亚洲欧美不卡视频 | 欧美一区二区精品夜夜嗨 | 91小视频在线观看免费版高清 | av在线久草 | 成人h精品动漫一区二区三区 | 国产午夜电影在线观看 | 国产69精品久久久久久 | 国产免费高清在线视频 | 中文字幕精品在线视频 | 深夜免费观看视频 | 一区二区高清视频在线观看 | 色偷偷一区 | 羞羞视频在线免费 | 成人免费福利网站 | 久草在线视频看看 | 最新黄色毛片 | 在线成人免费视频 | 久久久久久亚洲综合影院红桃 | 欧美视频在线一区二区三区 | 免费国产一级淫片 | 成人勉费视频 | 一级毛片播放 |