提交 ec06bbf0 编写于 作者: X xiaoyu

add net

上级 cd729752
# 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)
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");
}
}
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.
先完成此消息的编辑!
想要评论请 注册