💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。
- 推荐:kuan 的首页,持续学习,不断总结,共同进步,活到老学到老
- 导航
- 檀越剑指大厂系列:全面总结 java 核心技术点,如集合,jvm,并发编程 redis,kafka,Spring,微服务,Netty 等
- 常用开发工具系列:罗列常用的开发工具,如 IDEA,Mac,Alfred,electerm,Git,typora,apifox 等
- 数据库系列:详细总结了常用数据库 mysql 技术点,以及工作中遇到的 mysql 问题等
- 懒人运维系列:总结好用的命令,解放双手不香吗?能用一个命令完成绝不用两个操作
- 数据结构与算法系列:总结数据结构和算法,不同类型针对性训练,提升编程思维,剑指大厂
非常期待和您一起在这个小小的网络世界里共同探索、学习和成长。💝💝💝 ✨✨ 欢迎订阅本专栏 ✨✨
博客目录
- 1.InetAddress
- 2.socket 套接字
- 3.文件上传下载
- 4.UDP
- 5.chat
- 6.双人交流
- 7.URL
1.InetAddress
InetAddress常用方法如下:
public static void main(String[] args) throws Exception {//获取本机地址信息InetAddress localIp = InetAddress.getLocalHost();log.info("localIp.getCanonicalHostName()=" + localIp.getCanonicalHostName());//localhostlog.info("localIp.getHostAddress()=" + localIp.getHostAddress());//127.0.0.1log.info("localIp.getHostName()=" + localIp.getHostName());//qinyingjiedeMacBook-Pro.locallog.info("localIp.toString()=" + localIp.toString());//qinyingjiedeMacBook-Pro.local/127.0.0.1log.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.189log.info("baiduIp.getHostAddress()=" + baiduIp.getHostAddress());//14.119.104.189log.info("baiduIp.getHostName()=" + baiduIp.getHostName());//www.baidu.comlog.info("baiduIp.toString()=" + baiduIp.toString());//www.baidu.com/14.119.104.189log.info("baiduIp.isReachable(5000)=" + baiduIp.isReachable(5000));//falselog.info("====================================");//获取指定原始IP地址信息InetAddress ip = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});log.info("ip.getCanonicalHostName()=" + ip.getCanonicalHostName());//localhostlog.info("ip.getHostAddress()= " + ip.getHostAddress());//127.0.0.1log.info("ip.getHostName()=" + ip.getHostName());//localhostlog.info("ip.toString()=" + ip.toString());//localhost/127.0.0.1log.info("ip.isReachable(5000)=" + ip.isReachable(5000));//true
}
2.socket 套接字
客户端
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();}
}
服务端
@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.文件上传下载
客户端
public class File_001_client {public static void main(String[] args) throws Exception {//1.创建一个socketSocket 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();}
}
服务端
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 没有客户端服务端的概念,主要是为了演示
客户端
public class UDP_001_client {public static void main(String[] args) throws Exception {//1.创建一个socketDatagramSocket 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();}
}
服务端
@Slf4j
public class UDP_002_server {public static void main(String[] args) throws Exception {//1.创建socketDatagramSocket 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 方
public class Chat_001_send {public static void main(String[] args) throws Exception {//1.创建一个socketDatagramSocket 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 方
@Slf4j
public class Chat_002_receive {public static void main(String[] args) throws Exception {//1.创建socketDatagramSocket socket = new DatagramSocket(6666);while (true) { //2.接收数据包final byte[] buffer = new byte[1024];DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);socket.receive(packet);//断开连接 byefinal 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.双人交流
@Slf4j
public class TalkReceive implements Runnable {private DatagramSocket socket;private final int port;private final String msgFrom;@SneakyThrowspublic TalkReceive(int port, String msgFrom) {this.port = port;this.msgFrom = msgFrom;this.socket = new DatagramSocket(port);}@SneakyThrows@Overridepublic void run() {//1.创建socketwhile (true) { //2.接收数据包final byte[] buffer = new byte[1024];DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);socket.receive(packet);//断开连接 byefinal 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();}
}
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.创建一个socketthis.socket = new DatagramSocket(fromPort);//2.控制台输入this.reader = new BufferedReader(new InputStreamReader(System.in));}@SneakyThrows@Overridepublic 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();}
}
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
@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:8080log.info(url.getPath());///helloworld/index.jsplog.info(url.getProtocol());//httplog.info(url.getHost());//localhostlog.info(url.getFile());///helloworld/index.jsp?username=kuangshen&password=123log.info(url.getUserInfo());//nulllog.info(url.getQuery());//username=kuangshen&password=123log.info(url.getRef());//nulllog.info(String.valueOf(url.getDefaultPort()));//80log.info((String) url.getContent());}
}
觉得有用的话点个赞
👍🏻
呗。
❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!😄😄😄💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍
🔥🔥🔥Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧!🌙🌙🌙