-------
Android training and Java training. We look forward to communicating with you!
----------
Network Programming
Port
Physical Port:
Logical port: used to identify the logical address of a process. It identifies different processes. Valid port: 0 ~ 65535, 0 ~ 1024 the system uses or retains the port.
IP object in Java: inetaddress.
Import java.net .*;
Class ipdemo {
Public static void main (string [] ARGs) throws unknownhostexception {
// Obtain an IP object by name (IP string or host name.
Inetaddress IP = inetaddress. getbyname ("www.baidu.com"); // java.net. unknownhostexception
System. Out. println ("ADDR:" + IP. gethostaddress ());
System. Out. println ("name:" + IP. gethostname ());
}
}
Socket: socket, the communication endpoint.
It is a mechanism provided for network services. both ends of the communication have sockets. network communication is actually the communication between sockets, and data is transmitted through Io between the two sockets.
UDP Transmission
1. A socket is required for network transmission.
2. Data must be encapsulated into data packets, including the destination address, port, and data.
It is impossible to directly operate on UDP. For Java, udp should be encapsulated into an object, which is easy to use. This object is the socket object of UDP transmission protocol.
Because the data package contains a large amount of information, it is encapsulated as an object to facilitate the operation. This data packet object is: datagrampacket. Through the method in this object, various information in the data packet can be obtained.
Mongoramsocket provides the sending and receiving functions. During UDP transmission, you must specify the sender and the receiver.
UDP sender
1. Create a UDP socket service. If no port is specified when an object is created, the system will automatically allocate an unused port.
2. Specify the specific data to be sent.
3. encapsulate data into data packets.
4. Use the send method of the socket service to send data packets.
5. Close the resource.
--------------------------------------------------------------
Import java.net .*;
Class udpsend {
Public static void main (string [] ARGs) throws exception {
// 1. Establish the UDP socket service.
Datagramsocket DS = new datagramsocket (8888); // specify the sending port. If this parameter is not specified, the system will randomly allocate the port.
// 2. Specify the specific data to be sent.
String text = "UDP transmission demonstration buddy ";
Byte [] Buf = text. getbytes ();
// 3. encapsulate the data into data packets.
Datagrampacket dp = new datagrampacket (BUF,
Buf. length, inetaddress. getbyname ("10.1.31.127"), 10000 );
// 4. Use the send method of the socket service to send data packets.
DS. Send (DP );
// 5. Close the resource.
DS. Close ();
}
}
-------------------------------------------------------------
UDP Receiver
1. To create a UDP socket service, you must specify a port to ensure that only the data sent to the port is the data that can be processed by the receiver.
2. Define a data packet to store the received data.
3. Store the received data to the data packet through the Receiving Method of the socket service.
4. Obtain the specific data content in the data packet through the data packet method, such as IP address, port, and data.
5. Close the resource.
-------------------------------------------------------------
Class udprece {
Public static void main (string [] ARGs) throws exception {
// 1. Create the UDP socket service.
Datagramsocket DS = new datagramsocket (10000 );
// 2. Define a data packet to store the received data. First, define the byte array, and the data will be stored in the byte array.
Byte [] Buf = new byte [1, 1024];
Datagrampacket dp = new datagrampacket (BUF, Buf. Length );
// 3. Store the received data to the data packet through the Receiving Method of the socket service.
DS. Receive (DP); // This method is a blocking method.
// 4. Obtain the specific data content in the data packet through the data packet method, such as IP address, port, and data.
String IP = DP. getaddress (). gethostaddress ();
Int Port = DP. getport ();
String text = new string (DP. getdata (), 0, DP. getlength (); // convert the valid part of the byte array into a string.
System. Out. println (IP + ":" + port + "--" + text );
// 5. Close the resource.
DS. Close ();
}
}
-------------------------------------------------------------
TCP transmission: a data transmission channel is established after the two endpoints are connected. This channel is called a stream and a stream based on the network. It is called a socket stream. The stream can be read or written.
Two endpoints of TCP: one is the client and the other is the server.
Client: corresponding object, socket
Server: corresponding object, serversocket
Tcp client
1. Establish a TCP socket service. It is best to specify the specific address and port. When this object is created, it can be connected to the specified IP address and port (three-way handshake ).
2. If the connection is successful, it means that the channel is established and the socket stream is generated. You only need to get the read stream and write stream in the socket stream. You only need to get two stream objects through getinputstream and getoutputstream.
3. Close the resource.
--------------------------------------------------------------
Import java.net .*;
Import java. Io .*;
// Requirement: the client sends data to the server.
Class tcpclient {
Public static void main (string [] ARGs) throws exception {
Socket S = new socket ("10.1.31.69", 10002 );
Outputstream out = S. getoutputstream (); // gets the output stream object in the socket stream.
Out. Write ("TCP demo, buddy again! ". Getbytes ());
S. Close ();
}
}
--------------------------------------------------------------
TCP Server
1. Create a socket service on the server and listen to a port.
2. In order to provide services to the client, the server can obtain the client content through the accept method.
3. You can use the socket stream in the obtained socket object to communicate with a specific client.
4. If the communication ends, close the resource. Note: first shut down the client and then the server.
--------------------------------------------------------------
Class tcpserver {
Public static void main (string [] ARGs) throws exception {
Serversocket Ss = new serversocket (10002); // create a socket service for the server
Socket S = ss. Accept (); // obtain the client object
String IP = S. getinetaddress (). gethostaddress ();
System. Out. println (IP + "... connected ");
// You can use the socket stream in the obtained socket object to communicate with a specific client.
Inputstream in = S. getinputstream (); // reads client data and reads the stream using the socket of the client object
Byte [] Buf = new byte [1, 1024];
Int Len = in. Read (BUF );
String text = new string (BUF, 0, Len );
System. Out. println (text );
// If the communication ends, close the resource. Note: first shut down the client and then shut down the server.
S. Close ();
SS. Close ();
}
}