Java 網(wǎng)絡(luò)編程之 TCP 實(shí)現(xiàn)簡(jiǎn)單的聊天系統(tǒng)
客戶端
1、連接服務(wù)器 Socket
2、發(fā)送消息
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、要知道服務(wù)器的地址 端口號(hào) InetAddress serverIP = InetAddress.getByName("127.0.0.1"); int port = 9999; //2、創(chuàng)建一個(gè) socket 連接 socket = new Socket(serverIP, port); //3、發(fā)送消息 IO流 os = socket.getOutputStream(); os.write("你好,歡迎學(xué)習(xí)狂神學(xué)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(); } } } } }
服務(wù)端
1、建立服務(wù)的端口 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; /** * 服務(wù)端 */ public class TcpServerDemo1 { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1、我得有一個(gè)地址 serverSocket = new ServerSocket(9999); while (true){ //2、等待客戶端連接過(guò)來(lái) 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 { //關(guān)閉資源 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(); } } } } }
服務(wù)端
1、建立服務(wù)的端口 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; /** * 服務(wù)端 */ public class TcpServerDemo1 { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1、我得有一個(gè)地址 serverSocket = new ServerSocket(9999); while (true){ //2、等待客戶端連接過(guò)來(lái) 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 { //關(guān)閉資源 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 網(wǎng)絡(luò)編程之 TCP 實(shí)現(xiàn)簡(jiǎn)單的聊天系統(tǒng)的詳細(xì)內(nèi)容,更多關(guān)于Java 實(shí)現(xiàn)簡(jiǎn)單的聊天系統(tǒng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 基于Java實(shí)現(xiàn)互聯(lián)網(wǎng)實(shí)時(shí)聊天系統(tǒng)(附源碼)
- Java?NIO實(shí)現(xiàn)聊天系統(tǒng)
- Java Socket實(shí)現(xiàn)多人聊天系統(tǒng)
- 基于Java網(wǎng)絡(luò)編程和多線程的多對(duì)多聊天系統(tǒng)
- Java網(wǎng)絡(luò)編程UDP實(shí)現(xiàn)多線程在線聊天
- Java網(wǎng)絡(luò)編程實(shí)例——簡(jiǎn)單模擬在線聊天
- Java使用TCP實(shí)現(xiàn)在線聊天的示例代碼
- Java GUI編程實(shí)現(xiàn)在線聊天室
- Java中使用websocket實(shí)現(xiàn)在線聊天功能
- java實(shí)現(xiàn)在線聊天系統(tǒng)
相關(guān)文章
Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解
這篇文章主要為大家介紹了Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08如何解決SpringMVC不能訪問(wèn)html頁(yè)面
這篇文章主要介紹了如何解決SpringMVC不能訪問(wèn)html頁(yè)面問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09淺談Java由于不當(dāng)?shù)膱?zhí)行順序?qū)е碌乃梨i
為了保證線程的安全,我們引入了加鎖機(jī)制,但是如果不加限制的使用加鎖,就有可能會(huì)導(dǎo)致順序死鎖(Lock-Ordering Deadlock)。本文將會(huì)討論一下順序死鎖的問(wèn)題。2021-06-06Opencv實(shí)現(xiàn)身份證OCR識(shí)別的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何使用Opencv實(shí)現(xiàn)身份證OCR識(shí)別功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2024-03-03java操作mongodb時(shí),對(duì)象bean和DBObject相互轉(zhuǎn)換的方法(推薦)
下面小編就為大家?guī)?lái)一篇java操作mongodb時(shí),對(duì)象bean和DBObject相互轉(zhuǎn)換的方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11