ArticleDirectory
- 1. IP address and port (port number)
- 2. TCP and UDP
- 3. Socket
- 4. Network Programming in Java
- Now let's take a look at how to write UDP network programs.
First, some basic knowledge:
1. IP address and port (port number)
Local IP Address: 172.0.0.1
The port number ranges from 0 ~ Between 65535 (2 bytes), 0 ~ 1023 is used for well-known network services and applications, so it is best not to use 0 ~ 1023 these port numbers
2. TCP and UDP
TCP: connection-oriented communication protocol (Transmission Control Protocol)
UDP
3. Socket
Socket is the network driver layer provided to applications.ProgramProgramming Interface and a mechanism
The socket can be compared to a dock. The application will ship the goods to the dock, and the goods will be shipped. On the other side, the application only needs to take the goods away after the goods arrive at the dock.
The socket is created in the application. It establishes a relationship with the driver through a binding mechanism and notifies the corresponding IP address and port.
4. Network Programming in Java
In the java.net package
The datagramsocket class is used for UDP communication.
Serversocket server used for TCP Communication
Socket servers and clients used for TCP Communication
Now let's take a look at how to compile the UDP Network Program's initramsocket class.
Constructor
---- Public datagramsocket () (this parameter is used when sending data first without specifying the port number. A unused port number is automatically selected)
---- Public datagramsocket (INT port) (if you want to accept data, you must specify a clear port number)
---- Public datagramsocket (INT port, inetaddress laddr) (if a computer has multiple IP addresses, You need to specify a specific IP address ......)
---- Close method (after resources of the initramsocket are released, the port number can be used by other network resources again)
---- Send (datagrampacket p) method (used to send UDP packets)
---- Receive (datagrampacket p) method (used to accept UDP packets)
Datagrampacket class
If we compare mongoramsocket to a dock, mongorampacket can be considered as a container for sending and receiving data.
Constructor
---- Public writable rampacket (byte [] buff, int length) (buff indicates the buffer, and length indicates the buffer size. This parameter is used when data is received without specifying IP addresses and ports)
---- Public datagrampacket (byte [] buff, int length, inetaddress Address, int port) (address and port represent the IP address and port of the target computer, used when sending data)
---- Getaddress, getport, getdata, getlength Method
Inetaddress class
Inetaddress is a class used to represent the IP address of a computer.
---- Getbyname () static method, returns the instance
---- Gethostaddress () returns the IP address string, for example, "192.168.0.1"
Simplest UDP Program
Bidirectional conversion between strings and byte Arrays
The UDP receiver must be started before receiving the content sent by the UDP sender.
Solve the Problem of sending Chinese strings
Sender program:
1 Import Java.net. datagrampacket;
2 Import Java.net. datagramsocket;
3 Import Java.net. inetaddress;
4
5 Public Class Udpsend {
6 Public Static Void Main (string [] ARGs) Throws Exception {
7 Datagramsocket DS = New Datagramsocket ();
8 String info = "Hello world! ";
9 DS. Send ( New Datagrampacket (info. getbytes (), info. Length (), inetaddress. getbyname ("115.173.172.98"), 3000 ));
10 DS. Close ();
11 }
12 }
Receiver:
1 Import Java.net. datagrampacket;
2 Import Java.net. datagramsocket;
3
4 Public Class Udprecv {
5 Public Static Void Main (string [] ARGs) Throws Exception {
6 Datagramsocket DS = New Datagramsocket (3000 );
7 Datagrampacket dp = New Datagrampacket ( New Byte [1, 1024], 1024 );
8 DS. Receive (DP );
9 System. Out. println ( New String (DP. getdata (), 0, DP. getlength ()));
10 System. Out. println ("from:" + dp. getaddress (). gethostaddress () + ":" + dp. getport ());
11 DS. Close ();
12 }
13 }
The above program can only be used to send English characters. If you want to transfer Chinese characters, you need to change info. Length () in the sender program to info. getbytes (). length, because Chinese occupies two bytes.