Home > Java > javaTutorial > Detailed explanation of the use of Java network programming

Detailed explanation of the use of Java network programming

零下一度
Release: 2017-06-30 09:57:24
Original
1770 people have browsed it
Keywords: IP address,, port, UDP, DatagramPacket class, DatagramSocket class, TCP, ServerSocket class, Socket class, file upload
1. Basic concepts
1. Basic concepts
①MAC: MAC (Media Access Control or Medium Access Control) address, meaning media access control, or physical address, Hardware address, used to define the location of a network device. In the OSI model, the third network layer is responsible for IP addresses, and the second data link layer is responsible for MAC addresses. Therefore, a host will have a MAC address, and each network address will have an IP address unique to it.
②IP address: refers to the Internet Protocol Address (Internet Protocol Address, also translated as Internet Protocol Address), which is the abbreviation of IP Address. The IP address is a unified address format provided by the IP protocol. It assigns a logical address to each network and each host on the Internet to shield the differences in physical addresses. There are still some IP proxy software, but most of them charge a fee.
③Port: It can be considered as the outlet for communication between the device and the outside world.
The port number is represented by two bytes (16-bit binary number). Its value range is 0~65 535. The port number between 0~1023 is used for the following well-known network services. And applications, users' ordinary applications need to use port numbers above 1024. Ports are divided into physical ports and logical ports (numeric identification of software applications).

④TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) protocols are transport layer protocols.

The TCP protocol is a connection-oriented communication protocol that provides reliable transmission of data in an IP environment. The services it provides include data streaming, reliability, effective flow control, full-duplex operation and multiplexing. Delivered via connection-oriented, end-to-end and reliable packets. In layman's terms, it opens up a connected channel for the data to be sent in advance, and then sends the data;

⑤UDP is a wireless communication protocol and does not provide reliability, flow control or error recovery functions for IP.

2. Examples of common methods of the common InetAddress class:
 1 public class Example1 {
 2     public static void main(String[] args) throws Exception{
 3         //创建一个表示本地主机的InetAddress对象
 4         InetAddress localAddress = InetAddress.getLocalHost(); 
 5         //获得指定主机的InetAddress对象
 6         InetAddress remoteAddress = InetAddress.getByName("www.itcast.cn");
 7         //得到IP地址的主机名。
 8         System.out.println("本机的IP地址:"+localAddress.getHostName());
 9         //获得字符串格式的原始IP地址
10         System.out.println("itcast的IP地址:"+remoteAddress.getHostAddress());
11         //判断指定的时间内地址是否可以到达
12         System.out.println("3秒是否可达:"+remoteAddress.isReachable(3000));
13        
14         System.out.println("itcast的主机名为:"+remoteAddress.getHostName());
15     }
16 }
Copy after login

Running results:

本机的IP地址:wrt.local
itcast的IP地址:123.57.45.99
3秒是否可达:false
itcast的主机名为:www.itcast.cn
Copy after login

2. UDP communication

1. DatagramPacket

This class is similar to a container. When creating the DatagramPacket object of the sender and receiver, use The construction method is different. The construction method of the receiving end only needs to receive a byte array to store the received data, while the construction method of the sending end not only needs to receive the byte array storing the sent data, but also needs to specify the IP address and port of the sending end. Number.

DatagramPacket construction method:

①DatagramPacket (byte[] buf, int length)

Used for the receiving end. When creating a DatagramPacket object, the byte array of encapsulated data is specified. and data size.

②DatagramPacket (byte[] buf, int length, InetAddress addr, int port)

Used for the sender. When creating a DatagramPacket object, the byte array, data size, and The destination IP address (addr) and port number (port) of the data packet.

③DatagramPacket (byte[] buf, int offset, int length)

Used for the receiving end. When creating a DatagramPacket object, the byte array, data size, and starting point of the encapsulated data are specified. Location. The offset parameter is used to specify that the received data starts at offset when placed into the buf buffer array.

④DatagramPacket (byte[] buf, int offset, int length, InetAddress addr, int port)

Used for the sender. When creating a DatagramPacket object, the byte array of encapsulated data is specified. Data size, destination IP address (addr) and port number (port) of the data packet. The offset parameter is used to specify the offset of sending data, that is, starting to send data from the offset position.

#TCP/IP Network Mode

Application layer

Such as HTTP, FTP, DNS

Transport layer

such as TCP, UDP

network layer

such as IP, ICMP, IGMP

Chainsaw layer

Such as driver, interface

