借物表
在此文章记录了: 😍Weidows-の成长路线#网络编程
简介
IP
import java.net.InetAddress; import java.net.UnknownHostException;
public class Ip {
public static void main(String[] args) { try { InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1"); System.out.println(inetAddress1); InetAddress inetAddress2 = InetAddress.getByName("localhost"); System.out.println(inetAddress2); InetAddress inetAddress3 = InetAddress.getLocalHost(); System.out.println(inetAddress3);
InetAddress inetAddress4 = InetAddress.getByName("www.bilibili.com"); System.out.println(inetAddress4);
System.out.println(inetAddress4.getAddress()); System.out.println(inetAddress4.getCanonicalHostName()); System.out.println(inetAddress4.getHostAddress()); System.out.println(inetAddress4.getHostName()); } catch (UnknownHostException e) { e.printStackTrace(); } }
}
|
端口
TCP 与 UDP 各有 65535 个端口
两个协议间不冲突
但是一个协议下的一个端口只能同时被一个软件占用.
端口分类
常用 shell 指令
netstat -ano #查看所有的端口 netstat -ano | finstr "5900" #查看指定的端口 tasklist | findstr "8696" #查看指定端口的进程
|
InetSocketAddress
import java.net.InetSocketAddress;
public class Port { public static void main(String[] args) { InetSocketAddress socketAddress = new InetSocketAddress("localhost", 8080); System.out.println(socketAddress);
System.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getHostName()); System.out.println(socketAddress.getPort()); }
}
|
通信协议
通过约定协议来管控通信的速率,码率等问题
并通过协议分层来简化复杂度
几种协议
TCP
UDP
单向的信息传递
没有客户端与服务端界限
无论有没有接收方,无论接收方是否接收,发送方只管发送
常见的DDOS攻击
,让大量垃圾信息污染服务器接收端口,使其瘫痪.
TCP 连接
- Server 服务端与 Client 客户端通过 TCP 方式创建数据连接.
Server 端
建立服务的端口 ServerScoket
等待用户的连接 accept
接收用户的消息
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket;
public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9999);
while (true) { Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, length); } if (byteArrayOutputStream.toString().matches("quit")) { break; } else { System.out.println(byteArrayOutputStream.toString()); byteArrayOutputStream.close(); inputStream.close(); accept.close(); } }
serverSocket.close(); } }
|
Client 端
import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket;
public class Client { public static void main(String[] args) throws Exception { InetAddress address = InetAddress.getLocalHost();
int port = 9999;
Socket socket = new Socket(address, port);
OutputStream outputStream = socket.getOutputStream(); outputStream.write("Weidows connecting here.".getBytes());
outputStream.close(); socket.close(); } }
|
文件传输
Server
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket;
public class Server { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept(); System.out.println("Server开始监听.");
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("Java/src/main/java/twenty_one/net/file_upload/receive.png")); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } System.out.println("Server接收完毕.");
OutputStream os = socket.getOutputStream(); os.write("Server通知:接受完毕,Client可以断开了".getBytes());
fos.close(); is.close(); socket.close(); serverSocket.close(); System.out.println("Server已发送验证并断开连接."); }
}
|
Client
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket;
public class Client { public static void main(String[] args) throws Exception { Socket socket = new Socket(InetAddress.getByName("localhost"), 9999);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("Java/src/main/java/twenty_one/net/file_upload/upload.png"));
byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { os.write(buffer, 0, len); } System.out.println("Client传输完毕.");
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer2 = new byte[1024]; int len2; while ((len2 = inputStream.read(buffer2)) != -1) { baos.write(buffer2, 0, len2); } System.out.println(baos.toString());
baos.close(); inputStream.close(); fis.close(); os.close(); socket.close(); System.out.println("Client已接收到验证并断开连接"); }
}
|
UDP 连接
Server
import java.net.DatagramPacket; import java.net.DatagramSocket;
public class Server { public static void main(String[] args) throws Exception { DatagramSocket datagramSocket = new DatagramSocket(9999); System.out.println("等待数据传入中...");
byte[] buffer = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length); datagramSocket.receive(datagramPacket);
System.out.println("Server接收到数据:" + new String(datagramPacket.getData()));
datagramSocket.close(); } }
|
Client
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress;
public class Client { public static void main(String[] args) throws Exception { InetAddress localHost = InetAddress.getLocalHost(); int port = 9999; byte[] data = "这是Client发送的数据.".getBytes(); DatagramPacket datagramPacket = new DatagramPacket(data, data.length, localHost, port);
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(datagramPacket); System.out.println("传输成功.");
datagramSocket.close(); } }
|
UDP 循环发送
Server
import java.net.DatagramPacket; import java.net.DatagramSocket;
public class Server2 { public static void main(String[] args) throws Exception { DatagramSocket datagramSocket = new DatagramSocket(9999); while (true) { byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length); System.out.println("Server准备接收数据中.");
datagramSocket.receive(datagramPacket); String string = new String(datagramPacket.getData()); System.out.println("接收到数据:\t" + datagramPacket.getAddress() + ":" + datagramPacket.getPort() + "\t" + string);
if (string.contains("quit")) { break; } }
datagramSocket.close(); System.out.println("Server已关闭连接."); } }
|
Client
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress;
public class Client2 { public static void main(String[] args) throws Exception { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket datagramSocket = new DatagramSocket();
while (true) { System.out.println("Client准备读取Terminal数据:"); String readLine = bufferedReader.readLine(); byte[] bytes = readLine.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, new InetSocketAddress("localhost", 9999));
datagramSocket.send(datagramPacket); System.out.println("Client已经发送数据.");
if (readLine.equals("quit")) { break; } }
datagramSocket.close(); System.out.println("Client已关闭连接."); } }
|
双向聊天
实现发送
,接收
两个线程类
老师和学生各开上面两个线程,实现发,收
功能.
发送线程
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 TalkSend implements Runnable { BufferedReader bufferedReader = null; DatagramSocket datagramSocket = null;
private String targetIP; private int targetPort;
public TalkSend(String targetIP, int targetPort) { this.targetIP = targetIP; this.targetPort = targetPort;
try { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); datagramSocket = new DatagramSocket(); } catch (SocketException e) { e.printStackTrace(); } }
public static void main(String[] args) {
}
@Override public void run() { while (true) { System.out.println("Client准备读取Terminal数据:"); try { String readLine = bufferedReader.readLine(); byte[] bytes = readLine.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, new InetSocketAddress(targetIP, targetPort));
datagramSocket.send(datagramPacket); System.out.println("Client已经发送数据.");
if (readLine.equals("quit")) { break; } } catch (IOException e) { e.printStackTrace(); } }
datagramSocket.close(); System.out.println("Client已关闭连接."); } }
|
接收线程
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException;
public class TalkReceive implements Runnable { DatagramSocket datagramSocket = null; private int localPort;
public TalkReceive(int localPort) { this.localPort = localPort;
try { datagramSocket = new DatagramSocket(this.localPort); } catch (SocketException e) { e.printStackTrace(); } }
@Override public void run() { while (true) { try { byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length); System.out.println("Server准备接收数据中.");
datagramSocket.receive(datagramPacket);
String string = new String(datagramPacket.getData()); System.out.println("接收到数据:\t" + datagramPacket.getAddress() + ":" + datagramPacket.getPort() + "\t" + string);
if (string.contains("quit")) { break; } } catch (IOException e) { e.printStackTrace(); } }
datagramSocket.close(); System.out.println("Server已关闭连接."); }
public static void main(String[] args) {
} }
|
老师
public class Teacher { public static void main(String[] args) { new Thread(new TalkReceive(7777)).start(); new Thread(new TalkSend("localhost", 8888)).start(); }
}
|
学生
public class Student { public static void main(String[] args) { new Thread(new TalkReceive(8888)).start(); new Thread(new TalkSend("localhost", 7777)).start(); }
}
|
URL
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL;
public class GetURL { public static void main(String[] args) throws Exception { URL url = new URL("https://cdn.jsdelivr.net/gh/Weidows/Weidows/image/divider.png"); String path = "Java/src/main/java/twenty_one/net/"; String fileName = url.getPath().substring(url.getPath().lastIndexOf('/') + 1);
InputStream openStream = url.openStream(); FileOutputStream fileOutputStream = new FileOutputStream(new File(path + fileName));
byte[] bytes = new byte[1024]; int length; while ((length = openStream.read(bytes)) != -1) { fileOutputStream.write(bytes, 0, length); }
fileOutputStream.close(); openStream.close(); } }
|