JAVA网络编程6

网络编程

网络协议

JAVA网络编程6

IP地址

IP地址:InetAddress

  • 定位位移计算机
  • 127.0.0.1 :本机localhost
  • ip地址分类
    • ipv4/ipv6
    • 公网/私网
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
InetAddress inetAddress2 = InetAddress.getByName("localhost");

端口

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

  • 不同的进程有不同的端口,区分软件
  • 0 ~ 65535
  • 单个协议下端口号不可以冲突
  • 端口分类
    • 公有端口 0~1023
      • HTTP:80
      • HTTPS:443
    • 程序员注册端口 1024~49151
      • Tomcat:8080
      • MySQL:3306
    • 动态,私有端口:49152~65535
InetSocketAddress inetSocketAddress1 = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 8080);

通信协议

TCP/IP协议簇

TCP协议:用户传输协议 (打电话,三次握手、四次挥手、稳定)

UDP协议:用户数据报协议 (发短信,不稳定)

TCP

客户端

  1. 连接服务器 Socket
  2. 发送消息
//客户端
public class TcpClient {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try {
            //1.服务器地址、端口号
            InetAddress sercerIP = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //2.创建socket
            socket = new Socket(sercerIP, port);
            //3.发送消息
            os = socket.getOutputStream();
            os.write("发送一条消息".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

服务端

  1. 建立服务窗口ServerSocket
  2. 等待用户连接accept
  3. 接收消息
//服务端
public class TcpServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1.创建一个地址
            serverSocket = new ServerSocket(9999);
            //2.等待客户端连接
            socket = serverSocket.accept();
            //3.读取客户端消息
            is = socket.getInputStream();
            //管道流
            baos = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len;
            while ((len=is.read(bytes))!=-1){
                baos.write(bytes,0,len);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

TCP文件上传

//服务端
public class TcpFileServer {
    public static void main(String[] args) throws Exception {
        //1.创建服务
        ServerSocket serverSocket = new ServerSocket(9000);
        //2.监听用户连接
        Socket socket = serverSocket.accept();
        //获取输入流
        InputStream is = socket.getInputStream();
        //4.文件输出
        FileOutputStream fos = new FileOutputStream(new File("recerve.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while ((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        //通知客户端我已经接受完毕
        OutputStream os = socket.getOutputStream();
        os.write("我已经接收完成".getBytes());

        //关闭服务
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}
//客户端
public class TcpFileClient {
    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("src/bg.jpg"));
        //4.写出文件
        byte[] buffer = new byte[1024];
        int len;
        while ((len=fis.read(buffer))!=-1){
            os.write(buffer,0,len);
        }
        //通知服务器已经结束
        socket.shutdownOutput();

        //确定服务关闭
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer2 = new byte[1024];
        int len2;
        while ((len2=inputStream.read(buffer2))!=-1){
            baos.write(buffer2,0,len2);
        }
        System.out.println(baos.toString());
        //5.关闭资源
        baos.close();
        inputStream.close();
        fis.close();
        os.close();
        socket.close();
    }
}

UDP

DatagramSocket socket = new DatagramSocket();

socket.send(packet);

socket.receive(packet);

//接收
public class UDPServer  {
    public static void main(String[] args) throws Exception {
        //1.开放端口
        DatagramSocket socket = new DatagramSocket(9090);
        //2.接收数据
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        socket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        //3.关闭流
        socket.close();
    }
}
//发送
public class UDPClient {
    public static void main(String[] args) throws Exception {
        //1.建立连接
        DatagramSocket socket = new DatagramSocket();
        //2.建包
        String msg = "hello";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port = 9090;

        DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);

        //3.发送包
        socket.send(packet);
        //4.关闭流
        socket.close();
    }
}

URL类

new URL();

  • 网络编程基本操作的都是流,熟练掌握I/O流知识
上一篇:Android 使用 InetAddress 判断指定主机是否能够链接


下一篇:网络编程