- package classmate;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.*;
- /*
- 要求:
- 客户端通过键盘输入向服务端发送一条数据(字符串),
- 服务端接收数据,并将字符串转化成大写字母返回给客户端,
- 客户端打印转化后的字符串
- */
- public class Client {
- public static void main(String[] args) throws IOException {
-
- // 建立客户端Socket服务
- Socket s = new Socket("192.168.1.117",10023);
- // 读取键盘数据
- BufferedReader bufr =
- new BufferedReader(new InputStreamReader(System.in));
-
- try {
- //定义目的,将数据写入到Socket服务中,发给服务端
- BufferedWriter bufw=
- new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- //获取从服务端读来的数据流
- BufferedReader buIn=
- new BufferedReader(new InputStreamReader(s.getInputStream()));
- String line;
- while((line=bufr.readLine())!=null){
- //将数据写入服务端
- bufw.write(line);
- //readLine()读取的一行不包括换行符,所以要写入一个换行符
- bufw.newLine();
- bufw.flush();
- if("over".equals(line))
- break;
- //读取服务端的数据
- String len = buIn.readLine();
- System.out.println(len);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- bufr.close();
- s.close();
-
- }
-
- }
- class Servler{
- public static void main(String[] args) throws IOException {
-
- ServerSocket ss=new ServerSocket(10023);
- Socket a = ss.accept();
- //读取客户端的数据
- BufferedReader bufr=
- new BufferedReader(new InputStreamReader(a.getInputStream()));
- //将数据写入到Socket流中,并发给客户端
- BufferedWriter bufw=
- new BufferedWriter(new OutputStreamWriter(a.getOutputStream()));
- String line=null;
- while((line=bufr.readLine())!=null){
- System.out.println("服务端:"+line);
- bufw.write(line.toUpperCase());
- bufw.newLine();
- bufw.flush();
- }
- a.close();
- ss.close();
-
-
- }
- }
复制代码
|
|