Java from getting started to giving up JavaSE entry: Network Programming (Entry edition ),
To program the network, you must first understand the purpose.
Network Programming is simply to exchange data with computers on the network.
Since data interaction is required, a sender and an receiver are required.
According to the network, it is an attack ·
Of course, at this stage, computers on the Internet generally accept both data and data transmission. Therefore, these computers are all "socket-type" computers. They can be attacked and can be recovered !!!
Well, use the professional name:Client/Server.
How do two computers interact with data? See:
Perform the following steps:
I. server programs
1.1 create a server socket (ServerSocket) And bind it to the specified port.
1.2 listen to requests from the client. If a connection is received, the socket object (Socket).
1.3 obtain the input/output stream, that is, receiving or sending data.
1.4 disable socket ).
Ii. Client Program
2.1 create a socket and send a request to the specified product on the server.
2.2 After correctly connecting to the server, start receiving or sending data.
2.3 close the socket.
After the steps are analyzed, the next step is implementation.
The server code is as follows:
123456789101112131415161718192021222324252627282930313233 |
public class MyServer { private static final int SERVER_PORT = 9527 ; // Specify the listening port public MyServer() { try { ServerSocket ss = new ServerSocket(SERVER_PORT); // Create a server socket System.out.println( "The server has started and is waiting for the client ..." ); Socket s = ss.accept(); // Listen for requests from the client InputStream in = s.getInputStream(); // Obtain the input stream to receive data OutputStream out = s.getOutputStream(); // Obtain the output stream for sending data byte [] buf = new byte [ 1024 ]; // Data cache int len = in.read(buf); // Read data from the input stream String strFromClient = new String(buf, 0 , len); System.out.print( "Information from the client>" ); System.out.println(strFromClient); String strToClient = "I am also very good! " ; out.write(strToClient.getBytes()); // Send data to the output stream // Close the input stream and output stream in.close(); out.close(); // Disable the communication socket and server socket s.close(); ss.close(); System.out.println( "The server is disabled. " ); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { MyServer ms = new MyServer(); } } |
The client code is as follows:
12345678910111213141516171819202122232425262728293031 |
public class MyClient { private static final int SERVER_PORT = 9527 ; // The listening port of the server public MyClient() { try { // Because the server is also running on the local machine, the InetAddress object of the local machine is created. InetAddress address = InetAddress.getByName( "localhost" ); Socket s = new Socket(address, SERVER_PORT); // Send a request to the server listening port System.out.println( "The client has been started. " ); InputStream in = s.getInputStream(); // Obtain the input stream to receive data OutputStream out = s.getOutputStream(); // Obtain the output stream for sending data String strToServer = "Hello! " ; out.write(strToServer.getBytes()); // Send data to the output stream byte [] buf = new byte [ 1024 ]; int len = in.read(buf); // Read data from the input stream String strFromServer = new String(buf, 0 , len); System.out.print( "Answers from the server>" ); System.out.println(strFromServer); in.close();out.close(); // Close the input stream and output stream s.close(); // Close the communication socket System.out.println( "The client is closed. " ); } catch (UnknownHostException nhe) { System.out.println( "The specified host is not found ..." ); } catch (IOException ioe) { ioe.printStackTrace(); } } public static void main(String[] args) { MyClient mc = new MyClient(); } } |
Run the server first
Run the client again to find that the content on the server has changed.
Switch to the output window of the client.
The output content of the client is as follows:
If you are interested, you can modify the code so that it can be interacted by repeatedly inputting text on the console. Friends who have studied more can try to send files.
The most basic content of network programming is over here. You should like it !!!
"Software thinking" blog address:51CTO,BlogInterested friends can visit other related blog posts.