## Commonly used methods in the DatagramPacket class #Method declarationFunction descriptionInetAddress getAddress()This method is used to return the IP address of the sender or receiver. If the sender has a DatagramPacket object, it returns the IP address of the receiver. Otherwise, it returns the IP address of the senderint getPort()This method is used to return the port number of the sender or receiver. If the sender has a DatagramPacket object, it returns the port number of the receiver. Otherwise, it returns the port number of the receiver. Return the port number of the sending endbyte[] getData()This method is used to return the data to be received or sent. If it is the DatagramPacket object of the sending end, then Returns the data to be sent, otherwise, returns the received dataint getLength()This method is used to return the length of the data to be received or sent, If it is the DatagramPacket object of the sending end, the length of the data to be sent is returned. Otherwise, the length of the received data is returned.

2. DatagramSocket

DatagramSocket is similar to a dock. The instance object can send and receive DatagramPacket packets. When creating DatagramSocket objects at the sender and receiver, the construction methods used are different.

DatagramSocket construction method:

①DatagramSocket()

When used to create a DatagramSocket object on the sending end, the port number is not specified when creating the object. At this time, the system will Assign a port number that is not used by other network programs.

②DatagramSocket (int port)

This method can be used to create the DatagramSocket object of the receiving end, and can also create the DatagramSocket object of the sending end. When creating the DatagramSocket object of the receiving end, you must specify A port number so that you can listen on the specified port.

③DatagramSocket(int port,InetAddress addr)

When using this construction method in DatagramSocket, not only the port number is specified, but also the related IP address is specified. This situation is suitable for calculation There are multiple network cards on the computer.

## Commonly used methods in the DatagramSocket class #Method declarationFunction descriptionvoid receive(DatagramPacket p)This method is used to fill the received data into the DatagramPacket packet. It will be blocked until the data is received. Only when the data packet is received This method will return only when. void send(DatagramPacket p)This method is used to send DatagramPacket data packet. The data packet sent contains the data to be sent, the length of the data, and the remote host IP address and port numbervoid close()Close the current Socket and notify the driver to release the resources reserved for this Socket.

3、UDP 网络程序

