Client programming
The server-side program needs to use the Java.net.Socket class. In a server/client network application, each TCP connection request is initiated by the client to the server side. The establishment of a client program typically requires the following 4 steps:
1: Create an instance object of class Java.net.Socket in the client application and connect to the server side. When you create an instance object that connects to a socket, you need to develop the server-side hostname and port number to connect to (this port number must be
ServerSocket construction instances on the server side are monitored at the same port number.
2: Call the client socket socket method getInputStream () and Getoutputstream () to obtain the input stream and output stream corresponding to the socket.
3: Data is obtained from the server side by obtaining the input stream and the output stream, and data obtained from the servers and those that need to be sent to the server are processed.
4: After the data transfer is complete, turn off the input stream and output stream and socket.
A simple example of a socket client program is written below. The function of the program is to connect to the 80 port of the server, and output the server side and client information. The code is as follows:
ImportJava.io.*;Importjava.net.*; Public classwebclientdemo{ Public Static voidMain (String []args)throwsioexception{//Create a socket instance and connect to the 80 port on the Sina networkSocket client=NewSocket ("www.sina.com.cn", 80);//Output service-side informationSYSTEM.OUT.PRINTLN ("Server IP is:" +client.getinetaddress ()); System.out.println ("Server port is:" +Client.getport ());//Output Client InformationSYSTEM.OUT.PRINTLN ("Client IP is:" +client.getlocaladdress ()); System.out.println (The client port number is: "+Client.getlocalport ()); Client.close ();}}
The results of the operation are as follows:
The server IP is: www.sina.com.cn/202.108.33.60 server port is: The IP of the clientis:/192.168.3.112 client port number is: 4208
UDP Network Programming
UDP is less widely used than TCP, and several major application-layer protocols such as HHTP, FTP, and SMTP are TCP protocols. However, with the development of computer network, the UDP protocol is gradually showing its advantages. The information can be split into a
of unrelated information, UDP is a good choice, especially in situations where there is a strong need for real-time interactivity, such as online games, video conferencing, stock information, etc. The following describes how to use Java to implement UDP network programming.
UDP Communication Concepts
UDP communication is also known as packet communication. Packet-based network traffic used by the UDP protocol. A packet is a form of packet switching, in which all the data to be transmitted is segmented into packets and then sent out. It belongs to the non-connected type, which is packed into each package
(packet) is sent out as a separate message, so it is called a packet.
UDP communication is similar to the process of sending and winning letters through the post Office, and it is not necessary to establish a dedicated connection with another person until the letter is sent, just by knowing the address and number of the other party (equivalent to the service address and port numbers). Since there is no special
The connection is established, so it is not guaranteed that the packets will reach the specified host successfully or that the packets will reach the specified host in the order in which they are sent.
When choosing a protocol to use, you must be cautious about choosing UDP. In an environment where the quality of the network is unsatisfactory, the packet loss of UDP protocol is more serious. However, due to the characteristics of UDP: it does not belong to the connection protocol, so the resource consumption is small, processing speed
Fast, so often audio, video, and normal data are transmitted with more UDP, as they are not affected by the acceptance of the results even if they occasionally lose one or two packets. For example, we chat with the IQC and QQ is the use of UDP protocol
。
In the UDP network program of Java, It is used primarily in two classes: the Java.net.DatagramSocket and Java.net.DatagramPacket classes, where the Datagramsocket class is used to send and receive data packet sockets, while the Datagrampacket class is
The packets that are passed by UDP, that is, packaged data. The common methods of the Datagramsocket class are as follows:
Method Name Method Description
Send (Datagrampacket p) sends a packet
Recieve (Datagrampacket p) receives a packet
Disconnect () Disconnect this socket
Close () closes the socket
Features of UDP
Although UDP is an unreliable protocol, it is an ideal protocol for distributing information. For example, the stock market is reported on the screen, air information is displayed on the screen, and so on. UDP is also used in Routing Information Protocol (Routing information Protocol,rip)
To modify the routing table in the In these applications, if one message is lost, a new message will replace it in a few seconds. UDP is widely used in multimedia applications, such as the RealAudio developed by Progressive Networks.
The RealAudio Audio-ondemand Protocol protocol is the Protocol that runs on UDP, and most internet telephony software products are also running on UDP. The UDP protocol has several features:
The UDP protocol is a no-connection protocol that transmits data before the source and terminal do not establish a connection, and when it wants to transmit it simply crawls data from the application and puts it on the network as quickly as possible. At the sending end, UDP transmits data at a speed that is only
The speed at which data is generated by the application. The ability of the computer and the limitation of transmission bandwidth; At the receiving end, UDP places each message segment in the queue, and the application reads a message segment from the queue each time.
Because the transfer data does not establish a connection, there is no need to maintain the connection state, including sending and receiving status, so a server can transmit the same message to multiple users at the same time.
The header of a UDP packet is short, with only 8 bytes, and the extra overhead of a 20-byte packet relative to TCP is minimal. Throughput to the UK is not controlled by the mail control algorithm, only the rate of data generated by the application software, transmission bandwidth, source end, terminal host performance limit
System.
UDP uses its best effort to deliver, i.e. it does not guarantee reliable delivery, so the host does not need to maintain a complex connection state table (which has many parameters).
UDP is message-oriented. The sender's UDP message to the application is delivered down to the IP layer after the header is added. Instead of splitting and merging, the boundaries of these messages are preserved, so the application needs to select the appropriate message size.
Application of UDP
UDP is an unreliable network protocol, so what is the use or necessity? In some cases, the UDP protocol may become very useful. Because UDP has a speed advantage over the TCP protocol. Although the TCP protocol is populated with various security
Full-security function, but in the actual implementation of the process will occupy a large amount of system overhead, no doubt the speed has been seriously affected. Anti-UDP because the information reliable transmission mechanism is excluded, the security and sequencing functions are handed over to the upper application to complete, greatly reducing
The execution time, so that the speed is guaranteed.
The earliest specification for the UDP protocol was RFC768, which was released in 1980. Although the release time has been long, the UDP protocol continues to play a role in mainstream applications, including video conferencing systems, and many applications have proved that UDP exists
Value. Because these applications pay more attention to real-world performance than reliability, they can often sacrifice certain reliability (such as picture quality) for better use, such as higher frame refresh rates. This is the UDP and TCP two kinds of CO
The trade-offs of the debate. According to different environment and characteristics, the two transmission protocols will play a more important role in the future Network world.
The difference between UDP and TCP
The main difference between UDP and TCP protocols is that they differ in how to achieve reliable transmission of information. The TCP protocol includes a special delivery guarantee mechanism, which automatically sends a confirmation message to the sender when the receiving party receives information from the sender.
You will not continue to send additional information until you receive a confirmation message, or you'll be waiting until you receive a confirmation. Unlike TCP, the UDP protocol does not provide a guarantee mechanism for data transfer. If data is present during delivery from the sender to the receiving party
Packet loss, the protocol itself does not make any detection or prompt. As a result, the UDP protocol is often referred to as an unreliable transport protocol.
In contrast to the TCP protocol, another difference in the UDP protocol is how to accept multiple bursts of packets. Unlike TCP,UDP, there is no guarantee of the order in which data is sent and received. For example, an application that is located on a client sends four servers to a server
Data package:
D1
D22
D333
D4444
However, it is possible for UDP to submit the accepted data to the server's application in the following order:
D333
D1
D4444
D22
In fact, this sort of chaotic type of UDP protocol rarely occurs, and is usually only possible if the network is very congested.
Network Programming 2