Network applications are divided into two parts: client and server, and the Socket class is a Java class responsible for handling client communication. Through this class, you can connect to a server with a specified IP or domain name, and you can send and receive data with each other with the server.
For a brief description of Socket communication, the server writes something into the Socket output stream, and the client can read the corresponding content through the Socket input stream. The Socket and Socket are connected in two directions, so the client can also write things into the corresponding Socket output stream, and then the corresponding Socket input stream of the server can read out the corresponding content.
Example 1: Brief writing method of the client (I).
Socket client = null;try{client = new Socket(Ip,Port);String msg="sent data content!";//Get the socket read and write stream, send data to the server program client.getOutputStream().write(msg.getBytes());byte[] datas = new byte[2048];//Receive data from the server program client.getInputStream().read(datas);System.out.println(new String(datas));}catch(Exception e){e.printStackTrace();} finally {if (client != null) {try {client.close();} catch (IOException e) {System.out.println("systemerr:" +e);}}} Example 2: Brief writing method of client (2).
try{client = new Socket();SocketAddress socketAddress = new InetSocketAddress(Ip,Port);client.connect(socketAddress, 3000);String msg="The accessed server returns content!";//Get the socket read and write stream, and send data to the server program client.getOutputStream().write(msg.getBytes());byte[] datas = new byte[2048];//Receive data from the server program client.getInputStream().read(datas);System.out.println(new String(datas));}catch(Exception e){e.printStackTrace();} finally {if (client != null) {try {client.close();} catch (IOException e) {System.out.println("systemerr:" +e);}}} Example 3: Complete writing of the client.
try { //1. Establish a client socket connection, specify the server location and port. Socket socket =new Socket(Ip,Port); //2. Obtain the socket read and write stream OutputStream os=socket.getOutputStream(); PrintWriter pw=new PrintWriter(os); //Input stream InputStream is=socket.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); //3. Use the stream to read and write the socket according to certain operations String sendInfo="Data information sent to the server!";pw.write(sendInfo); pw.flush(); socket.shutdownOutput(); //The corresponding String of the receiving server replyInfo=null; while(!((replyInfo=br.readLine())==null)){ System.out.println("Receive server data information: "+replyInfo); } //4. Close the resource br.close(); is.close(); pw.close(); os.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }For more information about the relevant knowledge of the client program sending and receiving data in Java Socket Communication (I), the editor will introduce it to you. For more information, please log in to the Wulin.com website to learn more!