Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小雨青年
Java Demo
提交
ec06bbf0
J
Java Demo
项目概览
小雨青年
/
Java Demo
通知
53
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Java Demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
ec06bbf0
编写于
12月 09, 2021
作者:
X
xiaoyu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add net
上级
cd729752
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
112 addition
and
2 deletion
+112
-2
README.md
README.md
+6
-2
src/main/java/com/example/demo/controller/NetController.java
src/main/java/com/example/demo/controller/NetController.java
+72
-0
src/main/java/com/example/demo/tool/Server.java
src/main/java/com/example/demo/tool/Server.java
+34
-0
未找到文件。
README.md
浏览文件 @
ec06bbf0
#
Java Demo
#
参考原文
配套文章
[
【七日阅书】1 计算思维和Java特性《Java程序设计与计算思维》
](
https://blog.csdn.net/diandianxiyu_geek/article/details/121660902?spm=1001.2014.3001.5501
)
[
【七日阅书】2 Java的数据类型和流程控制《Java程序设计与计算思维》
](
https://blog.csdn.net/diandianxiyu_geek/article/details/121681529?spm=1001.2014.3001.5501
)
[
【七日阅书】3 字符串、数组、集合、泛型《Java程序设计与计算思维》
](
https://blog.csdn.net/diandianxiyu_geek/article/details/121703793?spm=1001.2014.3001.5501
)
[
【七日阅书】4 理解面向对象《Java程序设计与计算思维》
](
https://blog.csdn.net/diandianxiyu_geek/article/details/121723434?spm=1001.2014.3001.5501
)
[
【七日阅书】5 抽象和接口、异常处理《Java程序设计与计算思维》
](
https://blog.csdn.net/diandianxiyu_geek/article/details/121767341?spm=1001.2014.3001.5501
)
[
【七日阅书】6 多线程和文件流《Java程序设计与计算思维》
](
https://blog.csdn.net/diandianxiyu_geek/article/details/121797455?spm=1001.2014.3001.5501
)
[
【七日阅书】7 网络通信&全书总评《Java程序设计与计算思维》
](
https://coderfix.blog.csdn.net/article/details/121819159
)
src/main/java/com/example/demo/controller/NetController.java
0 → 100644
浏览文件 @
ec06bbf0
package
com.example.demo.controller
;
import
com.example.demo.tool.Server
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.io.*
;
import
java.net.*
;
@RestController
@RequestMapping
(
"/"
)
public
class
NetController
{
@GetMapping
(
"/net-base"
)
public
void
baseDemo
()
throws
UnknownHostException
{
InetAddress
inetAddress
=
InetAddress
.
getLocalHost
();
System
.
out
.
print
(
inetAddress
.
getHostAddress
());
System
.
out
.
print
(
"\r\n"
);
System
.
out
.
print
(
inetAddress
.
getHostName
());
}
@GetMapping
(
"/socket"
)
public
void
socketDemo
(){
Server
server
;
int
port
=
1024
;
BufferedReader
bufferedReader
;
PrintWriter
printWriter
;
server
=
new
Server
(
port
);
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
server
.
inputStream
));
printWriter
=
new
PrintWriter
(
new
OutputStreamWriter
(
server
.
outputStream
));
}
@GetMapping
(
"/udp-r"
)
public
void
udpDemo
()
throws
IOException
{
DatagramPacket
datagramPacket
;
DatagramSocket
datagramSocket
;
byte
[]
buffer
=
new
byte
[
20
];
String
msg
;
System
.
out
.
print
(
"服务端开始接收数据"
);
while
(
true
){
datagramPacket
=
new
DatagramPacket
(
buffer
,
buffer
.
length
);
datagramSocket
=
new
DatagramSocket
(
9995
);
datagramSocket
.
receive
(
datagramPacket
);
msg
=
new
String
(
buffer
,
0
,
datagramPacket
.
getLength
());
System
.
out
.
print
(
"信息为:"
+
msg
+
"\r\n"
);
datagramSocket
.
close
();
}
}
@GetMapping
(
"/udp-s"
)
public
void
udpSendDemo
()
throws
IOException
{
System
.
out
.
print
(
"发送消息\r\n"
);
String
msg
=
"msg 123232"
;
byte
[]
buffer
=
msg
.
getBytes
();
DatagramPacket
datagramPacket
=
new
DatagramPacket
(
buffer
,
buffer
.
length
,
InetAddress
.
getByName
(
"127.0.0.1"
),
9995
);
DatagramSocket
datagramSocket
=
new
DatagramSocket
();
datagramSocket
.
send
(
datagramPacket
);
datagramSocket
.
close
();
}
@GetMapping
(
"/url"
)
public
void
urlDemo
()
throws
MalformedURLException
{
URL
url
=
new
URL
(
"https://blog.csdn.net/diandianxiyu_geek"
);
System
.
out
.
print
(
url
.
getProtocol
()
+
"\r\n"
);
System
.
out
.
print
(
url
.
getPath
()
+
"\r\n"
);
System
.
out
.
print
(
url
.
getHost
()
+
"\r\n"
);
System
.
out
.
print
(
url
.
getFile
()
+
"\r\n"
);
System
.
out
.
print
(
url
.
getQuery
()
+
"\r\n"
);
System
.
out
.
print
(
url
.
getRef
()
+
"\r\n"
);
}
}
src/main/java/com/example/demo/tool/Server.java
0 → 100644
浏览文件 @
ec06bbf0
package
com.example.demo.tool
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
import
java.nio.charset.StandardCharsets
;
public
class
Server
{
ServerSocket
serverSocket
;
Socket
socket
;
public
InputStream
inputStream
;
public
OutputStream
outputStream
;
public
Server
(
int
port
){
try
{
serverSocket
=
new
ServerSocket
(
port
);
while
(
true
){
socket
=
serverSocket
.
accept
();
inputStream
=
socket
.
getInputStream
();
outputStream
=
socket
.
getOutputStream
();
String
line
=
System
.
getProperty
(
"line.separator"
);
String
out
=
"服务器信息:"
+
line
+
serverSocket
.
getInetAddress
().
getHostName
()
+
line
+
serverSocket
.
getLocalPort
();
outputStream
.
write
(
out
.
getBytes
(
StandardCharsets
.
UTF_8
));
}
}
catch
(
IOException
exception
)
{
System
.
err
.
print
(
exception
.
toString
());
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录