Java Network Communication Programming and java Network Communication Programming

Source: Internet
Author: User

Java Network Communication Programming and java Network Communication Programming

Method 1: Synchronous blocking (BIO ):

Server ):

1 package com. ietree. basicskill. socket. mode1; 2 3 import java. io. IOException; 4 import java.net. serverSocket; 5 import java.net. socket; 6 7/** 8 * Server 9 */10 public class Server {11 // PORT number 12 final static int PORT = 8765; 13 public static void main (String [] args) {14 ServerSocket server = null; 15 try {16 server = new ServerSocket (PORT); 17 System. out. println ("Server start ...... "); 18 // block 19 Socket Socket = server. accept (); 20 // create a program to execute the client task 21 new Thread (new ServerHandler (socket )). start (); 22 23} catch (IOException e) {24 e. printStackTrace (); 25} finally {26 if (server! = Null) {27 try {28 server. close (); 29} catch (IOException e) {30 e. printStackTrace (); 31} 32} 33 server = null; 34} 35} 36}

Use multiple threads to process received requests (ServerHandler ):

1 package com. ietree. basicskill. socket. mode1; 2 3 import java. io. bufferedReader; 4 import java. io. IOException; 5 import java. io. inputStreamReader; 6 import java. io. printWriter; 7 import java.net. socket; 8 9 public class ServerHandler implements Runnable {10 private Socket socket; 11 public ServerHandler (Socket socket) {12 this. socket = socket; 13} 14 15 @ Override16 public void run () {17 BufferedReade R in = null; 18 PrintWriter out = null; 19 try {20 in = new BufferedReader (new InputStreamReader (this. socket. getInputStream (); 21 out = new PrintWriter (this. socket. getOutputStream (), true); 22 String body = null; 23 while (true) {24 body = in. readLine (); 25 if (body = null) {26 break; 27} 28 System. out. println ("Server:" + body); 29 out. println ("server-side response data. "); 30} 31} catch (Exception e) {32 e. printStackTrace (); 33} finally {34 if (in! = Null) {35 try {36 in. close (); 37} catch (IOException e) {38 e. printStackTrace (); 39} 40} 41 if (out! = Null) {42 try {43 out. close (); 44} catch (Exception e) {45 e. printStackTrace (); 46} 47} 48 if (socket! = Null) {49 try {50 socket. close (); 51} catch (IOException e) {52 e. printStackTrace (); 53} 54} 55 socket = null; 56} 57} 58}

Client ):

1 package com. ietree. basicskill. socket. mode1; 2 3 import java. io. bufferedReader; 4 import java. io. IOException; 5 import java. io. inputStreamReader; 6 import java. io. printWriter; 7 import java.net. socket; 8 9/** 10 * Client 11 */12 public class Client {13 final static String ADDRESS = "127.0.0.1"; 14 final static int PORT = 8765; 15 public static void main (String [] args) {16 Socket socket = null; 17 Buffere DReader in = null; 18 PrintWriter out = null; 19 try {20 socket = new Socket (ADDRESS, PORT); 21 in = new BufferedReader (new InputStreamReader (socket. getInputStream (); 22 out = new PrintWriter (socket. getOutputStream (), true); 23 24 // send data 25 out to the server. println ("request data received from the client ...... "); 26 String response = in. readLine (); 27 System. out. println ("Client:" + response); 28} catch (Exception e) {29 e. printStackTra Ce (); 30} finally {31 if (in! = Null) {32 try {33 in. close (); 34} catch (IOException e) {35 e. printStackTrace (); 36} 37} 38 if (out! = Null) {39 try {40 out. close (); 41} catch (Exception e) {42 e. printStackTrace (); 43} 44} 45 if (socket! = Null) {46 try {47 socket. close (); 48} catch (IOException e) {49 e. printStackTrace (); 50} 51} 52 socket = null; 53} 54} 55}

Program output:

Server:

Server start... Server: receives the request data from the client ......

Client:

Client: The data returned by the server.

Non-blocking synchronization (NIO)

 

 

Asynchronous non-blocking (AIO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.