首页 > 代码库 > java 26 - 8 网络编程之 TCP协议的练习

java 26 - 8 网络编程之 TCP协议的练习

TCP练习:

1、客户端键盘录入,服务器输出文本文件

 

  客户端代码:

 1 public class ClientDemo { 2     public static void main(String[] args) throws IOException { 3  4         // 创建socket对象 5         Socket s = new Socket("172.19.12.233", 10010); 6  7         // 创建键盘输入流,用转换流包装成字符流 8         BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 9 10         // 把通道中的字节流用转换流包装成字符流11         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(12                 s.getOutputStream()));13         14         //读取键盘输入的数据,并传输到服务器端15         String line = null;16         while((line = br.readLine()) != null){17             //设定个关闭键盘的命令18             if(line.equals("esc")){19                 break;20             }21             bw.write(line);22             bw.newLine();23             bw.flush();24         }25         //释放资源26         s.close();27         28     }29 }

 

  服务器代码:

 1 public class ServerDemo { 2     public static void main(String[] args) throws IOException { 3  4         // 创建ServerSocket对象,监听 5         ServerSocket ss = new ServerSocket(10010); 6  7         // 创建socket对象 8         Socket s = ss.accept(); 9 10         // 把通道中的字节流包装成字符流11         BufferedReader br = new BufferedReader(new InputStreamReader(12                 s.getInputStream()));13 14         // 封装文件目录15         BufferedWriter bw = new BufferedWriter(new FileWriter("TCP.txt"));16 17         // 把读取到的数据写入到文件中18         String line = null;19         while ((line = br.readLine()) != null) {20             bw.write(line);21             bw.newLine();22             bw.flush();23         }24 25         // 释放资源26         s.close();27         bw.close();28 29     }30 }

 

2、客户端文本文件,服务器输出到控制台

  

  客户端代码:

 1 public class ClientDemo { 2  3     public static void main(String[] args) throws IOException, IOException { 4  5         // 创建socket传送对象 6         Socket s = new Socket("172.19.12.233", 10010); 7  8         // 封装文本文件,字符流读取里面的数据 9         BufferedReader br = new BufferedReader(new FileReader("InetAddressDemo.java"));10         // 封装通道里的字节流为字符流11         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(12                 s.getOutputStream()));13 14         // 把读取到的数据输出到服务器端15         String line = null;16         while ((line = br.readLine()) != null) {17             bw.write(line);18             bw.newLine();19             bw.flush();20         }21         22         //释放资源23         s.close();24         br.close();25     }26 }

 

    服务器代码:

 1 public class ServerDemo { 2     public static void main(String[] args) throws IOException { 3  4         // 创建ServerSocket对象,监听 5         ServerSocket ss = new ServerSocket(10010); 6  7         // 创建socket对象 8         Socket s = ss.accept(); 9 10         // 把通道中的字节流包装成字符流11         BufferedReader br = new BufferedReader(new InputStreamReader(12                 s.getInputStream()));13 14         // 接收客户端发来都数据15         String line = null;16         while ((line = br.readLine()) != null) {17             System.out.println(line);18         }19 20         //释放资源21         s.close();22 23     }24 }

 

 

3、客户端读取文本文件,服务器输出文本文件

  

  客户端的代码

 

 1 public class ClientDemo { 2     public static void main(String[] args) throws IOException { 3  4         // 创建Socket对象 5         Socket s = new Socket("172.19.12.233", 10010); 6  7         // 封装文件目录 8         BufferedReader br = new BufferedReader(new FileReader( 9                 "InetAddressDemo.java"));10 11         // 把通道内的字节流包装成字符流12         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(13                 s.getOutputStream()));14 15         // 获取文件中的数据,并输出给服务器16         String line = null;17         while ((line = br.readLine()) != null) {18 19             bw.write(line);20             bw.newLine();21             bw.flush();22         }23         24         s.close();25         br.close();26 27     }28 }

 

 

  服务器的代码:

 1 public class ServerDemo { 2     public static void main(String[] args) throws IOException { 3  4         // 创建ServerSocket对象,监听 5         ServerSocket ss = new ServerSocket(10010); 6  7         // 创建socket对象 8         Socket s = ss.accept(); 9 10         // 把通道内的字节流包装成字符流11         BufferedReader br = new BufferedReader(new InputStreamReader(12                 s.getInputStream()));13 14         // 封装文件目录15         BufferedWriter bw = new BufferedWriter(new FileWriter("TCPto.txt"));16 17         // 获取数据,并输出到文件中18         String line = null;19         while ((line = br.readLine()) != null) {20                 bw.write(line);21                 bw.newLine();22                 bw.flush();23         }24         25         //释放资源26         s.close();27         bw.close();28     }29 }

 

java 26 - 8 网络编程之 TCP协议的练习