Java Socket make a simple call program This article comes from: www.chinajavaworld.com Author: Xiao-feng (2001-12-30 08:30:00)
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 do I use socket to communicate with server communications in Java to communicate 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, the server has 3 main features: 1. Enter the server to detect the information of the specified port by constructing an instance of a ServerSocket class in the Java.Net class library. 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 usage method 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 is an integer as a serverator to specify a given port you can use, as follows: ServerSocket MyServersocket = New Serversocket (100); If you can keep capturing anomalies every time you construct 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 (); 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. Its 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 by the Socket class. Below we explain how to communicate online through a network programming instance.
II. 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 DIS; PrintStream Ps; Try {// Register Service Server = New ServerSocket (4321); Socket = Server.Accept (); // Listening Window, Waiting Connect System.Out.println ("Server OK"); System.out.Println ("***************************************************************************************************************************************************************************** ******************* "); System.out.Println ("); // Get input / output stream IS = socket.getinputstream (); OS = socket.getOutputStream (); // build 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 client's message ..."); system.out.println (""); s = disp.readline (); // Read the string from the Client System.out.println ("Client Said:" S); // Printing the string IF (S.trim (). Equals ("bye")) Break; // If it is "BYE", quit System.out .print ("You Say:"); s = in.readLine (); // Read the string ps.println (s) of the user entered; // Pass the read string to Clien T IF (S.trim (). Equals ("BYE")) Break; // If it is "BYE", exit} // Close connection dis.close (); // Turn off data input flow ps.close () ; // Close the data output stream is.close (); // Turn the input stream os.close (); // Turn off the output stream socket.close (); // Turn the sockey} catch (Exception E) {System.out. PRINTLN ("Error:" E);}}}}}} cluster 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 parameters No system.out.println ("You Forget the name of the server!"); System.out.println ("See Also: MyClient Yxf" ); System.exit (1); // exit if the parameters are not added} Socket Socket; string s = "yxfsoft @