在通信时只有接收端程序先运行,才能避免因发送端发送的数据无法接收,而造成数据丢失。示例:

 1 import java.net.DatagramPacket;
 2 import java.net.DatagramSocket;
 3 
 4 //接收端程序
 5 public class Example2 {
 6     public static void main(String[] args) throws Exception{
 7         //创建一个长度为1024的字节数组,用于接收数据
 8         byte [] buf = new byte[1024];
 9         //定义一个DatagramSocket对象,监听的端口为8954
10         DatagramSocket ds = new DatagramSocket(8954);
11         //定义一个DatagramPacket对象,用于接收数据
12         DatagramPacket dp = new DatagramPacket(buf,1024);
13         System.out.println("等待接收数据");
14         ds.receive(dp);          //等待接收数据,如果没有数据则会阻塞
15         //调用DatagramPacket的方法获得接收的消息,包括内容、长度、IP地址和端口号
16         String str = new String(dp.getData(),0,dp.getLength())
17                 +"from"+dp.getAddress().getHostAddress()+":"+dp.getPort();
18         System.out.println(str); //打印收到的信息
19         ds.close();              //释放资源
20     }
21 }
22 
23 
24 import java.net.DatagramPacket;
25 import java.net.DatagramSocket;
26 import java.net.InetAddress;
27 
28 //发送端程序
29 public class Example3 {
30     public static void main(String[] args) throws Exception {
31         //创建一个DatagramSocket对象
32         DatagramSocket ds = new DatagramSocket(3000);
33         String str = "Hello World!";     //要发送的数据
34         /*
35          * 创建一个要发送的数据包,包括发送数据,数据长度,接收端IP地址以及端口号
36          */
37         DatagramPacket dp = new DatagramPacket(str.getBytes(),str.length(),
38                 InetAddress.getByName("localhost"),8954);
39         System.out.println("发送消息");
40         ds.send(dp);                     //发送数据
41         ds.close();                      //释放资源
42     }
Copy after login

运行结果

发送消息

等待接收数据
Hello World!from127.0.0.1:3000
Copy after login

解析:发送货物(数据)前,确定到货码头是否能接收。

创建空间(数据容器)接收货物(数据),创建码头【DatagramSocket(8954)】并实时监听发货码头发货通道(端口),创建集装箱并将空间加入用于接收货物,一直等待接收货物,接收码头将货物填充到集装箱中,获取到货物信息(数据等信息)。

发送货物需要建一个码头【DatagramSocket(3000)】,码头可指定发送通道即端口(也可以不指定发送通道),将要发送货物(数据)装进集装箱(DatagramPacket

)中,并指定发送到的码头名字(IP地址或主机名)及接收通道(端口),通过码头把集装箱发出去[send()],腾出空间(close)。

三、TCP通信

1、ServerSocket

在开发TCP程序时,首先需要创建服务器端程序,其构造方法如下:

①ServerSocket()

使用该构造方法在创建ServerSocket对象时并没有绑定端口号,不能直接使用,还需要继续调用bind(SocketAddress endpoint)方法将其绑定到指定的端口上,才能正常使用。

②ServerSocket(int port)【最常用】

使用用该构造方法在创建ServerSocket对象时,就可以将其绑定到一个指定的端口号上。

③ServerSocket(int port,int backlog)

backlog 参数用于指定在服务器忙时,可以与之保持连接请求的等待客户数量,如果没有指定这个参数默认为50 。

④ServerSocket(int port,int backlog,InetAddress bindAddr)

指定了相关的IP地址,适用于计算机上有多块网卡和多个IP的情况。

ServerSocket类中的常用方法
方法声明功能描述
Socket accept()该方法用于等待客户端的连接,在客户端连接之前一直处于阻塞状态,如果有客户端连接就会返回一个与之对应的Socket对象
InetAddress getInetAddress()该方法用于返回一个InetAddress对象,该对象封装了ServerSocket绑定的IP地址
boolean isClosed()该方法用于判断ServerSocket对象是否为关闭状态,如果是关闭状态则返回true,反之则返回false
void bind(SocketAddress endpoint)该方法用于判断ServerSocket对象绑定到指定的IP地址和端口号,其中参数endpoint封装了IP地址和端口号。

2、Socket

Socket类常用构造方法:
①Socket()
使用该构造方法在创建Socket对象时,并没指定IP地址和端口号,创建对象后还需调用connect(SocketAddress endpoint)方法,才能完成与指定服务器的连接,参数endpoint封装了IP地址和端口号。
②Socket(String host,int port)
使用该构造方法在创建Socket对象时,根据参数去连接在指定IP地址和端口上运行的服务器程序,其中参数host接收的一个字符类型的IP地址。
③Socket(InetAddress addres,int port)
与第二个构造方法类似,参数address用于接收一个InetAddress类型的对象,该对象用于封装一个IP地址。
Socket类中的常用方法
方法声明功能描述
int getPort()该方法返回一个int类型对象,该对象时Socket对象与服务器端连接的端口号
InetAddress getLocalAddress()该方法用于获取Socket对象绑定的本地IP地址,并将IP地址封装成InetAddress类型的对象返回
void close()该方法用于关闭Socket连接,结束本次通信。在关闭Socket之前,应将于Socket相关的所有的输入与输出流全部关闭,这是因为一个良好的程序应该在执行完毕时释放所有的资源
IputStream getInputStream()该方法返回一个InputStream类型的输入流对象,如果该对象是由服务器端的Socket返回,就用于读取客户端发送的数据,反之,用于读取服务器端发送的数据
OutputStream getOutputStream()该方法返回一个OutputStream类型的输出流对象,如果该对象是由服务器端的Socket返回,就用于向客户端发送数据,反之,用于向服务器端发送数据
3、简单的TCP网络程序
 1 import java.io.OutputStream;
 2 import java.net.ServerSocket;
 3 import java.net.Socket;
 4 
 5 public class Example4 {
 6     public static void main(String[] args) throws Exception {
 7         new TCPServer().listen();      //创建TCPServer对象,并调用listen()方法
 8     }
 9 }
10 //TCP服务器端
11 class TCPServer{
12     private static final int PORT= 7788;//定义一个端口号
13 
14     public void listen() throws Exception{ //定义一个listen()方法,抛出一个异常
15         ServerSocket serverSocket = new ServerSocket(PORT);//创建ServerSocket对象
16         Socket client=serverSocket.accept();  //调用ServerSocket的accept()方法接收数据
17         OutputStream os = client.getOutputStream(); //获取客户端的输出流
18         System.out.println("开始与客户端交换数据");
19         os.write(("Java欢迎你!").getBytes());
20         Thread.sleep(5000);      //模拟执行其他功能占用的时间
21         System.out.println("结束与客户端交互数据");
22         os.close();
23         client.close();
24     }
25 }
Copy after login

 1 import java.io.InputStream;
 2 import java.net.InetAddress;
 3 import java.net.Socket;
 4 
 5 public class Example5 {
 6     public static void main(String[] args) throws Exception{
 7         new TCPClient().connect();//创建TCPClient对象,并调用connect()方法
 8     }
 9 }
10 //TCP客户端
11 class TCPClient{
12     private static final int PORT=7788;//服务端的端口号
13     public void connect() throws Exception{
14         //创建一个Socket并连接到给出地址和端口号的计算机
15         Socket client = new Socket(InetAddress.getLocalHost(),PORT);
16         InputStream is = client.getInputStream(); //得到接收数据的流
17         byte[] buf = new byte[1024];  //定义1024个字节数组的缓冲区
18         int len=is.read(buf);    //将数据读取到缓冲区中
19         System.out.println(new String(buf,0,len));  //将缓冲区中的数据输出
20         client.close();       //关闭Socket对象,释放资源
21     }
22 }
Copy after login

Example4 运行结果:
开始与客户端交换数据
结束与客户端交互数据

Example5 运行结果:
Java欢迎你!
Copy after login

4、TCP案例——文件上传

实现图片上传到服务器的功能。

服务端程序:

 1 import java.io.File;
 2 import java.io.FileOutputStream;
 3 import java.io.InputStream;
 4 import java.io.OutputStream;
 5 import java.net.ServerSocket;
 6 import java.net.Socket;
 7 
 8 public class Example7 {
 9     public static void main(String[] args) throws Exception{
10         ServerSocket serverSocket = new ServerSocket(10001);//创建ServerSocket对象
11         while (true){
12             //调用accept()方法接收客户端请求,得到Socket对象
13             Socket s = serverSocket.accept();
14             //每当和客户端建立Socket连接后,单独开启一个线程处理和客户端的交互
15             new Thread(new ServerThread(s)).start();
16         }
17     }
18 }
19 class ServerThread implements Runnable{
20     private Socket socket ;     //持有一个Socket类型的属性
21     public ServerThread(Socket socket){   //构造方法中吧Socket对象作为实参传入
22         this.socket=socket;
23     }
24 
25     @Override
26     public void run() {
27         String ip = socket.getInetAddress().getHostAddress(); //获取客户端的IP地址
28         int count =1;        //上传图片个数
29         try{
30             InputStream in = socket.getInputStream();
31             //创建上传图片目录的File对象
32             File parentFile =new File("/Users/adims/Downloads/upload/");
33             if (!parentFile.exists()){  //如果不存在,就创建这个目录
34                 parentFile.mkdir();
35             }
36             //把客户端的IP地址作为上传出文件的文件名
37             File file = new File(parentFile,ip+"("+count+").jpeg");
38             while (file.exists()){
39                 //如果文件名存在,则把count++
40                 file=new File(parentFile,ip+"("+(count++)+").jpeg");
41             }
42             //创建FileOutputStream对象
43             FileOutputStream fos = new FileOutputStream(file);
44             byte[] buf=new byte[1024];  //定义一个字节数组
45             int len=0;     //定义一个int类型的变量len,初始值为0
46             while ((len=in.read(buf))!=-1){   //循环读取数据
47                 fos.write(buf,0,len);
48             }
49             OutputStream out = socket.getOutputStream();  //获取服务端的输出流
50             out.write(("上传成功").getBytes());    //上传成功后向客户端写出"上传成功"
51             fos.close();                          //关闭输出流对象
52             socket.close();                       //关闭Socket对象
53         }catch (Exception e){
54               throw new RuntimeException(e);
55         }
56     }
57 }
Copy after login

客户端程序:

 1 import java.io.FileInputStream;
 2 import java.io.InputStream;
 3 import java.io.OutputStream;
 4 import java.net.InetAddress;
 5 import java.net.Socket;
 6 
 7 public class Example8 {
 8     public static void main(String[] args) throws Exception{
 9         Socket socket= new Socket(InetAddress.getLocalHost(),10001);  //创建客户端Socket对象,指定IP地址和端口号
10         OutputStream out= socket.getOutputStream();  //获取Socket的输出流对象
11         //创建FileInputStream对象
12         FileInputStream fis = new FileInputStream("/Users/adims/Downloads/WechatIMG1.jpeg");
13         byte[] buf =new byte[1024]; //定义一个字节数组
14         int len;       //定义一个int类型的变量len
15         while ((len=fis.read(buf))!=-1){    //循环读取数据
16             out.write(buf,0,len);
17         }
18         socket.shutdownOutput();      //关闭客户端输出流
19         InputStream in = socket.getInputStream();   //获取Socket的输入流对象
20         byte[] bufMsg = new byte[1024];    //定义一个字节数组
21         int num =in.read(bufMsg);          //接收服务端的信息
22         String Msg = new String(bufMsg,0,num);
23         System.out.println(Msg);
24         fis.close();                   //关闭输入流对象
25         socket.close();                //关闭Socket对象
26     }
27 }
Copy after login

需注意:shutdownOutput()方法非常重要,因为服务器端程序在while循环中读取客户端发送的数据,当读取到-1时才会结束循环,如果客户端不调用shutdownOutput()方法关闭输出流,服务器端就不会读到-1,而会一直执行while循环,同时客户端服务器端的read(byte[])方法也是一个阻塞方法,这样客户端与服务器端进入一个“死锁”状态。

The above is the detailed content of Detailed explanation of the use of Java network programming. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template