惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

莫拉维克猫屋

4D Radar - A Novel Sensing Paradigm for 3D Object Detection 粗略的cs231n学习笔记 simulink仿真数字通信系统 基于开源的livego搭建直播服务器 EDA实验二 功能可调综合计时器 EDA实验一 指令运算单元设计——第一次用FPGA开发板 NodeMCU-ESP8266联网获取实时天气并使用lcd1602显示 SSM框架:导出数据库内容到Excel表格 我要这差分放大电路有何用? Hello World
Java网络编程-TCP通信
默认作者 · 2020-04-11 · via 莫拉维克猫屋

概述

TCP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket对象,从而在通信的两端形成网络虚拟链路,一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信。

image-20250107203021180

Java对基于TCP协议的网络提供了良好的封装,使用Socket对象来代表两端的通信端口,并通过Socket产生IO流来进行网络通信。

Java为客户端提供了Socket类,为服务器端提供了ServerSocket类。

接收端

  • 创建服务器端的Socket对象(ServerSocket) : ServerSocket(int port)
  • 监听客户端的连接,返回一个Socket对象 : Socket accept()
  • 获取输入流,读数据: InputStream getInputStream()
  • 释放资源 : void close()
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
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;







public class ServerDemo {
public static void main(String[] args) throws IOException {


ServerSocket ss = new ServerSocket(9000);

Socket accept = ss.accept();

InputStream is = accept.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
String s = new String(bytes, 0, len);
System.out.println("数据是:" + s);


accept.close();
ss.close();
}
}



发送端

  • 创建客户端的Socket对象(Socket) : Socket(String host, int port)
  • 获取输出流,写数据 : OutputStream getOutputStream()
  • 释放资源 : void close()
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
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;







public class ClientDemo {
public static void main(String[] args) throws IOException {



Socket socket = new Socket("192.168.1.63",9000);



OutputStream os = socket.getOutputStream();
os.write("hello world".getBytes());


socket.close();
}
}