Java網(wǎng)絡(luò)編程TCP實現(xiàn)聊天功能
網(wǎng)絡(luò)編程TCP實現(xiàn)聊天的前提還需要掌握IO流,話不多說,直接上代碼!
客戶端:
package com.kuang.lesson02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
//客戶端
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1、要知道服務(wù)器的地址、端口號
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2、創(chuàng)建一個socket連接
socket = new Socket(serverIP, port);
//3、發(fā)送消息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();
}
}
}
}
}
服務(wù)端:
package com.kuang.lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服務(wù)端
public class TcpServerDemo01 {
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());
}
} catch (Exception 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();
}
}
}
}
}
運行結(jié)果:
1、首先運行服務(wù)端,等待接收消息,可以發(fā)現(xiàn)服務(wù)端一直在運行

2、接著運行客戶端,發(fā)送消息,可以發(fā)現(xiàn)客戶端運行結(jié)束

3、返回服務(wù)端查看,可以發(fā)現(xiàn)服務(wù)端已經(jīng)接收到了客戶端發(fā)送來的消息

4、由于是循環(huán)操作,所以只要客戶端發(fā)送消息來,服務(wù)端就能接收到,可以實現(xiàn)多次發(fā)送消息。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC使用@ExceptionHandler注解在Controller中處理異常
這篇文章主要為大家介紹了SpringMVC使用@ExceptionHandler注解在Controller中處理異常示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
Spring Cloud服務(wù)入口Gateway的介紹和使用問題小結(jié)
Spring Cloud Gateway是Spring Cloud的?個全新的API?關(guān)項?, 基于Spring + SpringBoot等技術(shù)開發(fā), ?的是為了替換掉Zuul,這篇文章主要介紹了Spring Cloud服務(wù)入口Gateway的介紹和使用問題小結(jié),需要的朋友可以參考下2025-03-03
解決spring?data?jpa?saveAll()?保存過慢問題
這篇文章主要介紹了解決spring?data?jpa?saveAll()保存過慢問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring Cloud根據(jù)服務(wù)名獲取服務(wù)的ip端口問題
這篇文章主要介紹了Spring Cloud根據(jù)服務(wù)名獲取服務(wù)的ip端口,本篇示例我就以Nacos注冊中心為例了,下面是我注冊的兩個服務(wù),需要的朋友可以參考下2022-09-09
Java編程Post數(shù)據(jù)請求和接收代碼詳解
這篇文章主要介紹了Java編程Post數(shù)據(jù)請求和接收代碼詳解,涉及enctype的三種編碼,post與get等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。2017-11-11

