💝💝💝歡迎來到我的博客,很高興能夠在這里和您見面!希望您在這里可以感受到一份輕松愉快的氛圍,不僅可以獲得有趣的內容和知識,也可以暢所欲言、分享您的想法和見解。
- 推薦: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 道阻且長,行則將至,讓我們一起加油吧!🌙🌙🌙