- import java.io.*;
- import java.net.*;
- public class Test {
- public static void main(String[] args) {
-
- new Thread(new Server1()).start();
-
- new Thread(new Client1()).start();
- }
- }
- class Client1 implements Runnable {
- public void run() {
- try {
- Socket s = new Socket("127.0.0.1", 10000);
- BufferedReader bufr = new BufferedReader(new InputStreamReader(
- System.in));
- PrintWriter out = new PrintWriter(s.getOutputStream(), true);
- BufferedReader buf = new BufferedReader(new InputStreamReader(
- s.getInputStream()));
- String line = null;
- while ((line = bufr.readLine()) != null) {
- if ("over".equals(line))
- break;
- out.println(line);
- System.out.println(new String(buf.readLine()));
- }
- // String str = buf.readLine();System.out.println(str);
- s.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- class Server1 implements Runnable {
- public void run() {
- try {
- ServerSocket ss = new ServerSocket(10000);
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip + "...connected");
- BufferedReader bufr = new BufferedReader(new InputStreamReader(
- s.getInputStream()));
- PrintWriter out = new PrintWriter(s.getOutputStream(), true);
- String line = null;
- while ((line = bufr.readLine()) != null) {
- System.out.println(line);
- out.println(new StringBuffer(line).reverse());
- }
- s.close();
- ss.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
复制代码
试试这个 |