IP、端口、TCP、UDP、文件上传

时间:2021-06-02 15:28:12   收藏:0   阅读:0

1.ip

public class TestInetAddress {
    public static void main(String[] args) {
        try {
            //查询本机IP,可以new
            InetAddress byName = InetAddress.getByName("localhost");
            System.out.println(byName);
            InetAddress byName1 = InetAddress.getByName("127.0.0.1");
            System.out.println(byName1);
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println(localHost);
            System.out.println("=============================================");
            //查询网站ip地址
            InetAddress byName2 = InetAddress.getByName("www.baidu.com");
            System.out.println(byName2);
            System.out.println(byName2.getHostAddress());//ip
            System.out.println(byName2.getHostName());//域名或者自己电脑的名字

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

2.端口

端口表示计算机上的一个程序的进程

public class TestInetSocketAddress {
    public static void main(String[] args) {
        //可以new
        InetSocketAddress socketAddress = new InetSocketAddress("localhost", 8080);
        System.out.println(socketAddress);
        System.out.println("================================");
        System.out.println(socketAddress.getAddress().getHostAddress());
        System.out.println(socketAddress.getHostName());//地址,域名
        System.out.println(socketAddress.getPort());//端口
    }
}

3.通信协议

TCP和UDP对比:

4.TCP

客户端

服务器

//客户端
public class TcpClientDemo01 {
    public static void main(String[] args) {

        try {
            //1.要知道服务器地址
            InetAddress serverIp = InetAddress.getByName("127.0.0.1");
            //2.端口号
            int port = 9999;
            //3.创建一个socket连接
            Socket socket = new Socket(serverIp,port);
            //4.发送消息 IO流
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("你好,欢迎学习".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

//服务端
public class TcpServerDemo01 {
    public static void main(String[] args) {
        ServerSocket serverSocket= null;
        Socket accept = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;

        try {
            //1.首先要有一个地址
            serverSocket = new ServerSocket(9999);
            while(true) {
                //2.等待客户端连接
                accept = serverSocket.accept();
                //3.读取客户端消息
                inputStream = accept.getInputStream();
                //管道流
                byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] bytes = new byte[1024];
                int len;
                if ((len = inputStream.read(bytes)) != -1) {
                    byteArrayOutputStream.write(bytes, 0, len);
                }
                System.out.println(byteArrayOutputStream.toString());
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            if (byteArrayOutputStream!=null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream!=null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept!=null) {
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket!=null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

5.文件上传

//客户端
public class TcpClientDemo02 {
    public static void main(String[] args) throws Exception {
        //1.创建一个Socket连接
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
        //2.创建一个输出流
        OutputStream os = socket.getOutputStream();
        //3.读取文件
        FileInputStream fis = new FileInputStream(new File("1.png"));
        //4.写出文件
        byte[] buffer = new byte[1024];
        int len;
        if ((len = fis.read(buffer))!=-1){
            os.write(buffer,0,len);
        }

        //通知服务器,已经结束
        socket.shutdownOutput();//我已经传输完了!
        //确认服务器接收完毕才能断开连接
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer1 = new byte[2048];
        int len2;
        if ((len2 = is.read(buffer1))!=-1){
            baos.write(buffer1,0,len2);
        }
        System.out.println(baos.toString());
        //5.关闭资源
        baos.close();
        is.close();
        fis.close();
        os.close();
        socket.close();
    }
}


//服务端
public class TcpServerDemo02 {
    public static void main(String[] args) throws Exception {
        //1.创建服务
        ServerSocket serverSocket = new ServerSocket(9000);
        //2.监听客户端的连接
        Socket socket = serverSocket.accept();
        //3.获取输入流
        InputStream is = socket.getInputStream();
        //4.文件输出
        FileOutputStream fos = new FileOutputStream(new File("2.png"));
        byte[] buffer = new byte[1024];
        int len;
        if ((len = is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        //通知客户端已接收完毕
        OutputStream os = socket.getOutputStream();
        os.write("我接收完毕了,你可以断开了".getBytes());
        //5.关闭资源
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }

}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!