码迷,mamicode.com
首页 > 其他好文 > 详细

TCP数据传输(三)

时间:2016-05-13 01:13:50      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

练习题2:客户端键盘录入,服务器输出到控制台

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.ServerSocket;

import java.net.Socket;

 

public classServerDemo {

   public static void main(String[] args) throws IOException {

      // 创建服务器Socket对象

      ServerSocketss = newServerSocket(22222);

 

      // 监听客户端连接

      Sockets = ss.accept();

 

      // 包装通道内容的流

      BufferedReaderbr = newBufferedReader(newInputStreamReader(

            s.getInputStream()));

      Stringline = null;

      while ((line = br.readLine())!= null){

         System.out.println(line);

      }

 

      // br.close();

      s.close();

      // ss.close();

   }

}

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.Socket;

 

/*

 * 客户端键盘录入,服务器输出到控制台

 */

public classClientDemo {

   public static void main(String[] args) throws IOException {

      // 创建客户端Socket对象

      Sockets = newSocket("219.216.80.19", 22222);

 

      // 键盘录入数据

      BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));

      // 把通道内的流给包装一下

      BufferedWriterbw = newBufferedWriter(newOutputStreamWriter(

            s.getOutputStream()));

 

      Stringline = null;

      while ((line = br.readLine())!= null){

         // 键盘录入数据要自定义结束标记

         if ("886".equals(line)) {

            break;

         }

         bw.write(line);

         bw.newLine();

         bw.flush();

      }

 

      // 释放资源

      // bw.close();

      // br.close();

      s.close();

   }

}

 

练习题3:客户端键盘录入,服务器输出文本文件

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.ServerSocket;

import java.net.Socket;

 

public classServerDemo {

   public static void main(String[] args) throws IOException {

      // 创建服务器Socket对象

      ServerSocketss = newServerSocket(23456);

 

      // 监听客户端连接

      Sockets = ss.accept();

 

      // 封装通道内的数据

      BufferedReaderbr = newBufferedReader(newInputStreamReader(

            s.getInputStream()));

      // 封装文本文件

      BufferedWriterbw = newBufferedWriter(newFileWriter("a.txt"));

 

      Stringline = null;

      while ((line = br.readLine())!= null){

         bw.write(line);

         bw.newLine();

         bw.flush();

      }

 

      bw.close();

      // br.close();

      s.close();

      // ss.close();

   }

}

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.Socket;

 

/*

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

 */

public classClientDemo {

   public static void main(String[] args) throws IOException {

      // 创建客户端Socket对象

      Sockets = newSocket("219.216.80.19", 23456);

 

      // 封装键盘录入

      BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));

      // 封装通道内的数据

      BufferedWriterbw = newBufferedWriter(newOutputStreamWriter(

            s.getOutputStream()));

 

      Stringline = null;

      while ((line = br.readLine())!= null){

         if ("over".equals(line)) {

            break;

         }

 

         bw.write(line);

         bw.newLine();

         bw.flush();

      }

 

      // bw.close();

      // br.close();

      s.close();

   }

}

 

练习题3:客户端文本文件,服务器输出到控制台

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.ServerSocket;

import java.net.Socket;

 

public classServerDemo {

   public static void main(String[] args) throws IOException {

      // 创建服务器Socket对象

      ServerSocketss = newServerSocket(34567);

 

      // 监听客户端连接

      Sockets = ss.accept();

 

      // 封装通道内的流

      BufferedReaderbr = newBufferedReader(newInputStreamReader(

            s.getInputStream()));

 

      Stringline = null;

      while ((line = br.readLine())!= null){

         System.out.println(line);

      }

 

     

      s.close();

   }

}

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.net.Socket;

 

/*

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

 */

public classClientDemo {

   public static void main(String[] args) throws IOException {

      // 创建Socket对象

      Sockets = newSocket("192.168.12.92", 34567);

 

      // 封装文本文件

      BufferedReaderbr = newBufferedReader(newFileReader(

            "InetAddressDemo.java"));

      // 封装通道内的流

      BufferedWriterbw = newBufferedWriter(newOutputStreamWriter(

            s.getOutputStream()));

 

      Stringline = null;

      while ((line = br.readLine())!= null){

         bw.write(line);

         bw.newLine();

         bw.flush();

      }

 

      br.close();

      s.close();

   }

}

TCP数据传输(三)

标签:

原文地址:http://blog.csdn.net/wangyanming123/article/details/51347696

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!