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

推荐订阅源

T
Tenable Blog
H
Heimdal Security Blog
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Schneier on Security
G
GRAHAM CLULEY
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
C
Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
P
Proofpoint News Feed
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
S
Security Affairs
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
N
News | PayPal Newsroom
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
NISL@THU
NISL@THU

莫拉维克猫屋

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();
}
}