An example of Java communication program (transfer)

xiaoxiao2021-03-06  17

This article describes a method of implementing servers and client communication with Java's socket and Serversocket. The principle is to establish a ServerSocket port on the server side and listen, establish the socket when receiving the client's connection request, get the input and output flow of the socket. Create new sockets in the client and get the input and output stream. The server and client input and output streams are established after using the WriteLine () and Println () methods to send and receive data from each other. To improve code efficiency, establish two class clientagent and serverat.

First, the client graphics interface

In this example, the graphical interface of the client is implemented with SWING, the server side does not use the graphical interface.

public class javaClient extends JFrame implements ActionListener // JFrame to as base class to implement the interface ActionListener {JButton sendButton; // "Send" button JTextField inputField; // input box JTextArea outputAera; // server returns to block public javaClient () // in The initialization of the graphical interface is completed in the constructive function {INPUTFIELD = New JtextField ("Enter ..."); // For the text box for the client input OutputArea = New Jtextarea ("Server Return"); // Display the server Returns data Text domain sendbutton = new jbutton ("send"); jPanel panel = new jPanel (); // New panel panel.setLayout ()); // Setting the panel style for borderLayoutPanel.Add (Inputfield, BorderLayout.North) ; // Place the control panel.add (Outputarea, BorderLayout.center); SetContentPane (PANEL);}} 2, client communication class clientagent New Custom Package Clientagent, including class Clientagent This class is used to perform interactions with the server, separating interfaces and communication classes to make the program debugging and maintenance easier. The role of the ClientAgent class includes: 1. Creating and connecting to the server in the constructive function. 2, sendRequest () method sends data to the server. 3, getResponse () method gets responding from the server side 1. Member variable

The CLIENTAGENT class includes: PrintStream OPS; // Output stream (pointing server) DataInputStream IPS; // Enter stream (from server) String CLTREQUEST; / / client request string svrresponse; // server response

2. Constructing the CLIENTAGENT (STRING ServerName, INT port) contains two parameters, receives the server name and port number passed from the JavaClient's main () function.

public clientAgent (String serverName, int port) {try {Socket clientSocket = new Socket (serverName, port); // build Socketops = new PrintStream (clientSocket.getOutputStream ()) according to the server name and port number; // get the output stream Socket IPS = New DataNputStream (ClientSocket.getinputStream ()); // Get input stream of Socket} catch (Exception E) {system.out.println ("Unable to connect server!");}} 3, sendRequest () method

Public void sendRequest (string request) {Ops.println (request); // write strings to Socket} 4, getRespone () method public string getResponse () {string str = new string (); try {STR = ips.readline (); // From the input stream of the socket into strings} catch (ioException e) {} // must capture error RETURN STR;}

Third, the main () function and event processing of the client JavaClient class 1, the main () function has the above pave, the main () function becomes very simple

Public static void main (string [] args) {javaclient frame = new javaclient (); frame.pack (); // Note Javaclient is a derived class of JFrame, calling base class frame.setVisible (TRUE); CA = New ClientAgent ("127.0.0.1", 1001); // Pass server name and port number}

2, capture and process events that click "Send" button

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

New Post(0)