Java實(shí)現(xiàn)TCP互發(fā)消息
本文實(shí)例為大家分享了Java實(shí)現(xiàn)TCP互發(fā)消息的具體代碼,供大家參考,具體內(nèi)容如下
TCP客戶端:
package tcp;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) {
Socket socket =null;
OutputStream os =null;
try {
//創(chuàng)建socket對(duì)象,指明服務(wù)器端的ip和端口號(hào)
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 8888);
//獲取一個(gè)輸出流,用于輸出數(shù)據(jù)
os = socket.getOutputStream();
//寫(xiě)出數(shù)據(jù)的操作
os.write("你好,我是客戶端".getBytes());
}catch(IOException e){
e.printStackTrace();
}finally {
//資源的關(guān)閉
if(os!=null){
try{
os.close();
}catch (IOException e){
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
TCP服務(wù)端:
package tcp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
class TcpServer{
public static void main(String[] args) {
ServerSocket ss=null;
Socket socket=null;
InputStream is=null;
ByteArrayOutputStream baos =null;
try {
//創(chuàng)建服務(wù)器端的ServerSocket,指明自己的端口
ss = new ServerSocket(8888);
//調(diào)用accept()表示接收來(lái)自于客戶端的socket
socket = ss.accept();
//獲取輸入流中的數(shù)據(jù)
is = socket.getInputStream();
/*讀取輸入流中的數(shù)據(jù)(ByteArrayOutputStream可以把字節(jié)一次性記錄下來(lái),
這樣就可以避免一些字符的字節(jié)碼不一致導(dǎo)致發(fā)送后解析出現(xiàn)亂碼;
ByteArrayOutputStream的功能與StringBuilder的作用有異曲同工之妙。)
*/
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
}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 (ss!=null){
try {
ss.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
注意:在Intellij idea中運(yùn)行時(shí),需先打開(kāi)兩個(gè)端的平行運(yùn)行設(shè)置,操作如下:


最后的運(yùn)行結(jié)果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
構(gòu)建springboot自動(dòng)生成mapper文件和dao接口項(xiàng)目的步驟和配置方法
這篇文章主要介紹了構(gòu)建springboot自動(dòng)生成mapper文件和dao接口項(xiàng)目的步驟和配置方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
SpringBoot中ApplicationEvent和ApplicationListener用法小結(jié)
這篇文章介紹SpringBoot中ApplicationEvent用法,注意ApplicationEvent和MQ隊(duì)列雖然實(shí)現(xiàn)的功能相似,但是MQ還是有其不可替代性的,最本質(zhì)的區(qū)別就是MQ可以用于不同系統(tǒng)之間的消息發(fā)布,而SpringEvent這種模式只能在一個(gè)系統(tǒng)中,需要的朋友可以參考下2023-03-03
SpringBoot整合數(shù)據(jù)庫(kù)訪問(wèn)層的實(shí)戰(zhàn)
本文主要介紹了SpringBoot整合數(shù)據(jù)庫(kù)訪問(wèn)層的實(shí)戰(zhàn),主要包含JdbcTemplate和mybatis框架的整合應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下2022-03-03
Java漢字轉(zhuǎn)拼音工具類完整代碼實(shí)例
這篇文章主要介紹了java漢字轉(zhuǎn)拼音工具類完整代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java項(xiàng)目中添加外部jar包的兩種方式(收藏版)
這篇文章主要介紹了java項(xiàng)目中添加外部jar包的兩種方式,第二種方式是將外部jar包引入到本地maven倉(cāng)庫(kù)中,本文給大家講解的非常詳細(xì),需要的朋友可以參考下2023-03-03
Spring Boot Actuator執(zhí)行器運(yùn)行原理詳解
這篇文章主要介紹了Spring Boot Actuator執(zhí)行器運(yùn)行原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

