package com.lxf.network;
import java.net.InetAddress;
public class NetDemo {
public static void main(String[] args) {
try {
//查询地址
InetAddress inetAddress1 =InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2 =InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
InetAddress inetAddress3 =InetAddress.getByName("localhost");
System.out.println(inetAddress3);
InetAddress inetAddress4 =InetAddress.getLocalHost();
System.out.println(inetAddress4);
System.out.println(inetAddress2.getCanonicalHostName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
C:\Users\82127>netstat | findstr "63342"
TCP 127.0.0.1:52375 kubernetes:63342 ESTABLISHED
TCP 127.0.0.1:63342 kubernetes:52375 ESTABLISHED
C:\Users\82127>netstat | findstr "80"
TCP 127.0.0.1:51279 kubernetes:51280 ESTABLISHED
TCP 127.0.0.1:51280 kubernetes:51279 ESTABLISHED
C:\Users\82127>netstat | findstr 80
TCP 127.0.0.1:51279 kubernetes:51280 ESTABLISHED
TCP 127.0.0.1:51280 kubernetes:51279 ESTABLISHED
C:\Users\82127>netstat | findstr ^80
TCP 127.0.0.1:51279 kubernetes:51280 ESTABLISHED
TCP 127.0.0.1:51280 kubernetes:51279 ESTABLISHED
package com.lxf.network;
import java.net.InetSocketAddress;
public class SocketDemo {
public static void main(String[] args) {
InetSocketAddress socketAddress1 = new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",8080);
System.out.println(socketAddress1);
System.out.println(socketAddress2);
System.out.println(socketAddress1.getAddress());
System.out.println(socketAddress1.getHostName());
System.out.println(socketAddress1.getPort());
}
}
package com.lxf.network;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//服务器地址
serverSocket = new ServerSocket(9999);
//端口
socket = serverSocket.accept();
//等待连接
is = socket.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!= -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
if (baos != null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.lxf.network;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//客户端地址 端口
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//创建socket连接
socket = new Socket(serverIP,port);
//发送消息
os = socket.getOutputStream();
os.write("你好".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d16Uo2zS-1651333808793)(../图片/typora/1649659419954.png)]](https://img-blog.csdnimg.cn/9daa05742c5b4fa6acb6f89cc9fd33d3.png)
package com.lxf.network;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
OutputStream os = null;
try {
//服务器地址
serverSocket = new ServerSocket(9000);
//端口
socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//等待连接
is = socket.getInputStream();
fos = new FileOutputStream( new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!= -1){
fos.write(buffer,0,len);
}
//管道流 通知客户端接受完毕
os = socket.getOutputStream();
os.write("OK shutdown".getBytes());
System.out.println(fos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.lxf.network;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
//客户端地址 端口
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9000;
//创建socket连接
socket = new Socket(serverIP,port);
//发送消息
os = socket.getOutputStream();
fis = new FileInputStream(new File("IMG_0001.JPG"));
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!= -1){
os.write(buffer,0,len);
}
os.write("你好".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.lxf.network;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpClientDemo {
public static void main(String[] args) throws Exception {
//建立UDP的socket
DatagramSocket socket = new DatagramSocket(8888);
//建立数据包
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String msg = reader.readLine();
byte[] buffer = msg.getBytes();
//数据包要发给谁
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length,new InetSocketAddress("localhost",6666));
socket.send(packet);//发送包
if (msg.equals("bye")){
break;
}
}
socket.close();
}
}
package com.lxf.network;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServerDemo {
public static void main(String[] args) throws Exception {
//监听UDP端口
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//接受数据
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);/* 阻塞接受 */
byte[] data = packet.getData();
String receiveDate = new String(data,0,packet.getLength());
System.out.println(receiveDate);
// System.out.println(new String(packet.getData(),0,packet.getLength()));
if (new String(packet.getData(),0,packet.getLength()).equals("bye")){
break;
}
}
socket.close();
}
}
package com.lxf.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceiveDemo implements Runnable {
DatagramSocket socket = null;
private int port;
private String msgFrom ;
public TalkReceiveDemo(int port,String msgFrom) {
this.port = port;
this.msgFrom = msgFrom ;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
BufferedReader reader = null;
@Override
public void run() {
while (true){
try {
//接受数据
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);/* 阻塞接受 */
byte[] data = packet.getData();
String receiveDate = new String(data,0,packet.getLength());
System.out.println(msgFrom+":"+receiveDate);
// System.out.println(new String(packet.getData(),0,packet.getLength()));
if (new String(packet.getData(),0,packet.getLength()).equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
=======================================================================
package com.lxf.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSendDemo implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSendDemo(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
String msg = reader.readLine();
byte[] buffer = msg.getBytes();
//数据包要发给谁
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);//发送包
if (msg.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
=====================
package com.lxf.network;
public class TalkDemoT {
public static void main(String[] args) {
new Thread(new TalkSendDemo(5555,"localhost",8888)).start();
new Thread(new TalkReceiveDemo(9999,"学生")).start();
}
}
=============================
package com.lxf.network;
public class TalkDemoS {
public static void main(String[] args) {
new Thread(new TalkSendDemo(7777,"localhost",9999)).start();
new Thread(new TalkReceiveDemo(8888,"老师")).start();
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LkqH5kwA-1651333808795)(../../图片/typora/1651331731113.png)]](https://img-blog.csdnimg.cn/ac7c21d87fd9410f95ba369b65d8c46e.png)
统一资源定位符
package com.lxf.network;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLDemo {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/lxf/SECFile.html");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("SECFile.html");
byte[] buffer = new byte[1024];
int len ;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-usZY56GO-1651333808795)(../../图片/typora/1651333697608.png)]](https://img-blog.csdnimg.cn/59539f659f71403590a549295c83a641.png)