java 网络通信编程_Java网络通信基础编程

一、同步阻塞方式(BIO)

方式一:

服务器端(Server):

1 packagecom.ietree.basicskill.socket.mode1;2

3 importjava.io.IOException;4 importjava.net.ServerSocket;5 importjava.net.Socket;6

7 /**

8 * 服务端9 */

10 public classServer {11 //端口号

12 final static int PORT = 8765;13 public static voidmain(String[] args) {14 ServerSocket server = null;15 try{16 server = newServerSocket(PORT);17 System.out.println("Server start......");18 //进行阻塞

19 Socket socket =server.accept();20 //创建一个程序执行客户端的任务

21 new Thread(newServerHandler(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 }

采用多线程来处理接收到的请求(ServerHandler):

1 packagecom.ietree.basicskill.socket.mode1;2

3 importjava.io.BufferedReader;4 importjava.io.IOException;5 importjava.io.InputStreamReader;6 importjava.io.PrintWriter;7 importjava.net.Socket;8

9 public class ServerHandler implementsRunnable {10 privateSocket socket;11 publicServerHandler(Socket socket) {12 this.socket =socket;13 }14

15 @Override16 public voidrun() {17 BufferedReader 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("服务器端回送响应的数据。");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 packagecom.ietree.basicskill.socket.mode1;2

3 importjava.io.BufferedReader;4 importjava.io.IOException;5 importjava.io.InputStreamReader;6 importjava.io.PrintWriter;7 importjava.net.Socket;8

9 /**

10 * 客户端11 */

12 public classClient {13 final static String ADDRESS = "127.0.0.1";14 final static int PORT = 8765;15 public static voidmain(String[] args) {16 Socket socket = null;17 BufferedReader in = null;18 PrintWriter out = null;19 try{20 socket = newSocket(ADDRESS, PORT);21 in = new BufferedReader(newInputStreamReader(socket.getInputStream()));22 out = new PrintWriter(socket.getOutputStream(), true);23

24 //向服务器端发送数据

25 out.println("接收到客户端的请求数据......");26 String response =in.readLine();27 System.out.println("Client: " +response);28 } catch(Exception e) {29 e.printStackTrace();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 }

程序输出:

Server:

Server start......

Server: 接收到客户端的请求数据......

Client:

Client: 服务器端回送响应的数据。

方式二:

Server类:

packagecom.ietree.basicskill.socket.basic.bio2;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;/*** Created by Administrator on 2017/5/25.*/

public classServer {final static int PORT = 8765;public static voidmain(String[] args) {

ServerSocket server= null;

BufferedReader in= null;

PrintWriter out= null;try{

server= newServerSocket(PORT);

System.out.println("Server Start......");

Socket socket= null;

HandlerExecutorPool executorPool= new HandlerExecutorPool(50, 1000);while (true) {

socket=server.accept();

executorPool.execute(newServerHandler(socket));

}

}catch(IOException e) {

e.printStackTrace();

}finally{if (in != null) {try{

in.close();

}catch(Exception e1) {

e1.printStackTrace();

}

}if (out != null) {try{

out.close();

}catch(Exception e2) {

e2.printStackTrace();

}

}if (server != null) {try{

server.close();

}catch(Exception e3) {

e3.printStackTrace();

}

}

server= null;

}

}

}

serverHandler类:

packagecom.ietree.basicskill.socket.basic.bio2;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.Socket;/*** Created by Administrator on 2017/5/25.*/

public class ServerHandler implementsRunnable {privateSocket socket;publicServerHandler(Socket socket) {this.socket =socket;

}

@Overridepublic voidrun() {

BufferedReader in= null;

PrintWriter out= null;try{try{

in= new BufferedReader(new InputStreamReader(this.socket.getInputStream()));

out= new PrintWriter(this.socket.getOutputStream(), true);

String body= null;while (true) {

body=in.readLine();if (body == null) {

System.out.println("Server:" +body);

out.println("Server Response");

}

}

}catch(IOException e) {

e.printStackTrace();

}

}finally{if (in != null) {try{

in.close();

}catch(Exception e1) {

e1.printStackTrace();

}

}if (out != null) {try{

out.close();

}catch(Exception e2) {

e2.printStackTrace();

}

}if (socket != null) {try{

socket.close();

}catch(Exception e3) {

e3.printStackTrace();

}

}

socket= null;

}

}

}

线程池HandlerExecutorPool类:

packagecom.ietree.basicskill.socket.basic.bio2;importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;/*** Created by Administrator on 2017/5/25.*/

public classHandlerExecutorPool {privateExecutorService executor;public HandlerExecutorPool(int maxPoolSize, intqueueSize) {this.executor = newThreadPoolExecutor(

Runtime.getRuntime().availableProcessors(),

maxPoolSize,120L,

TimeUnit.SECONDS,new ArrayBlockingQueue(queueSize)

);

}public voidexecute(Runnable task) {

executor.execute(task);

}

}

客户端Client类:

packagecom.ietree.basicskill.socket.basic.bio2;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.Socket;/*** Created by Administrator on 2017/5/25.*/

public classClient {final static String ADDRESS = "127.0.0.1";final static int PORT = 8765;public static voidmain(String[] args) {

Socket socket= null;

BufferedReader in= null;

PrintWriter out= null;try{

socket= newSocket(ADDRESS, PORT);

in= new BufferedReader(newInputStreamReader(socket.getInputStream()));

out= new PrintWriter(socket.getOutputStream(), true);

out.println("Client request");

String response=in.readLine();

System.out.println("Client:" +response);

}catch(Exception e) {

e.printStackTrace();

}finally{if (in != null) {try{

in.close();

}catch(Exception e1) {

e1.printStackTrace();

}

}if (out != null) {try{

out.close();

}catch(Exception e2) {

e2.printStackTrace();

}

}if (socket != null) {try{

socket.close();

}catch(Exception e3) {

e3.printStackTrace();

}

}

socket= null;

}

}

}

程序输出:

Server端:

Server Start......

Server:Client request

Client端:

Client:Server Response

二、 同步非阻塞(NIO)

三、异步非阻塞(AIO)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值