For a fully functional Socket, it must contain the following basic structure. The working process includes the following four basic steps:
(1) create a Socket;
(2) Open the input/output stream connected to the Socket;
(3) perform read/write operations on the Socket according to certain protocols;
(4) Disable Socket.
The third step is the key step for programmers to call Socket and implement program functions. The other three steps are basically the same in various programs.
If an error occurs during socket creation, an IOException is generated and must be processed in the program. Therefore, exceptions must be caught or thrown when a Socket or ServerSocket is created.
(1) The following is a typical process of creating a client Socket.
Try {Socket socket = new Socket ("127.0.0.1", 4700); // 127.0.0.1 is the default local address in TCP/IP protocol} catch (IOException e) {System. out. println ("Error:" + e );}
(2) The following is a typical process of creating Server-side ServerSocket.
ServerSocket server = null; try {server = new ServerSocket (4700); // create a ServerSocket to listen to customer requests on port 4700} catch (IOException e) {System. out. println ("can not listen to:" + e);} Socket socket = null; try {socket = server. accept (); // accept () is a blocking method. Once a customer requests, it returns a Socket object for interaction with the customer.} catch (IOException e) {System. out. println ("Error:" + e );}
The above program is a typical working mode of the Server, but here the Server can only receive one request. After receiving the program, the Server will quit. In actual applications, it always keeps receiving requests cyclically. Once a customer requests, the Server always creates a service thread to serve new customers, and listens on its own. The accept () in the program is a blocking function. The so-called obstructive method means that after the method is called, it will wait for the customer's request until a customer starts and requests to connect to the same port, then accept () returns a socket corresponding to the customer. At this time, both the client and the service provider have established Sockets for communication. Next, each socket opens its own input/output streams.
After learning about the principles, we can start to write
Write on the server
Server. java
Package net; import java.net. *; import java. io. *; public class server {public static void main (String [] args) {ServerSocket ss = null; Socket s = null; try {ss = new ServerSocket (6000 ); // create a ServerSocket to listen to customer requests on the port s = ss. accept (); // accept () is a blocking method. Once a customer requests, it returns a Socket object for interaction with the customer OutputStream OS = s. getOutputStream (); // get the output stream InputStream is = s. getInputStream (); // gets the OS of the inbound/outbound stream. write ("hello world ". getBytes (); // The output flows to the client to send data // The input stream reads data from the network byte [] buf = new byte [100]; int length = is. read (buf); System. out. println (new String (buf, 0, length); OS. close (); // close the output stream is. close (); // close the input stream s. close (); close the client ss. close (); close server} catch (Exception e) {e. printStackTrace ();}}}
Client Compilation
Client. java
Package net; import java.net. *; import java. io. *; public class Client {public static void main (String [] args) {try {Socket s = new Socket (InetAddress. getByName (null), 6000); // Socket s = new Socket ("127.0.0.1", 6000); // create a client to intercept the child OutputStream OS = s. getOutputStream (); // get the output stream InputStream is = s. getInputStream (); // get the input stream byte [] buf = new byte [100]; int len = is. read (buf); System. out. println (new String (buf, 0, len); OS. write ("hello, this is fan ". getBytes (); OS. close (); is. close (); s. close ();} catch (Exception e) {e. printStackTrace ();}}}
We first compile sever. java for listening.
Compile client. java