import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
//服务器
public class Test01 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(666);
        while (true) {
            Socket client = ss.accept();
            new Thread() {
                @Override
                public void run() {
                    //

                    try {
                        InputStream is = client.getInputStream();
                        int ch;
                        byte[] arr = new byte[1024];
                        ch = is.read(arr);
                        System.out.println(new String(arr, 0, ch));

                        //
                        OutputStream os = client.getOutputStream();
                        String word = new Scanner(System.in).nextLine();
                        os.write(word.getBytes());

                        client.close();

                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {

                    }
                }
            }.start();
        }

    }
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
//客户端
public class Test02 {
    public static void main(String[] args) throws IOException {

        while (true) {
            Socket client = new Socket("10.1.10.42"/*客户端IP地址*/, 666);
            //
            OutputStream os = client.getOutputStream();
            String word = new Scanner(System.in).nextLine();
            os.write(word.getBytes());

            if (word.equals("bb")){
                break;
            }

            //
            InputStream is = client.getInputStream();
            int ch;
            byte[] arr = new byte[1024];
            ch = is.read(arr);
            System.out.println(new String(arr,0,ch));

            client.close();
        }
    }
}


//客户端将硬盘数据给服务器
import java.io.*;
import java.net.Socket;

public class Test03 {
    public static void main(String[] args) throws Exception {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("day21\\client\\a.txt"));

        Socket client = new Socket("10.1.10.42", 777);
        OutputStream os = client.getOutputStream();

        int ch;
        byte[] arr = new byte[1024];
        while ((ch = bis.read(arr)) != -1) { /*客户端读到内存*/
            os.write(arr, 0, ch);/*客户端写到管道流*/
        }
        //client.shutdownOutput();

        InputStream is = client.getInputStream();
        byte[] arr2 = new byte[1024];
        int ch2 = is.read(arr2);
        System.out.println(new String(arr2,0,ch2));

        client.close();
        bis.close();
    }
}
//服务器接收来自客户端的数据并存入硬盘
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Test04 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(777);
        while (true) {
            Socket client = ss.accept();
            new Thread(){
                @Override
                public void run() {
                    try {
                        InputStream is = client.getInputStream();
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day21\\server\\"+System.currentTimeMillis()+".txt"));
                        byte[] arr = new byte[1024];
                        int ch;
                        while ((ch = is.read(arr))!= -1) {   /*服务器从管道流中读取到内存*/
                            bos.write(arr, 0, ch); /*服务器写到硬盘*/
                        }

                        OutputStream os = client.getOutputStream();
                        os.write("传输完成".getBytes());

                        bos.close();
                        client.close();
                    } catch(IOException e) {
                        e.printStackTrace();
                    }

                }
            }.start();

        }

    }
}



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

public class Test05 {
    public static void main(String[] args) throws IOException {
        // http://localhost:8888/day21/web/index.html
        // GET  /day21/web/index.html HTTP/1.1
        ServerSocket ss = new ServerSocket(5678);

        while (true) {
            Socket client = ss.accept();
            new Thread() {
                @Override
                public void run() {
                    try {
                        InputStream is = client.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));  //将字节流转换为字符流,只有字符流才可以一次读一行
                        String line = br.readLine();//接收来自浏览器的网址,一行字符串
                        //System.out.println(line);
                        String[] split = line.split(" ");
                        String path = split[1].substring(1);   //   day21/web/index.html  /*无需将'/'改为"\\"*/
                        System.out.println(path);
                        //读取浏览器需要的内容
                        FileInputStream fis = new FileInputStream(path);

                        OutputStream os = client.getOutputStream();
                        //响应给浏览器
                        os.write("HTTP/1.1 200 OK\r\n".getBytes());
                        os.write("Content-Type:text/html\r\n".getBytes());
                        os.write("\r\n".getBytes());

                        int ch;
                        byte[] arr = new byte[1024];
                        while ((ch = fis.read(arr)) != -1) {
                            os.write(arr, 0, ch);
                        }
                        client.close();
                        fis.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
}

 

posted on 2020-07-31 17:22  JustCrazy  阅读(129)  评论(0编辑  收藏  举报