Basic concepts of network programming IP and Port
IP is the only identification of networked devices in the Internet, and network programming is the communication with the remote computer, so must be able to locate the remote computer, IP is used to solve such problems; a computer may have many processes, specific to which process to communicate, which depends on the port to identify. TCP and UDP protocols
TCP is the abbreviation of Tranfer Control Protocol, and it is a reliable transmission protocol for connection. Through the TCP protocol transmission, it is a sequential error-free data stream. TCP is more complex, but it can provide services for application layer protocols that require reliability, such as file transfer, mail delivery, Web browsing, remote logins, and so on.
UDP is the user Datagram protocol abbreviation, is a connectionless protocol, the data packets into a datagram, independent transmission. UDP protocol transmission can not guarantee the data is not lost, error-free, and can not guarantee that the data sent and received in the same order. UDP protocol is relatively simple, high efficiency, for the reliability requirements are not high, real-time requirements higher should provide services, such as instant messaging, video conferencing, live and so on.
TCP protocol Features:
1, connection-oriented, providing reliable data transmission services;
2, provides the flow control, the TCP can control the process to send the data the rate, guarantees the other end not to be inundated with the massive data to overflow;
3, the provision of congestion control, when the network congestion, TCP will also control the speed and number of data;
4, must establish the connection, the efficiency will be slightly low, but can carry on the large data quantity transmission in the connection;
UDP protocol Features:
1, for the connectionless, is a way to do their best, do not provide reliable data transmission services;
2, UDP has no blocking and flow control;
3, does not need to establish the connection, the speed is quicker;
4, UDP is transmitted by the datagram, the size of each datagram is limited to 64k; Socket
A socket is a network-driven layer that is provided to an application programming interface or a mechanism. Two programs on the network realize data exchange through a two-way communication connection, one end of this two-way link is called a socket. The socket is typically used to implement a connection between the client and the service party. Socket is a very popular programming method for TCP/IP protocol, a socket is uniquely determined by an IP address and a port number.
The socket is created in the application, which establishes a relationship with the driver through a binding mechanism, telling itself the corresponding IP and port. After the socket is created, the application writes the data to the socket, and the socket is given to the driver to send data to the network, and the computer receives data from the network on the IP and port bound to a socket, and then the driver gives the socket. The application can read the received data from this socket. This is how a network application sends and receives data through a socket.
The basic workflow for a socket consists of the following four steps:
1, create socket;
2, open the input and output stream connected to the socket;
3, according to a certain agreement on the socket to read and write operations;
4, close socket; Java Network Programming common class inteaddress class
Inteaddress is a class that encapsulates IP address information. IP addresses are using 32-bit or 128-bit unsigned digits, and UDP and TCP protocols are built on it. Inteaddress the IP address in the form of an object, it is easier to manipulate and get its properties. Note that inteaddress does not have a constructor method, it can only get its object through a static method (the method automatically determines whether to create a inte4address or create a inte4address).
The following are common methods for inteaddress:
Get the local host IP address object
Static inetaddress Getlocalhost ();
Gets the IP address object for the given host name
Static InetAddress Getbyname (String host);
Gets an array of IP address objects for a given host name
Static inetaddress[] Getallbyname (String host);
Get InetAddress object by IP address
Static InetAddress getbyaddress (byte[] addr);
Get IP Address string
String gethostaddress ();
Get this IP address host name
String gethostname ();
Import java.net.InetAddress;
public class Ipdemo {public
static void Main (String args[]) throws Exception {
//Get native IP
inetaddress localhos t = Inetaddress.getlocalhost ();
SYSTEM.OUT.PRINTLN ("Address:" + localhost.gethostaddress ());
System.out.println ("Name:" + localhost.gethostname ());
Gets the specified domain name host IP (if the host has multiple IPs, only one is fetched)
inetaddress ia = inetaddress.getbyname ("www.baidu.com");
SYSTEM.OUT.PRINTLN ("Address:" + ia.gethostaddress ());
System.out.println ("Name:" + ia.gethostname ());
Gets the specified domain name host all IP
inetaddress[] iaarray = Inetaddress.getallbyname ("www.sina.com");
for (inetaddress I:iaarray) {
System.out.println ("Address:" + i.gethostaddress ());
System.out.println ("Name:" + i.gethostname ());
}
Get the InetAddress object
inetaddress IP = inetaddress.getbyaddress (new byte[] {121, 1, (byte) 189}) by IP address;
System.out.println (Ip.getcanonicalhostname ());
}
sockets and ServerSocket classes
Both socket and ServerSocket classes are located in the java.net package, which is an important class for implementing TCP protocol Communication in Java. A socket represents a client socket that is used to receive and send data; ServerSocket represents a server-side socket that listens for a client to request a connection and establishes a connection to obtain the corresponding socket object. (Details refer to Java TCP communication) Datagramsocket and Datagrampacket classes
Both Datagramsocket and Datagrampacket are located in the java.net package, which is an important class for implementing UDP protocol communication in Java. Datagrampacket is used to indicate a datagram that holds data, datagromsocket to accept or send datagrams. (Detailed reference to Java UDP communication)