java网络编程day04

作业总结

homewoek01

使用TCP编程,编写客户端,服务端代码,实现相互文字交流

package homework;

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

class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("9999端口等待链接");
        Socket socket = serverSocket.accept();
        //接收提问
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(br.readLine());

        //回答
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("sdy");
        bw.newLine();
        //使用字符流必须刷新
        bw.flush();
        //接收提问
        System.out.println(br.readLine());

        //回答
        bw.write("java");
        bw.newLine();
        bw.flush();

        //关流
        br.close();
        bw.close();
        serverSocket.close();

    }
}

class Clinet {
    public static void main(String[] args) throws IOException{
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);

        //提问
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("你叫什么名字");
        bw.newLine();
        bw.flush();

        //接收回答
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(br.readLine());

        //提问
        bw.write("你爱好什么");
        bw.newLine();
        bw.flush();

        //接受回答
        System.out.println(br.readLine());

        //关流
        bw.close();
        br.close();
    }
}

homewoek02

编写接受端A和发送端B使用UDP协议完成

接受端在8888端口等待接受数据(receive)

发送端向接收端发送数据“四大名著是什么”

接受端收到并回答,否则回答what

package homework;

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

class ReceiveA {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(9999);
        byte[] buf = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        datagramSocket.receive(packet);
        //输出接受的数据
        System.out.println(new String(packet.getData(), 0, packet.getLength()));

        //回答
        byte[] data = "三国演义,红楼梦,水浒传".getBytes();
        DatagramPacket packet1 = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 9998);
        datagramSocket.send(packet1);

        datagramSocket.close();
    }
}

class SendB {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(9998);

        //提问
        byte[] data = "四大名著是什么".getBytes();
        DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 9999);
        datagramSocket.send(packet);

        //接受回答

        byte[] buf = new byte[1024];
        DatagramPacket packet1 = new DatagramPacket(buf, buf.length);
        datagramSocket.receive(packet1);
        System.out.println(new String(packet1.getData(), 0, packet.getLength()));

        datagramSocket.close();
    }
}

homework03

编写客户端服务端,客户端输入一个文件名,服务端收到后返回这个文件,如果没有就返回默认文件,客户端收到文件后返回到某某路径

package homework;

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

class ServerHomeWork03 {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();

        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String musicName = br.readLine();
        System.out.println("音乐名为:" + musicName);

        File file = new File("//home/sdy/下载" + musicName);
        String name = "";
        if (file.exists()) {
            name = musicName;
        } else {
            name = "1.jpg";
        }
        InputStream is = new FileInputStream("//home/sdy/下载/" + name);

        OutputStream os = socket.getOutputStream();

        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = is.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
        is.close();
        socket.shutdownOutput();

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

class ClinetHomeWork03 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        Scanner s = new Scanner(System.in);
        String name = s.nextLine();
        bw.write(name);
        bw.newLine();
        bw.flush();

        //接收并写入本地
        InputStream is = socket.getInputStream();
        OutputStream os = new FileOutputStream("//home/sdy/下载/2.jpg");
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = is.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
        bw.close();
        is.close();
        os.close();
        socket.close();
    }
}
 
posted @ 2022-03-23 23:43  ふじさんのゆき  阅读(22)  评论(0编辑  收藏  举报