文章目录

  • 练习1:服务器给出反馈
  • 练习2:客户端数据来自于键盘录入
  • 练习3:服务器数据写入文本文件
  • 练习4:客户端数据来源于文本文件
  • 练习5:上传文件服务器给出反馈
  • 练习6:多线程实现文件上传


练习1:服务器给出反馈

客户端:发送数据,接收服务器反馈
服务器:接收数据,给出反馈

服务器代码:

//创建服务器端的Socket对象(ServerSocket)
ServerSocket ss=new ServerSocket(12345);
//监听客户端连接,返回一个Socket对象
Socket s=ss.accept();
//获取输入流,读数据,并把数据显示在控制台
InputStream is=s.getInputStream();
byte[] bytes=new byte[1024];
int len=is.read(bytes);
String data=new String(bytes,0,len);
System.out.println("服务器:"+data);
//给出反馈
OutputStream os=s.getOutputStream();
os.write("hi!".getBytes());
//释放资源
ss.close();

客户端代码:

//创建客户端的Socket对象(Socket)
Socket s=new Socket("192.168.1.8",12345);
//获取输出流,写数据
OutputStream os=s.getOutputStream();
os.write("hello".getBytes());
//接收服务器反馈
InputStream is=s.getInputStream();
byte[] bytes=new byte[1024];
int len=is.read(bytes);
String data=new String(bytes,0,len);
System.out.println("客户端:"+data);
//释放资源
s.close();

练习2:客户端数据来自于键盘录入

客户端:数据来自于键盘录入,直到录入的数据是886,发送数据结束
服务器:接受到的数据在控制台输出

客户端代码:

//创建客户端Socket对象
Socket socket=new Socket("192.168.1.8",10001);
//数据来自于键盘录入,直到输入的数据是886发送结束
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line=bufferedReader.readLine())!=null){
    if ("886".equals(line)){
        break;
    }
    bufferedWriter.write(line);
    bufferedWriter.newLine();
    bufferedWriter.flush();
}
//释放资源
socket.close();

服务器代码:

//创建服务器Socket对象
ServerSocket serverSocket=new ServerSocket(10001);
//监听客户端连接,返回一个对应的Socket对象
Socket socket=serverSocket.accept();
//获取输入流
InputStream inputStream=socket.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));

String line;
while ((line=bufferedReader.readLine())!=null){
    System.out.println(line);
}
serverSocket.close();

练习3:服务器数据写入文本文件

客户端:数据来自于键盘录入,直到输入的数据是886,发送数据结束
服务器:接受到的数据写入文本文件

客户端代码:

Socket socket=new Socket("192.168.1.8",10003);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
String line;
while((line=bufferedReader.readLine())!=null){
    if ("886".equals(line)){
        break;
    }
    BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    bufferedWriter.write(line);
    bufferedWriter.newLine();
    bufferedWriter.flush();
}
socket.close();

服务器代码:

ServerSocket serverSocket=new ServerSocket(10003);
Socket socket=serverSocket.accept();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//把数据写入文本文件
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("E:\\IOTest\\03.txt"));
String line;
while ((line=bufferedReader.readLine())!=null){
    bufferedWriter.write(line);
    bufferedWriter.newLine();
    bufferedWriter.flush();
}
//释放资源
serverSocket.close();
bufferedWriter.close();

练习4:客户端数据来源于文本文件

客户端:数据来源于文本文件
服务端:收到的数据写入文本文件

服务器代码:

//创建服务器Socket对象
ServerSocket serverSocket=new ServerSocket(10004);
//监听客户端连接,返回一个对应的Socket对象
Socket socket=serverSocket.accept();
//接收数据
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//把数据写入文本文件
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("E:\\IOTest\\05.txt"));
String line;
while ((line=bufferedReader.readLine())!=null){
    bufferedWriter.write(line);
    bufferedWriter.newLine();
    bufferedWriter.flush();
}
//释放资源
bufferedWriter.close();
serverSocket.close();

客户端代码:

Socket socket=new Socket("192.168.1.8",10004);
//封装输出流写数据
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader bufferedReader=new BufferedReader(new FileReader("E:\\IOTest\\01.txt"));
String line;
while ((line=bufferedReader.readLine())!=null){
    bufferedWriter.write(line);
    bufferedWriter.newLine();
    bufferedWriter.flush();
}
//释放资源
bufferedReader.close();
socket.close();

练习5:上传文件服务器给出反馈

客户端:数据来自于文本文件,接收服务器反馈
服务器:接收到的数据写入文本文件,给出反馈

出现问题:程序一直等待
原因:读取数据的方法是阻塞式的
解决办法:自定义一个结束标记,使用shutdownOutput()方法

客户端代码:

Socket socket=new Socket("192.168.1.8",10005);
BufferedReader bufferedReader=new BufferedReader(new FileReader("E:\\IOTest\\02.txt"));
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line=bufferedReader.readLine())!=null){
    bufferedWriter.write(line);
    bufferedWriter.newLine();
    bufferedWriter.flush();
}
socket.shutdownOutput();
//接收反馈
BufferedReader brClient=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String string=brClient.readLine();
System.out.println("服务器:"+string);
//释放资源
bufferedReader.close();
socket.close();
brClient.close();

服务端代码:

ServerSocket serverSocket=new ServerSocket(10005);
Socket socket=serverSocket.accept();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("E:\\IOTest\\06.txt"));
String line;
while ((line=bufferedReader.readLine())!=null){
    bufferedWriter.write(line);
    bufferedWriter.newLine();
    bufferedWriter.flush();
}
//给出反馈
BufferedWriter bwServer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bwServer.write("文件上传成功!");
bwServer.newLine();
bwServer.flush();
//释放资源
bufferedWriter.close();
serverSocket.close();
bwServer.close();

练习6:多线程实现文件上传

客户端:数据来自文本文件,接收服务器反馈
服务器:接收到的数据写入文本文件,给出反馈,代码用线程封装,为每一个客户端开启一个线程

客户端代码:

Socket socket=new Socket("192.168.1.8",10005);
BufferedReader bufferedReader=new BufferedReader(new FileReader("E:\\IOTest\\02.txt"));
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line=bufferedReader.readLine())!=null){
    bufferedWriter.write(line);
    bufferedWriter.newLine();
    bufferedWriter.flush();
}
socket.shutdownOutput();
//接收反馈
BufferedReader brClient=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String string=brClient.readLine();
System.out.println("服务器:"+string);
//释放资源
bufferedReader.close();
socket.close();
brClient.close();

服务器代码:

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket=new ServerSocket(10005);
    while (true){
        Socket socket=serverSocket.accept();
        new Thread(new ServerThread(socket)).start();
    }
}

public class ServerThread implements Runnable {
    private Socket socket;
    public ServerThread(Socket socket) {
        this.socket=socket;
    }
    @Override
    public void run() {
        try {
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            int count=0;
            File file=new File("E:\\IOTest\\Copy"+count+".txt");
             while (file.exists()){
                count++;
                file=new File("E:\\IOTest\\Copy"+count+".txt");
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(file));
            String line;
            while ((line=bufferedReader.readLine())!=null){
                bufferedWriter.write(line);
                bufferedWriter.newLine();
                bufferedWriter.flush();
                //给出反馈
                BufferedWriter bwServer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                bwServer.write("文件上传成功!");
                bwServer.newLine();
                bwServer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}