服务端发送给客户端数据,经过客户端修改,再返回给服务端。- package L78Z;
- import java.io.*;
- import java.net.*;
- public class CopyNetTest {
- public static void main(String[] args) {
- new Thread(new MyServer1()).start();
- new Thread(new MyClient1()).start();
- }
- }
- class MyServer1 implements Runnable{
- ServerSocket ss = null;
- Socket s = null;
- OutputStream out1 = null;
- InputStream in1 = null;
- public void run(){
- try {
- ss = new ServerSocket(88);
- s = ss.accept();
- out1 = s.getOutputStream();
- in1 = s.getInputStream();
- String str1 = "河北工程大学";
- System.out.println("server send: "+str1);
- byte[] buf = str1.getBytes();
- out1.write(buf);in1.read(buf);
- String str = new String(buf);
- System.out.println("server receive: "+str);
- ss.close();
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- }
- class MyClient1 implements Runnable{
- Socket s = null;
- OutputStream out1 = null;
- InputStream in1 = null;
- public void run(){
- try {
- s = new Socket(InetAddress.getLocalHost(),88);
- out1 = s.getOutputStream();
- in1 = s.getInputStream();
- byte[] buf = new byte[32];
- in1.read(buf);
- String str = new String(buf);
- System.out.println("client receive: "+str);
- String newStr = str.replaceAll("河北工程大学", "矿院");
- buf = newStr.getBytes();
- out1.write(buf);
- s.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
复制代码
|
|