客户端:- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class CopyOfTransClient {
- public static void main(String args[]) throws UnknownHostException,
- IOException {
- run();
- }
-
- public static void run() throws UnknownHostException, IOException {
- // 连接服务端、获取客户端输出流。
- Socket socket = new Socket("192.168.0.3", 10009);
- BufferedWriter bwso = new BufferedWriter(new OutputStreamWriter(
- socket.getOutputStream()));
- // 获取键盘录入。socket输出流输出。
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
-
- // socket输入流。
- BufferedReader brso = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- String str = null;
- while ((str = br.readLine()) != null) {
- if (str.equals("over"))
- break;
- bwso.write(str);
- bwso.flush();
- // socket输入流。接受服务端的反馈。
- String strso = brso.readLine();
- System.out.println(strso);
- bwso.close();
- br.close();
- }
- }
- }
复制代码 服务端:- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class CopyOfTransServer {
- public static void main(String args[]) throws IOException {
- run();
- }
-
- public static void run() throws IOException {
- // 获取连接过来的客户端。
- ServerSocket serverSocket = new ServerSocket(10009);
- Socket socket = serverSocket.accept();
-
- //获取ip
- String ip = socket.getInetAddress().getHostAddress();
- System.out.println(ip);
- // 获取客户端流对象。
- BufferedReader br = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
- socket.getOutputStream()));
-
- String str = null;
- while ((str = br.readLine()) != null) {
- bw.write(str.toUpperCase());
- }
- serverSocket.close();
- socket.close();
- }
- }
复制代码 |