💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。
- 推荐:kuan 的首页,持续学习,不断总结,共同进步,活到老学到老
- 导航
非常期待和您一起在这个小小的网络世界里共同探索、学习和成长。💝💝💝 ✨✨ 欢迎订阅本专栏 ✨✨
博客目录
1.InetAddress
InetAddress常用方法如下:
java
public static void main(String[] args) throws Exception {
//获取本机地址信息
InetAddress localIp = InetAddress.getLocalHost();
log.info("localIp.getCanonicalHostName()=" + localIp.getCanonicalHostName());//localhost
log.info("localIp.getHostAddress()=" + localIp.getHostAddress());//127.0.0.1
log.info("localIp.getHostName()=" + localIp.getHostName());//qinyingjiedeMacBook-Pro.local
log.info("localIp.toString()=" + localIp.toString());//qinyingjiedeMacBook-Pro.local/127.0.0.1
log.info("localIp.isReachable(5000)=" + localIp.isReachable(3000));//true
//获取指定域名地址信息
log.info("====================================");
InetAddress baiduIp = InetAddress.getByName("www.baidu.com");
log.info("baiduIp.getCanonicalHostName()=" + baiduIp.getCanonicalHostName());//14.119.104.189
log.info("baiduIp.getHostAddress()=" + baiduIp.getHostAddress());//14.119.104.189
log.info("baiduIp.getHostName()=" + baiduIp.getHostName());//www.baidu.com
log.info("baiduIp.toString()=" + baiduIp.toString());//www.baidu.com/14.119.104.189
log.info("baiduIp.isReachable(5000)=" + baiduIp.isReachable(5000));//false
log.info("====================================");
//获取指定原始IP地址信息
InetAddress ip = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
log.info("ip.getCanonicalHostName()=" + ip.getCanonicalHostName());//localhost
log.info("ip.getHostAddress()= " + ip.getHostAddress());//127.0.0.1
log.info("ip.getHostName()=" + ip.getHostName());//localhost
log.info("ip.toString()=" + ip.toString());//localhost/127.0.0.1
log.info("ip.isReachable(5000)=" + ip.isReachable(5000));//true
}
2.socket 套接字
客户端
java
public class Basic_001_Client {
public static void main(String[] args) throws Exception {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(inetAddress, port);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("你好".getBytes());
outputStream.close();
socket.close();
}
}
服务端
java
@Slf4j
public class Basic_002_Server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(9999);
while (true) {
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
log.info(byteArrayOutputStream.toString());
byteArrayOutputStream.close();
inputStream.close();
socket.close();
}
}
}
3.文件上传下载
客户端
java
public class File_001_client {
public static void main(String[] args) throws Exception {
//1.创建一个socket
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.文件流
final FileInputStream fileIs = new FileInputStream("/Users/qinyingjie/Documents/idea-workspace/ant/ant-netty/src/main/java/Electron.png");
//4.写出文件
final byte[] buffer = new byte[1024];
int len = 0;
while ((len = fileIs.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
//5.关闭资源
fileIs.close();
os.close();
socket.close();
}
}
服务端
java
public class File_002_server {
public static void main(String[] args) throws Exception {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端连接
Socket socket = serverSocket.accept();
//3.获取输入流
InputStream inputStream = socket.getInputStream();
//4.文件输出
FileOutputStream fileOut = new FileOutputStream("/Users/qinyingjie/Documents/idea-workspace/ant/ant-netty/src/main/java/receive.png");
final byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOut.write(buffer, 0, len);
}
//5.关闭资源
fileOut.close();
inputStream.close();
socket.close();
}
}
4.UDP
udp 没有客户端服务端的概念,主要是为了演示
客户端
java
public class UDP_001_client {
public static void main(String[] args) throws Exception {
//1.创建一个socket
DatagramSocket socket = new DatagramSocket();
//2.建立一个包
String msg = "你好";
final InetAddress inetAddress = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, inetAddress, port);
//3.发送
socket.send(packet);
//4.关闭资源
socket.close();
}
}
服务端
java
@Slf4j
public class UDP_002_server {
public static void main(String[] args) throws Exception {
//1.创建socket
DatagramSocket socket = new DatagramSocket(9090);
//2.接收数据包
final byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
log.info(packet.getAddress().getHostAddress());
log.info(new String(packet.getData(), 0, packet.getLength()));
//3.关闭资源
socket.close();
}
}
5.chat
send 方
java
public class Chat_001_send {
public static void main(String[] args) throws Exception {
//1.创建一个socket
DatagramSocket socket = new DatagramSocket(8888);
//2.控制台输入
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
final String data = reader.readLine();
final byte[] datas = data.getBytes();
final DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
//3.发送
socket.send(packet);
if (StringUtils.equalsIgnoreCase(data, "bye")) {
break;
}
}
//4.关闭资源
socket.close();
}
}
receive 方
java
@Slf4j
public class Chat_002_receive {
public static void main(String[] args) throws Exception {
//1.创建socket
DatagramSocket socket = new DatagramSocket(6666);
while (true) { //2.接收数据包
final byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
//断开连接 bye
final byte[] data = packet.getData();
final String receive = new String(data, 0, packet.getLength());
log.info(receive);
if (StringUtils.equalsIgnoreCase(receive, "bye")) {
break;
}
}
//3.关闭资源
socket.close();
}
}
6.双人交流
java
@Slf4j
public class TalkReceive implements Runnable {
private DatagramSocket socket;
private final int port;
private final String msgFrom;
@SneakyThrows
public TalkReceive(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
this.socket = new DatagramSocket(port);
}
@SneakyThrows
@Override
public void run() {
//1.创建socket
while (true) { //2.接收数据包
final byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
//断开连接 bye
final byte[] data = packet.getData();
final String receive = new String(data, 0, packet.getLength());
log.info(msgFrom + ": " + receive);
if (StringUtils.equalsIgnoreCase(receive, "bye")) {
break;
}
}
//3.关闭资源
socket.close();
}
}
java
public class TalkSend implements Runnable {
DatagramSocket socket;
BufferedReader reader;
private final int fromPort;
private final String toIp;
private final int toPort;
public TalkSend(int fromPort, String toIp, int toPort) throws Exception {
this.fromPort = fromPort;
this.toIp = toIp;
this.toPort = toPort;
//1.创建一个socket
this.socket = new DatagramSocket(fromPort);
//2.控制台输入
this.reader = new BufferedReader(new InputStreamReader(System.in));
}
@SneakyThrows
@Override
public void run() {
while (true) {
final String data = reader.readLine();
final byte[] datas = data.getBytes();
final DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIp, this.toPort));
//3.发送
socket.send(packet);
if (StringUtils.equalsIgnoreCase(data, "bye")) {
break;
}
}
//4.关闭资源
socket.close();
}
}
java
public class TalkStuden {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(7777, "localhost", 9999)).start();
new Thread(new TalkReceive(8888, "老师")).start();
}
}
public class TalkTeacher {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(5555, "localhost", 8888)).start();
new Thread(new TalkReceive(9999, "学生")).start();
}
}
7.URL
java
@Slf4j
public class UrlDemo {
public static void main(String[] args) throws Exception {
final URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=kuangshen&password=123");
log.info(url.getAuthority());//localhost:8080
log.info(url.getPath());///helloworld/index.jsp
log.info(url.getProtocol());//http
log.info(url.getHost());//localhost
log.info(url.getFile());///helloworld/index.jsp?username=kuangshen&password=123
log.info(url.getUserInfo());//null
log.info(url.getQuery());//username=kuangshen&password=123
log.info(url.getRef());//null
log.info(String.valueOf(url.getDefaultPort()));//80
log.info((String) url.getContent());
}
}
觉得有用的话点个赞
👍🏻
呗。❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!😄😄😄
💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍
🔥🔥🔥Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧!🌙🌙🌙