A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© xiaoxinxin003 中级黑马   /  2015-7-13 15:21  /  237 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

服务端发送给客户端数据,经过客户端修改,再返回给服务端。
  1. package L78Z;
  2. import java.io.*;
  3. import java.net.*;
  4. public class CopyNetTest {

  5.         public static void main(String[] args) {
  6.                 new Thread(new MyServer1()).start();
  7.                 new Thread(new MyClient1()).start();
  8.         }

  9. }
  10. class MyServer1 implements Runnable{
  11.         ServerSocket ss = null;
  12.         Socket s = null;
  13.         OutputStream out1 = null;
  14.         InputStream in1 = null;
  15.         public void run(){
  16.                 try {
  17.                         ss = new ServerSocket(88);
  18.                         s = ss.accept();
  19.                         out1 = s.getOutputStream();
  20.                         in1 = s.getInputStream();
  21.                         String str1 = "河北工程大学";
  22.                         System.out.println("server send: "+str1);
  23.                         byte[] buf = str1.getBytes();
  24.                         out1.write(buf);in1.read(buf);
  25.                         String str = new String(buf);
  26.                         System.out.println("server receive: "+str);
  27.                         ss.close();
  28.                 } catch (Exception e) {
  29.                         // TODO: handle exception
  30.                 }
  31.         }
  32. }
  33. class MyClient1 implements Runnable{
  34.         Socket s = null;
  35.         OutputStream out1 = null;
  36.         InputStream in1 = null;
  37.         public void run(){
  38.                 try {
  39.                         s = new Socket(InetAddress.getLocalHost(),88);
  40.                         out1 = s.getOutputStream();
  41.                         in1 = s.getInputStream();
  42.                         byte[] buf = new byte[32];
  43.                         in1.read(buf);
  44.                         String str = new String(buf);
  45.                         System.out.println("client receive: "+str);
  46.                         String newStr = str.replaceAll("河北工程大学", "矿院");
  47.                         buf = newStr.getBytes();
  48.                         out1.write(buf);
  49.                         s.close();
  50.                 } catch (IOException e) {
  51.                        
  52.                         e.printStackTrace();
  53.                 }
  54.         }
  55. }
复制代码



2 个回复

倒序浏览
各位大哥咱们一块研究下
回复 使用道具 举报
我就想问,你的技术分怎么得到得而?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马