Java is a simplest call program

xiaoxiao2021-03-06  36

The network programming in Java is a very important part and is one of the places where it is programmed. There is a special Java.Net class library in Java to manage network programming.

Let's first introduce how customers communicate with the server with the server in Java. Finally, a simpler call program is introduced.

1. How to use Socket to communicate with the server

Communication programming between client / servers in Java. Socket is a valid endpoint for communication between two entities. The source IP address and source port, end point IP address, and end point port can be obtained via Socket. The user can connect multiple sockets into the same port so that there can be multiple connections for a single port. Creating a distributed program that can be used by many people via Socket Customer / Server, and all customers can work with unified front ends and communicate with the server.

To communicate with the server must have three conditions: server programs, clients, and socket programs that connect them. These three parts are unable to. However, there are many ways between the client and the server, another method is to use the customer as a request, put the server as a given person. Let's take a look at the Java server programming.

In Java, there are 3 main features in the server:

1. In the Java.Net class library, the server can detect the information of the specified port by constructing an instance of a ServerSocket class. Use the Accept () method in Serversocke to enable the server to detect the activity of the specified port. In addition, the server is also responsible for testing customers that are connected to it.

· The instance of the Socket class represents the customer and the server connection success. By programming, multiple users can be connected to the server through the same port, which is an instance of the Socket class.

2. You can send and capture data with the GetInputStream () and getOutStream () methods of the Socket class. The method of use is as follows:

Try {

Serversocket MyServersocket = New Serversocket (100);

Socket my100socket = myserversocket.accept ();

} catch (exception e) {}

In the above code, first construct an instance of a ServerSocket class, and pass it to it an integer as a serverator to specify a given port that can be used, as follows:

Serversocket MyServersocket = New Serversocket (100);

In the Java program, if you can maintain the exception event each time you construct a ServerSocket, you can always specify the port that is ready to use. The following code uses the accept () method to detect port activities.

Socket my100socket = myserversocket.accept ();

The accept () method continues to perform the interrupt executor until the user's connection request is received. Once the customer's connection is successful, My100Socket represents the connection and can send and receive data.

Finally, let's take a look at how the customer requests to connect. The connection method is as follows:

Try {

Socket MySocket = New Socket ("www.cpcw.com", 100);

} catch (exception e) {}

It is possible to see the above code and is also implemented through the Socket class. Below we explain how to communicate online through a network programming instance.

Two. One simplest call program

Call server:

Import java.net. *;

Import java.io. *;

Import java.lang. *;

Public class myserver {

Public static void main (string args []) {

Serversocket Server;

Socket socket;

String S;

InputStream IS; OUTPUTSTREAM OS;

DataInputstream DIS;

PRINTSTREAM PS;

Try {

// Register in port 4321

Server = new serversocket (4321);

Socket = server.accept (); // Listening to the window, wait for the connection

System.out.println ("Server OK");

System.Out.println ("******************************************************************************************************************************************************************************************** ***** ");

System.out.println ("");

// Get the input / output flow of the corresponding socket

IS = Socket.getInputStream ();

OS = socket.getOutputStream ();

// Establish a data stream

DIS = New DataInputStream (IS);

PS = New PrintStream (OS);

DataInputStream in = New DataInputStream (System.in);

While (true) {

System.out.println ("");

System.out.println ("Please wait." "");

System.out.println ("");

s = dish, Readline (); // Read the string from the client

System.out.println ("Client Said:" S); // Print the string

IF (S.trim (). Equals ("BYE")) Break; // If it is "bye", you will exit

System.out.print ("You Say:");

s = in.readline (); // read the string of the user input

Ps.println (s); // Pass the read string to the client

IF (S.trim (). Equals ("BYE")) Break; // If it is "bye", you will exit

}

// Close connection

Dis.close (); // Turn off the data input stream

Ps.close (); // Turn off data output flow

Is.close (); // Close input stream

Os.close (); // Turn off output flow

Socket.close (); // Close Sockey

}

Catch (Exception E) {

System.out.println ("Error:" E);

}

}

}

Call client

Import java.net. *;

Import java.io. *;

Import java.lang. *;

Public class myclient {

Public static void main (string args []) {

IF (args.length <1) {// judgment command plus parameter is not

System.out.println ("You Forget The Name of the Server!");

System.out.println ("See Also: MyClient YXF");

System.exit (1); // Exit if you don't add parameters

}

Socket socket;

String s = "yxfsoft@263.net";

String Len;

InputStream IS;

Outputstream OS;

DataInputstream DIS;

PRINTSTREAM PS;

Try {

/ / Apply to the host name Args [0] to apply for connection

/ Note that the port number should be consistent with the server: 4321Socket = New Socket (Args [0], 4321);

System.out.Println ("Client OK");

System.Out.println ("******************************************************************************************************************************************************************************************** ***** ");

System.out.println ("");

// Get the input / output flow of the corresponding socket

IS = Socket.getInputStream ();

OS = socket.getOutputStream ();

// Establish a data stream

DIS = New DataInputStream (IS);

PS = New PrintStream (OS);

DataInputStream in = New DataInputStream (System.in);

While (true) {

System.out.print ("You Say:");

s = in.readline (); // read the string of the user input

Ps.println (s); // Pick the read string to Server

IF (S.trim (). Equals ("BYE")) Break; // If it is "bye", you will exit

Else

{

System.out.println ("");

System.out.Println ("Please wait server's message ...");

System.out.println ("");

}

s = disp.readline (); // get a string from the server

System.out.println ("Server SAID:" S); // Print string

IF (S.trim (). Equals ("BYE")) Break; // If it is "bye", you will exit

}

// Close connection

Dis.close (); // Turn off the data input stream

Ps.close (); // Turn off data output flow

Is.close (); // Close input stream

Os.close (); // Turn off output flow

Socket.close (); // Close Socket

}

Catch (Exception E) {

System.out.println ("Error:" E);

}

}

}

Download source file: Client Engineering, Server Engineering. The programming environment is Visualj 6.0.

Readers first run MyServer.exe on a machine (MyServer.exe in the server engineering), then open the console (DOS window) on the same machine or with the first machine, and then go to The directory where MyClient.exe (MyClient.exe in the client engineering) runs the client program: MyClient ServerHostname or MyClient ServerHostip, ServerHostName is the machine name of the running server, ServerHostip's IP address of the machine running the server. You can talk to each other after running. This call program can only be rotated, and readers can enrich their feature based on it.

Author: Xiao-feng

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

New Post(0)