首页 > 编程语言 >Java网络编程

Java网络编程

时间:2023-07-11 13:34:39浏览次数:65  
标签:Java socket 编程 bytes 网络 System close println new

1. ip和端口

ip地址 InetAddress

//因为没有构造方法,所以不能通过new来生成对象,但是可以通过类名来调用类的静态方法
InetAddress inetAddress1 = InetAddress.getByName("localhost");
            System.out.println(inetAddress1);
            InetAddress inetAddress2 = InetAddress.getLocalHost();
            System.out.println(inetAddress2);

IP+端口的套接字 InetSocketAddress

 //有构造方法,所以可以创造对象
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
        System.out.println(inetSocketAddress);//输出ip加端口:/127.0.0.1:8080

        System.out.println(inetSocketAddress.getAddress());//输出:/127.0.0.1
        System.out.println(inetSocketAddress.getHostName());//输出:127.0.0.1,获取地址
        System.out.println(inetSocketAddress.getPort());//输出:8080,获取端口

2. TCP发消息

客户端

public class TcpClienttest {
    public static void main(String[] args) throws Exception {
        //1. 要知道服务器的ip 端口
        InetAddress serverIp = InetAddress.getByName("127.0.0.1");
        int port=9999;
        //2. 创建socket连接
        Socket socket = new Socket(serverIp, port);
        //3. 发送io流
        OutputStream os = socket.getOutputStream();  //客户端输出流
        os.write("4522".getBytes());
        //4.关闭连接
        os.close();
        socket.close();

    }

服务器

// 1.建立服务端套接字
        ServerSocket serverSocket = new ServerSocket(9999);
        // 2. 等待连接
        Socket accept = serverSocket.accept();
        // 3.读取客户端消息
        InputStream is = accept.getInputStream();
        // 使用管道流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len;
//        while((len=is.read(bytes))!=-1){
//            baos.write(bytes,0,len);
//        }
        while((len=is.read(bytes))!=-1){
            String str = new String(bytes,0,len);
            System.out.println(str);
        }
        //4.打印结果
//        System.out.println(baos.toString());

        //5.关闭资源
        baos.close();
        is.close();
        accept.close();
        serverSocket.close();
    }

3. TCP文件上传

socket获取输入输出流,再通过文件输出输入流写到本地。以服务器为例子,socket得到输入流,写入文件输出流。

服务端 ServerSocket

  1. 建立套接字,监听
  2. 获取套接字输入流,读到内存bytes
  3. 获取文件输出流,在读的while中写入。
  4. 反向通知客户端收到了,需要获取套接字的输出流。
public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket accept = serverSocket.accept();
        InputStream is = accept.getInputStream();

        FileOutputStream fileOutputStream = new FileOutputStream("src/main/b.jpg");
        int len=0;
        byte[] bytes = new byte[1024];
        while((len=is.read(bytes))!=-1) {
            fileOutputStream.write(bytes,0,len);
        }

        //反向通知
        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("我已经收到啦".getBytes());
        System.out.println("服务端结束!");
        is.close();
        accept.close();
        serverSocket.close();

    }

客户端

  1. 与服务端建立连接
  2. 获取文件的输入流,将文件读到内存
  3. 获取套接字的输出流,在读的同时写入。
  4. 输出流结束,由于服务器还要发送信息,需要手动关闭输出流。否则服务端会认为客户端输入尚未结束,阻塞等待。
  5. 获取套接字的输入流,读到内存(服务端发来的收到消息)
public static void main(String[] args) throws Exception {
        InetAddress byName = InetAddress.getByName("127.0.0.1");
        int port=9999;
        Socket socket = new Socket(byName, port);
        OutputStream outputStream = socket.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream("a.jpg");
        byte[] bytes = new byte[1024];
        int len =0;
        while((len=fileInputStream.read(bytes))!=-1) {
            outputStream.write(bytes,0,len);
        }
        //通知服务端,客户端这里的output结束。
        socket.shutdownOutput();
        System.out.println("已发出1");
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int len2=0;
        byte[] bytes1 = new byte[1024];
        while((len2=inputStream.read(bytes1))!=-1) {
            byteArrayOutputStream.write(bytes1,0,len2);
        }
        System.out.println(byteArrayOutputStream.toString());
        byteArrayOutputStream.close();
        fileInputStream.close();
        outputStream.close();

        socket.close();
    }

4.UDP 数据传输 DatagramSocket

udp中不是c/s概念,没有服务端客户端。只有接收方和发送方

接收方

  1. 建立自己的套接字,开放自己的端口
  2. 构建接收数据包, byte[],offset,length
  3. 调用接收方法,通过数据包的方法获取
public static void main(String[] args) throws Exception {
        //1. 开放端口
        DatagramSocket datagramSocket = new DatagramSocket(9999);
        //2. 构造接受数据包
        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
        //3. 阻塞接收
        datagramSocket.receive(datagramPacket);
        System.out.println(datagramPacket.getAddress());
        System.out.println(datagramPacket.getAddress().getHostAddress());
        System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
        datagramSocket.close();
    }

发送方

  1. 建立自己的socket,用不着可以不传入自己的端口号
  2. 构建发送数据包,byte[],offset,len,InetAddress,port
  3. 发送方法
public static void main(String[] args) throws Exception {
        //1. 创建自己的socket
        DatagramSocket socket = new DatagramSocket();
        //2. 构建数据包
        //数据
        String str= "我他么来了";
        //接收方
        InetAddress toIp = InetAddress.getByName("127.0.0.1");
        int toPort=9999;
        //构造
        DatagramPacket packet = new DatagramPacket(str.getBytes(), 0, str.getBytes().length, toIp, toPort);
        //3. 发送
        socket.send(packet);
        //4. 关闭
        socket.close();

    }

标签:Java,socket,编程,bytes,网络,System,close,println,new
From: https://www.cnblogs.com/its1440/p/17535291.html

相关文章