Write network communication programs with Java

zhaozj2021-02-08  238

Write network communication programs with Java

Author: Yu Hongbin, Ma Junguang, car cedar

I. Java and network communication

Java is a language suitable for distributed computing environment, especially the Internet programming. This is not only just that Java has good security and portability, and Java has provided rich network library support for Internet programming. With these network libraries, you can easily write multiple types of network communication programs.

TCP / IP protocol is the most popular protocol today, and it is also an Internet of INTERNET, which represents a set of protocols. In addition to the Transmission Control Protocol TCP and Internet Protocol IP, TCP / IP protocols also include other protocols such as UDP, FTP, UUCP, ICMP, etc.

The general TCP / IP network data communication can be divided into two different communication protocols, one is a connection-oriented communication protocol, which must be connected at both ends before data transmission, and the transmitted data is not It will be lost, which is called TCP, also known as stream; another way is a non-connection mode, that is, before transmitting data, it is not necessary to establish a connection, but to packet the data to be transmitted into one group. Using this method, since there is no additional control, the transmitted data may be lost. This method is called UDP, also known as DataGram.

TCP and UDP are communication protocols on the transport layer, as well as the most commonly used communication protocols on the general TCP / IP network, and their use, such as TCP is more reliable, so it is used in applications that do not allow data loss. UDP is more applied to the processing speed, and the data transmission reliability requirement is not very high, such as data broadcasting. These two different protocols are also supported in Java, and their support is provided in the form of class libraries. Support for TCP communication is provided through the Socket and Serversocket class, which provides the DataGramsocket and DataGrampacket classes for UDP communication. They are all included in the Java.net class library. In this article, we mainly explore the writing of TCP communication programs, and the writing of UDP communication programs is similar.

Second, the writing of the communication program

Before further discuss, let's take a look at the definition of the Socket and ServerSocket classes (Table 1) and (Table 2), and see the online documentation of Sun JDK or Microsoft VJ 1.1 for details.

To write a network communication program using the features provided above, we can divide the two-ends to communicate into a server and client-side, which is to establish a so-called client / server programming mode. A Serversocket object must first be established first on the server side, then waits for access to the client. In the client, it is to create a socket object directly to the server side. If the connection is successful, the server will generate a socket object, and then we can use this socket object to communicate with the client-side Socket object. A reliable connection is created between the server and the client, the client and server can transmit data on this connection. The client issues a request, the server monitors the request from the client and provides the corresponding service for the client.

Based on the above principles, we have written a simple client / server mode network communication program. On the server side, the server listening to the client's request, the Socket connection is requested for each client to serve the client. The service provided is just to read a line of text from the client and send it back to the client. The following is a server-side communication program.

Import java.io. *;

Import java.net. *;

Class JavaServer Extends thread {

Serversocket Server;

Public javaserver () {

Try {

Server = New Serversocket (600);

Catch (IOException E) {

System.out.Println ("Cannot Create Server");

System.exit (0);

}

System.out.println ("Now Socket Server Will Start");

THIS.START ();

}

Public void run () {

Try {

While (true) {

Socket Client = Server.Accept ();

Service ss = new service (client);

}

}

Catch (IOException E) {

System.out.Println ("Cannot Provide Service!");

System.exit (1);

}

}

Public static void main (string args []) {

String data;

DataInputstream Keyinput;

New javaserver ();

Keyinput = New DataInputStream (System.in);

Try {

Data = Keyinput.readline ();

}

Catch (IOException E) {

Return;

}

IF (Data.Equals ("Quit")) System.exit (1);

}

}

Class service extends thread {

String data;

DataInputStream INPUTS;

PRINTSTREAM OUTPUTS;

Socket client;

Public service (socket clientsocket) {

Client = clientsocket;

Try {

Inputs = New DataInputStream

Client.getInputStream ());

Outputs = New PrintStream

Client.getOutputStream ());

}

Catch (IOException E) {

System.out.println ("Cannot Connect With Client!");

Return;

}

THIS.START ();

}

Public void run () {

Try {

While (true) {

Data = INPUTS.READLINE ();

IF (DATA == NULL) BREAK;

Else {

Outputs.println (DATA);

System.out.println ("From Clom Client: DATA);

}

}

}

Catch (IOException E) {

System.out.println ("Read Data Error");

}

Try {

Client.Close ();

}

Catch (IOException E) {

System.out.Println ("Cannot Close Socket");

}

}

}

In the above program, we use a multi-threaded mechanism. JavaServer and Service objects themselves are a thread. The JavaServer object first creates a Serversocket object and launches the running of the thread. Its Run () method is used to listen to the connection from the client. Whenever there is a new client connection, ServerSocket creates a new Socket class instance and creates a service object while startling this object's thread. Each service object is used to complete the task of communication with the client, providing a service. This allows the server to connect to multiple clients while providing a number of clients. When receiving a Quit string from a standard input, the server exits the run. In the client, first create a socket object for communication with the server. It reads data from standard input, passing these data to the server, and then read answers from the server, and then writes these response information to standard output. When 5 lines of data are read, the client program will exit. The following is a client-end communication program.

Import java.io. *;

Import java.net. *;

Class javaclient {

Public static void main (string args []) {

String data;

Socket client;

DataInputStream INPUTS;

DataInputstream Keys;

PRINTSTREAM OUTPUTS;

INT i = 0;

Try {

Client = New Socket ("172.17.3.2", 600);

INPUTS = New

DataInputStream (Client.getInputStream ());

Outputs = New PrintStream

Client.getOutputStream ());

Keys = New DataInputStream (System.in);

}

Catch (IOException E) {

System.out.println ("Cannot Connect

WITH SERVER ");

Return;

}

Try {

While (i <5) {

Data = Keys.Readline ();

Outputs.println (DATA);

System.out.println ("echo from

Server: " INPUTS.READLINE ());

i ;

}

}

Catch (IOException E) {

System.out.println ("IOEXCEPTION

");

}

Try {

System.out.println ("now Will

End this program ");

Client.Close ();

}

Catch (IOException E) {

System.out.println ("System Cannot

Close Socket ");

}

}

}

Third, conclude

Through the above discussion, it is very simple to write network communication programs in Java language, which is mainly because Java language itself is a language-oriented language. Java provides a number of class libraries that can be used to access standard Internet protocols, which support a variety of Internet protocols, including: FTP, HTTP, NNTP, and WWW, etc., which greatly simplifies network programming, which can be more convenient to write function Perfect application.

The above is just our coarse discussion of the network communication program for Java languages. The benefits of using Java language for programming are different. May we can play the role of throwing bricks.

Contact address: Graduate Postgraduate Postal Code: 6 Series, Harbin University of Engineering Postal Code: 150001

Contact number: (0451) 2519605

转载请注明原文地址:https://www.9cbs.com/read-1671.html

New Post(0)