您的位置:首页 > 职场人生

黑马程序员 Java学习笔记 (day11)

2014-09-03 22:02 302 查看
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

 

 

    好久没有写博客了,分享一下今天做的一道基础测试题:

/*

 10、使用TCP协议完成一个客户端一个服务器。客户端从键盘输入读取一个字符串,发送到

 服务器。服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印。

 */

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.UnknownHostException;

public class Test10{//客户端

 public static void main(String[] args)  {

       Socket s = null;

 try {

  s = new Socket("192.168.1.2",10004);

 } catch (UnknownHostException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

 } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

 }

       //定义读取键盘录入的流对象

       BufferedReader bufr = new BufferedReader

         (new InputStreamReader(System.in));

       //将数据写到socket输出流,发给服务端

       BufferedWriter bufOut = null;

 try {

  bufOut = new BufferedWriter

       (new OutputStreamWriter(s.getOutputStream()));

 } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

 }

      //定义一个socket读取流,读取服务端返回的信息

       BufferedReader bufIn = null;

 try {

  bufIn = new BufferedReader

       (new InputStreamReader(s.getInputStream()));

 } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

 }

       String line =null;

       try {

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

    // 输入over,程序停止

      if("over".equals(line))

      break;

      bufOut.write(line);

      bufOut.newLine();

      bufOut.flush();

      //接受服务器的数据

      String str = bufIn.readLine();

      //打印服务器返回的数据

      System.out.println(str);

     }

 } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

 }

       //关闭资源

       try {

  bufr.close();

 } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

 }

       try {

  s.close();

 } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

 }

 

 }

}

class Server{

 public static void main(String[] args){

  ServerSocket ss = null;

  try {

   ss = new ServerSocket(10004);

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  Socket s = null;

  try {

   s = ss.accept();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  String ip = s.getInetAddress().getHostAddress();

  System.out.println(ip+":connected");

  //读取socket读取流中的数据

  BufferedReader bufIn = null;

  try {

   bufIn = new BufferedReader

     (new InputStreamReader(s.getInputStream()));

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  //socket输出流

  BufferedWriter bufOut = null;

  try {

   bufOut = new BufferedWriter

     (new OutputStreamWriter(s.getOutputStream()));

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  //定义StringBuilder容器用于反转

  StringBuilder sb = new StringBuilder();

  String line = null;

  try {

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

    sb.append(line);

    //将数据反转之后输出

    bufOut.write(sb.reverse().toString());

    bufOut.newLine();

    bufOut.flush();    

    sb.delete(0, sb.length());

   }

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  //关闭资源

  try {

   s.close();

  } catch (IOException e1) {

   // TODO Auto-generated catch block

   e1.printStackTrace();

  }

  try {

   ss.close();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

}

    这道题我做了很长时间,做的时候思路也有些乱,不停的用dos命令行验证,还好最后做出来了,主要分享以下部分:

  StringBuilder sb = new StringBuilder();

  String line = null;

  try {

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

    sb.append(line);

    //将数据反转之后输出

    bufOut.write(sb.reverse().toString());

    bufOut.newLine();

    bufOut.flush();    

    sb.delete(0, sb.length());

   }

    因为要反转字符串,所有我用到了StringBuilder容器的reverse()功能,在bufOut.write(sb.reverse().toString())这一块因为忽略了toString()导致编译无法通过,就这么简单的地方,我花了半个小时才弄出来,看来自己的很多基础部分的理解还不够,下面的学习得加紧了。看着一期期的错过,心里真是有些着急。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: