午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

Java 網絡編程之 TCP 實現簡單的聊天系統

瀏覽:115日期:2022-08-20 18:08:45

客戶端

1、連接服務器 Socket

2、發送消息

package lesson02;import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;/** * 客戶端 */public class TcpClientDemo1 { public static void main(String[] args) { Socket socket = null; OutputStream os = null; try { //1、要知道服務器的地址 端口號 InetAddress serverIP = InetAddress.getByName('127.0.0.1'); int port = 9999; //2、創建一個 socket 連接 socket = new Socket(serverIP, port); //3、發送消息 IO流 os = socket.getOutputStream(); os.write('你好,歡迎學習狂神學Java'.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null){try { os.close();} catch (IOException e) { e.printStackTrace();} } if (socket != null){try { socket.close();} catch (IOException e) { e.printStackTrace();} } } }}

服務端

1、建立服務的端口 ServerSocket

2、等待用戶的連接 accept

3、接收用戶的消息

package lesson02;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;/** * 服務端 */public class TcpServerDemo1 { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1、我得有一個地址 serverSocket = new ServerSocket(9999); while (true){//2、等待客戶端連接過來socket = serverSocket.accept();//3、讀取客戶端的消息is = socket.getInputStream();//管道流baos = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len;while((len = is.read(buffer)) != -1){ baos.write(buffer, 0 , len);}System.out.println(baos.toString()); } /* byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1){String msg = new String(buffer, 0, len);System.out.println(msg); } */ } catch (IOException e) { e.printStackTrace(); } finally { //關閉資源 if (baos != null){try { baos.close();} catch (IOException e) { e.printStackTrace();} } if (is != null){try { is.close();} catch (IOException e) { e.printStackTrace();} } if (socket != null){try { socket.close();} catch (IOException e) { e.printStackTrace();} } if (serverSocket != null){try { serverSocket.close();} catch (IOException e) { e.printStackTrace();} } } }}

服務端

1、建立服務的端口 ServerSocket

2、等待用戶的連接 accept

3、接收用戶的消息

package lesson02;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;/** * 服務端 */public class TcpServerDemo1 { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1、我得有一個地址 serverSocket = new ServerSocket(9999); while (true){//2、等待客戶端連接過來socket = serverSocket.accept();//3、讀取客戶端的消息is = socket.getInputStream();//管道流baos = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len;while((len = is.read(buffer)) != -1){ baos.write(buffer, 0 , len);}System.out.println(baos.toString()); } /* byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1){String msg = new String(buffer, 0, len);System.out.println(msg); } */ } catch (IOException e) { e.printStackTrace(); } finally { //關閉資源 if (baos != null){try { baos.close();} catch (IOException e) { e.printStackTrace();} } if (is != null){try { is.close();} catch (IOException e) { e.printStackTrace();} } if (socket != null){try { socket.close();} catch (IOException e) { e.printStackTrace();} } if (serverSocket != null){try { serverSocket.close();} catch (IOException e) { e.printStackTrace();} } } }}

以上就是Java 網絡編程之 TCP 實現簡單的聊天系統的詳細內容,更多關于Java 實現簡單的聊天系統的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 中文一区二区 | 精品中文视频 | 久一久久| 玖玖久久 | 偷偷操不一样 | 91禁男男在线观看 | 一区三区视频 | 亚洲精品乱码久久久久久日本蜜臀 | 黄91在线观看 | 良辰之屋在线观看 | 日韩在线第一 | 国产精品久久久久久妇女6080 | 日韩av男人天堂 | 欧美激情免费视频 | 五月婷婷av | 亚洲一区二区视频在线播放 | 久久最新免费视频 | 久草视频手机在线 | 久久99精品波多结衣一区 | 狠狠的干 | 高清不卡av | 国产一区不卡在线 | 爽妇综合网 | 久操精品视频 | 亚洲成人动漫在线观看 | 四虎永久在线精品 | 四虎影院在线观看免费 | 婷婷在线视频观看 | 欧美大胆视频 | 久久久免费高清视频 | 欧洲做受高潮欧美裸体艺术 | 亚洲欧洲日本在线 | 国产一区黄色 | 国产无遮挡免费视频 | 中文字幕一区不卡 | 亚洲成av| 亚洲高清一区二区三区 | 日韩在线中文 | 一级黄色免费观看 | 国产88av| 天天干视频 |