Java基础 网络编程练习

1、编程题 Homework01.java

Homework01Server.java Homework01Client.java

1、使用字符流的方式,编写一个客户端程序和服务器端程序

2、客户端发送"name",服务端接收到后,返回"我是nova",nova是你自己的名字

3、客户端发送"hobby",服务端接收到后,返回"编写java程序"

4、不是这两个问题,回复"你说啥呢"

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @ClassName Homework01Client
 * @Description
 * @Author 小黄debug
 * @Date 2022/3/27 14:24
 * @Version 1.0
 **/
public class Homework01Client {
    public static void main(String[] args) throws IOException {
        //1、连接服务器(ip,端口)
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        //2、连接上以后,生成socket
        //通过socket.getOutputStream()得到socket对象关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();
        //3、通过输出流,写入数据到数据通道,使用字符流
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        //bufferedWriter.write("hello server 字符流");

        //从键盘读取用户的问题
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的问题");
        String question = scanner.next();

        bufferedWriter.write(question);
        bufferedWriter.newLine();//插入一个换行符,表示写入的内容结束
        bufferedWriter.flush();//将数据推送

        InputStream inputStream = socket.getInputStream();
        //将inputStream转成字符流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s= bufferedReader.readLine();
        System.out.println(s);

        //关闭
        bufferedReader.close();
        bufferedWriter.close();
        outputStream.close();
        socket.close();
    }
}
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @ClassName Homework01Server
 * @Description
 * @Author 小黄debug
 * @Date 2022/3/27 14:14
 * @Version 1.0
 **/
public class Homework01Server {
    public static void main(String[] args) throws IOException {
        //监听本机的9999端口
        ServerSocket serverSocket = new ServerSocket(9999);
        //如果没有客户端连接,程序会阻塞,等待连接
        Socket socket = serverSocket.accept();
        //连接成功后socket有值了,拿到从客户端写入到数据通道的数据
        InputStream inputStream = socket.getInputStream();
        //将inputStream转成字符流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s= bufferedReader.readLine();
        String answer = "";
        System.out.println(answer);
        if("name".equals(s)){
            answer = "我是小黄debug";
        }else if("hobby".equals(s)){
            answer = "编写java程序 ";
        }else{
            answer = "你说什么?";
        }

        //通过socket.getOutputStream()得到socket对象关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();
        //3、通过输出流,写入数据到数据通道,使用字符流
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write(answer);

        bufferedWriter.newLine();//插入一个换行符,表示写入的内容结束
        bufferedWriter.flush();//将数据推送
        //关闭
        bufferedWriter.close();
        bufferedReader.close();
        outputStream.close();
        socket.close();

    }
}

2、编程题Homework02.java

Homework02ReceiverA.java        Homework02SenderB.java

1、编写一个接收端A,和一个发送端B,使用UDP协议完成

2、接收端在8888端口等待接收数据(receive)

3、发送端向接收端 发送数据"四大名著是哪些"

4、接收端收到发慈禧太后端发送的问题后,返回四大名著是<<红楼梦>>..,否则返回what?

5、接收端和发送端程序退出

import java.io.IOException;
import java.net.*;

/**
 * @ClassName Homework02ReceiverA
 * @Description
 * @Author 小黄debug
 * @Date 2022/3/27 15:15
 * @Version 1.0
 **/
public class Homework02ReceiverA {
    public static void main(String[] args) throws IOException {
        //创建连接
        DatagramSocket socket = new DatagramSocket(9999);

        //
        byte[] buf = new byte[64*1024];
        DatagramPacket packet = new DatagramPacket(buf,buf.length);
        //接收数据
        socket.receive(packet);
        //拆包
        byte[] data = packet.getData();
        int length = packet.getLength();
        String s = new String(data,0,length);//将数据转成String;
        System.out.println(s);

        if("四大名著".equals(s)){
            s = "《红楼梦》。。";
        }else{
            s = "what ? ";
        }

        data = s.getBytes();
        //封装
        packet = new DatagramPacket(data,data.length, InetAddress.getByName("192.168.1.39"),9998);
        //发送
        socket.send(packet);

        socket.close();
    }
}
import java.io.IOException;
import java.net.*;

