首页 > 代码库 > Java网络通信基础编程
Java网络通信基础编程
方式一:
服务器端(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 * 服务端 9 */ 10 public class Server { 11 // 端口号 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 // 进行阻塞 19 Socket socket = server.accept(); 20 // 创建一个程序执行客户端的任务 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 }
采用多线程来处理接收到的请求(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 @Override 16 public void run() { 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 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 * 客户端 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 BufferedReader 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 // 向服务器端发送数据 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: 服务器端回送响应的数据。
Java网络通信基础编程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。