浏览 10297 次
锁定老帖子 主题:java Socket通信的学习笔记三
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|||||||||
---|---|---|---|---|---|---|---|---|---|
作者 | 正文 | ||||||||
发表时间:2009-06-18
最后修改:2009-06-18
先看一段服务器端的代码: package com.socket.test; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { System.out.println("开始侦听请求。。。。"); ServerSocket serverSocket=new ServerSocket(3333);//服务器套接字 Socket clientSocket=null; while(true){ clientSocket=serverSocket.accept();//获得客户端的请求的Socket System.out.println("已侦听到了客户端的请求。。。。。"); new ServerThread(clientSocket); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 下面是ServerThread的代码: package com.socket.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; public class ServerThread extends Thread { private Socket socket; private InputStream clientInput; private OutputStream clientOutput; public ServerThread(Socket socket){ this.socket=socket; try { this.clientInput=socket.getInputStream(); this.clientOutput=socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } this.start(); } @Override public void run() { System.out.println("this is child class's run method....."); BufferedReader breader=new BufferedReader(new InputStreamReader(clientInput)); try { String strLine=breader.readLine(); if("over".equalsIgnoreCase(strLine)){ PrintWriter printWriter=new PrintWriter(clientOutput,true); printWriter.println("over"); }else{ double side=Double.parseDouble(strLine); side=Double.parseDouble(strLine); System.out.println("Side of Square Received:"+side); double squareArea=side*side; String str="Side:"+side+"Square:"+squareArea; PrintWriter printWriter=new PrintWriter(clientOutput,true); printWriter.println(str); System.out.println("Finish sending to client..."); } clientInput.close(); clientOutput.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 这段代码主要作用: 1从构造函数中获得客户端的套接字请求,创建向客户端输入和输出的流。调用线程执行接下来的请求(this.start()); public ServerThread(Socket socket){ this.socket=socket; try { this.clientInput=socket.getInputStream(); this.clientOutput=socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } this.start(); } 2通过bufferedReader获取客户端的输入并进行分析,假如是over,则向客户端输出over,并关闭,反之。则计算从客户端传来的数值,对应正方形的面积,并蒋结果返回到客户端。之后关闭。 if("over".equalsIgnoreCase(strLine)){ PrintWriter printWriter=new PrintWriter(clientOutput,true); printWriter.println("over"); }else{ double side=Double.parseDouble(strLine); side=Double.parseDouble(strLine); System.out.println("Side of Square Received:"+side); double squareArea=side*side; String str="Side:"+side+"Square:"+squareArea; PrintWriter printWriter=new PrintWriter(clientOutput,true); printWriter.println(str); System.out.println("Finish sending to client..."); } 注意PrintWriter的使用。。。。。。。。。 下面是客户端调用的代码: package com.socket.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { try { System.out.println("开始发送请求。。。。"); Socket socket=new Socket("127.0.0.1",3333); BufferedReader bfReader=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please input the line....."); String strLine=bfReader.readLine(); OutputStream outputStream=socket.getOutputStream(); InputStream inputStream=socket.getInputStream(); PrintWriter pw=new PrintWriter(outputStream,true); pw.println(strLine); BufferedReader br=new BufferedReader(new InputStreamReader(inputStream)); System.out.println(br.readLine()); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 代码分析: Socket socket=new Socket("127.0.0.1",3333); BufferedReader bfReader=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please input the line....."); String strLine=bfReader.readLine(); 建立与服务器的连接: Socket socket=new Socket("127.0.0.1",3333); 构建缓从输入流: BufferedReader bfReader=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please input the line....."); String strLine=bfReader.readLine(); 获得服务器的输出和输入流: OutputStream outputStream=socket.getOutputStream(); InputStream inputStream=socket.getInputStream(); 把输入发送到服务器: PrintWriter pw=new PrintWriter(outputStream,true); pw.println(strLine); 获得服务器的输出:
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream)); System.out.println(br.readLine()); 这里的 PrintWriter pw=new PrintWriter(outputStream,true); pw.println(strLine); 代码用outputStream我理解的是服务器的输出流对应的就是客户端的输入流,所以客户端发送到服务器端的数据就用outputStream,而获取服务器的输出则用inputStream,感觉这有点怪。不敢肯定是否对? 参考java API: public class PrintWriter
向文本输出流打印对象的格式化表示形式。此类实现在
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|||||||||
返回顶楼 | |||||||||