/**
 * @ClassName Homework02ReceiverA
 * @Description
 * @Author 小黄debug
 * @Date 2022/3/27 15:15
 * @Version 1.0
 **/
public class Homework02ReceiverA {
    public static void main(String[] args) throws IOException {
        //创建连接
        DatagramSocket socket = new DatagramSocket(9999);

        //
        byte[] buf = new byte[64*1024];
        DatagramPacket packet = new DatagramPacket(buf,buf.length);
        //接收数据
        socket.receive(packet);
        //拆包
        byte[] data = packet.getData();
        int length = packet.getLength();
        String s = new String(data,0,length);//将数据转成String;
        System.out.println(s);

        if("四大名著".equals(s)){
            s = "《红楼梦》。。";
        }else{
            s = "what ? ";
        }

        data = s.getBytes();
        //封装
        packet = new DatagramPacket(data,data.length, InetAddress.getByName("192.168.1.39"),9998);
        //发送
        socket.send(packet);

        socket.close();
    }
}

3、Homework03.java

1、编写客户端程序 和服务器端程序

2、客户端可以输入一个音乐文件名,比如高山流水,服务端收到音乐名后,可以给客户端返回这个音乐文件,如果服务器没有这个文件,返回一个默认的音乐即可

3、客户端收到文件后,保存到本地 e:\\

4、提示:该程序 可以使用StreamUtils.java

import com.hsydebug.upload.StreamUtils;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * @ClassName Homework04Client
 * @Description
 * @Author 小黄debug
 * @Date 2022/3/27 22:50
 * @Version 1.0
 **/
public class Homework03Client {
    public static void main(String[] args) throws IOException {
        //1、接收用户,指定下载文件名
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入下载文件名");
        String downLoadFileName = scanner.next();
        //2.客户端连接服务端,准备发送
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        //3.获取和Socket关联的输出流
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(downLoadFileName.getBytes());
        //设置定稿结束标记
        socket.shutdownOutput();
        //4、读取服务端返回的文件(字节数据)
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        byte[] bytes = StreamUtils.streamToByteArray(bis);
        //5、得到一个输出流,准备将bytes写入磁盘文件
        String filePath = "e:\\"+downLoadFileName+".mp3";
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
        bufferedOutputStream.write(bytes);

        bis.close();
        bufferedOutputStream.close();
        outputStream.close();
        socket.close();
    }
}
import com.hsydebug.upload.StreamUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName Homework03Server
 * @Description
 * @Author 小黄debug
 * @Date 2022/3/27 19:56
 * @Version 1.0
 **/
public class Homework03Server {
    public static void main(String[] args) throws IOException {
        //1.监听9999端口
        ServerSocket serverSocket = new ServerSocket(9999);
        //2.等待客户端连接
        Socket socket = serverSocket.accept();
        //3.读取
        InputStream inputStream = socket.getInputStream();
        byte[] b = new byte[1024];
        int len = 0;
        String downLoadFileName = "";
        while((len = inputStream.read(b)) != -1){
            downLoadFileName += new String(b,0,len);
        }
        System.out.println("客户端希望下载的文件:"+downLoadFileName);

        //
        String resFileName = "";
        if("高山流水".equals(downLoadFileName)){
            resFileName = "src\\高山流水.mp3";
        }else{
            resFileName = "src\\无名.mp3";
        }

        //创建一个输入流,读取文件
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(resFileName));
        //5、使用工具类StreamUtils,读取一个字节数组
        byte[] bytes = StreamUtils.streamToByteArray(bufferedInputStream);
        //6、得到socket关联的输出流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
        //7、写入到数据通道,返回客户端
        bufferedOutputStream.write(bytes);
        socket.shutdownOutput();

        bufferedInputStream.close();
        bufferedOutputStream.close();
        socket.close();
        serverSocket.close();
    }
}

你可能感兴趣的:(Java基础,java,网络协议)