network programming
1 Overview
computer network:
Computer network refers to the geographical positions different independent function having a plurality of computers and peripheral equipment, connected via a communication line, the network operating system , network management software and a network communication protocol under the management and coordination, to achieve the sharing of resources and information computer system delivery.
Network programming purposes:
Unlimited radio stations, the spread of information exchange, data exchange, communication ......
What I want to achieve the desired effect:
- A host ip address on how to accurately locate network: port, navigate to a resource on this computer
- I found the host, how to transfer data?
Concept B / S and C /:
javaweb: web programming B / S
Network Programming: TCP / IP C / S
2, the elements of the communication network
How to achieve communication network?
Communication between the two sides address:
- ip: port
Rules: network communication protocol
TCP / IP reference model:
summary:
- Network Programming with two main issues:
- How accurate positioning on the network to one or multiple hosts
- After finding how to communicate with the host
- Network Programming elements:
- IP port number and IP
- Network communication protocol UDP, TCP
- Everything Is an Object:
- There must communicate with the network-related classes in java
- No classes need to be related to define your own
3、IP
ip address: InetAdress
The only locate computers on a network
127.0.0.1: local address i.e. localhost
Classification ip address
ipv4/ipv6
IPV4: 4 bytes. About 42 million in North America accounted for 30 billion and 400 million in Asia in 2011, has been used up
IPV6: 12 bits. 8 unsigned integer, e.g.
1111:2222:3333:4444:5555:6666:aaaa:cccc
Public network (Internet) and private (local area network)
- ABCD class address
- 192.1688.xx.xx, specifically for use within the organization
Domain Name: Memory IP
- IP: www.jd.com (self-purchase)
Test code:
package pers.mobian.tcpip;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestMyip {
public static void main(String[] args) throws UnknownHostException {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
InetAddress inetAddress2 = InetAddress.getByName("localhost");
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress1);
System.out.println(inetAddress2);
System.out.println(inetAddress3);
//查询网络ip地址
InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress4);
//常用的方法
System.out.println(inetAddress4.getCanonicalHostName());//规范的名字
System.out.println(inetAddress4.getHostAddress());//ip
System.out.println(inetAddress4.getHostName());//域名或者自己电脑的名字
}
}
Results of the:
/127.0.0.1
localhost/127.0.0.1
DESKTOP-EGA979M/192.168.253.1
www.baidu.com/183.232.231.174
183.232.231.174
183.232.231.174
www.baidu.com
4, Port
Port process represents a program on the computer
Ports have different software used to distinguish
Port predetermined range: 0 to 65535
TCP and UDP respectively occupy 65,535, that is the total number of computer port 65535 * 2
Port Classification
Public ports 0 to 1023
- HTTP : 80
- HTTPS : 443
- FTP : 21
- Telent : 23
Program Port Register: 1024 to 49151, or assign the user program
- Tomcat:8080
- MySQL:3306
- Oracle:1521
Dynamic private: 49152 to 65535
netstat -ano //查看所有的端口 netstat -ano |findstr “端口号” //查看指定的端口号 tasklist|findstr "端口号" //查看指定端口号的进程
Test code:
package pers.mobian.socket;
import java.net.InetSocketAddress;
public class TestSocket02 {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost",8080);
System.out.println(inetSocketAddress);
System.out.println(inetSocketAddress2);
System.out.println(inetSocketAddress.getAddress());//获取地址
System.out.println(inetSocketAddress.getHostName());//获取主机名字
System.out.println(inetSocketAddress.getPort());//获取端口
}
}
Results of the:
/127.0.0.1:8080
localhost/127.0.0.1:8080
/127.0.0.1
127.0.0.1
8080
5, the communication protocol
Network communication protocol: rate, transmission rate, code structure, the transmission control ......
TCP / IP protocol suite: is a set of protocols
- TCP: User Transfer Protocol
- UDP: User Datagram Protocol
- IP: Internet Protocol
TCP and UDP comparison:
TCP: call
Stable connection
Three-way handshake, waving four
//最少需要三次,保证稳定连接!<连接> A:你瞅啥? B:瞅你咋地? A:干一场 //四次挥手 <断开> A:我要走了 B:你真的要走了吗? B:你真的真的要走了吗? A:我真的要走了
Client, server
The transfer is complete, releases the connection, inefficiency
UDP: texting
- Not connected, unstable
- Client, server: no clear boundaries
- With or without ready, you can send it to you
- DDOS: flood attack (send large amounts of information, port congestion, saturation attack)
6、TCP
1. Text Transfer
The client test code:
package pers.mobian.socket;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class TestClientSocket {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1、要知道服务器得地址、端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2、创建一个socket连接
socket = new Socket(serverIP, port);
//3、发送IO流信息
os = socket.getOutputStream();
os.write("你好,默辨".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源,对关闭得资源进行一个判定,如果没有开启则不需要关闭
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Server test code:
package pers.mobian.socket;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务器端
public class TestServersSocket {
public static void main(String[] args) throws Exception {
//1、给自己新建一个端口号
ServerSocket serverSocket = new ServerSocket(9999);
//2、等待客户端进行连接
Socket socket = serverSocket.accept();
//3、读取客户端的消息
InputStream inputStream = socket.getInputStream();
//4、管道流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer))!= -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
//5、关闭资源(后开先关)
baos.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
Results of the:
//服务器端显示
你好,默辨
2. File Upload
Test code:
//服务器
package pers.mobian.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServersSocket2 {
public static void main(String[] args) throws Exception {
//1、创建服务
ServerSocket serverSocket = new ServerSocket(9999);
//2、监听客户端得连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待
//3、获取输入流
InputStream is = socket.getInputStream();
//4、文件输出
FileOutputStream fos = new FileOutputStream(new File("E:\\ideaWorkSpace\\pan\\day14_socket_01\\33.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
//5、通知客户端,我接受完毕了
OutputStream os = socket.getOutputStream();
os.write("我接受完了".getBytes());
//6、关闭资源
fos.close();
is.close();
socket.close();
}
}
//客户端
package pers.mobian.socket;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TestClientSocket2 {
public static void main(String[] args) throws Exception {
//1、创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
//2、创建一个输出流
OutputStream os = socket.getOutputStream();
//3、读取文件
FileInputStream fis = new FileInputStream(new File("E:\\ideaWorkSpace\\pan\\day14_socket_01\\3.jpg"));
//4、写出文件
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//5、确定服务器接受完毕,才能断开连接
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());
//6、关闭资源
fis.close();
os.close();
os.close();
socket.close();
}
}
7、Tomcat
Server;
- Custom S
- Tomcat server S
Client:
- Custom C
- Browser B
8、UDP
UDP does not have the concept of server-side and client, only the sender and receiver have the concept
1. Case I: A simple message
Sending end test code:
package pers.mobian.socket;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//发送端
public class TestClientSocket3 {
public static void main(String[] args) throws Exception {
//1、建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2、建包
String msg = "你好";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//参数得列表分别是:数据,数据长度始,数据长度终,地址,端口号
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
//3、发送包
socket.send(packet);
//4、关闭资源
socket.close();
}
}
Test code receiving end:
package pers.mobian.socket;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//接收端
public class TestServersSocket3 {
public static void main(String[] args) throws Exception {
//1、设置开放端口
DatagramSocket socket = new DatagramSocket(9090);
//2、准备接受数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
//3、将需要打印得数据进行格式转换
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
2. Case I: a simple chat realization
Sending end test code:
package pers.mobian.udp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TestUDPSender {
public static void main(String[] args) throws Exception {
//设置开放端口
DatagramSocket socket = new DatagramSocket(8888);
while (true) {
//1、读取控制台的数据
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//2、将控制台的一行语句赋值给data
String data = reader.readLine();
//3、将data包装成数据
byte[] datas = data.getBytes();
//4、将数据打包成package
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
//5、发送信息
socket.send(packet);
//判定的数据输入应为最初的输入数据,而非包装后的数据
if(data.equals("bye")){
break;
}
}
socket.close();
}
}
Test code receiving end:
package pers.mobian.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestUDPReceiver {
public static void main(String[] args) throws Exception {
//设置开放端口
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
//1、准备接受包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);
//2、打印接受到的数据
System.out.println(new String(packet.getData(),0,packet.getLength()));
//设置断开连接
byte[] data = packet.getData();
String receiverData = new String(data, 0, data.length);
if (receiverData.equals("bye")) {
break;
}
}
socket.close();
}
}
3. Using multithreading chat
Sender test code:
package pers.mobian.udp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TestUDPSender2 implements Runnable {
private int fromPort;
private String toIP;
private int toPort;
DatagramSocket socket = null;
BufferedReader reader = null;
public TestUDPSender2(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
//设置开放端口
socket = new DatagramSocket(fromPort);
//1、读取控制台的数据
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
//2、将控制台的一行语句赋值给data
String data = reader.readLine();
//3、将data包装成数据
byte[] datas = data.getBytes();
//4、将数据打包成package
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
//5、发送信息
socket.send(packet);
//判定的数据输入应为最初的输入数据,而非包装后的数据
if (data.equals("bye")) {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
Receiver test code:
package pers.mobian.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestUDPReceiver2 implements Runnable {
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TestUDPReceiver2(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
//设置开放端口
socket = new DatagramSocket(port);
} catch (Exception e) {
e.printStackTrace();
System.out.println(88888);
}
}
@Override
public void run() {
while (true) {
//1、准备接受包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
try {
socket.receive(packet);//阻塞式接受
} catch (IOException e) {
e.printStackTrace();
System.out.println("111");
}
//2、打印接受到的数据
//设置断开连接
byte[] data = packet.getData();
String receiverData = new String(data, 0, data.length);
System.out.println(msgFrom + " : " + receiverData);
if (receiverData.equals("bye")) {
break;
}
}
socket.close();
}
}
Test code:
package pers.mobian.udp;
public class TestUDPReceiver2Main1 {
public static void main(String[] args) {
new Thread(new TestUDPSender2(7777,"localhost",9999)).start();
new Thread(new TestUDPReceiver2(8888,"老师")).start();
}
}
Test code:
package pers.mobian.udp;
public class TestUDPReceiver2Main2 {
public static void main(String[] args) {
new Thread(new TestUDPSender2(5555,"localhost",8888)).start();
new Thread(new TestUDPReceiver2(9999,"学生")).start();
}
}
Results of the:
//Main1
ni
学生 : jj
//Main2
老师 : ni
jj
Class 4. url
URL category of use:
Test code:
package pers.mobian.udp;
import java.net.MalformedURLException;
import java.net.URL;
public class TestURL {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/mobian/index.jsp?username=mobian&password=123");
System.out.println(url.getProtocol());//使用的协议
System.out.println(url.getHost());//主机IP
System.out.println(url.getPort());//使用的端口号
System.out.println(url.getPath());//文件路径
System.out.println(url.getFile());//文件的全路径
System.out.println(url.getQuery());//参数列表
}
}
Results of the:
http
localhost
8080
/mobian/index.jsp
/mobian/index.jsp?username=mobian&password=123
username=mobian&password=123
The use of network resources url path to download the test code:
package pers.mobian.udp;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestDownURL {
public static void main(String[] args) throws IOException {
//1、请求下载的地址
URL url = new URL("https://m701.music.126.net/20200121172558/e95dfac9e276e6d145b5f2638a036c2b/jdyyaac/035e/565f/055f/447bb91679a53ba93bc2258f72f57669.m4a");
//2、连接到这个资源HTTP
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("77.m4a");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}