Home > Java > javaTutorial > body text

Detailed explanation of the specific steps of network programming

零下一度
Release: 2017-07-18 18:00:17
Original
1386 people have browsed it

Three elements of network programming

    • ##What is an IP address
    • InetAddress class_This class represents the Internet Protocol (IP) address
    • Yes The unique identifier of a computer on the Internet.

    • ##The InetAddress class has no constructor, use the following method to obtain the InetAddress object

    • IP address
      ##static InetAddress(String host)

      #static InetAddress[]

      getAllByName(String host)

      Given a hostname, returns an array of its IP address based on the name service configured on the system.

      ##static InetAddress

      ##getByAddress

      (byte[] addr) # In the case of a given original IP address, return the INETADDRESS object.

      ##getByAddress

      (String host, byte[] addr) Create an InetAddress based on the provided hostname and IP address.

      ##static InetAddress

      ##getByName

      Determines the IP address of a host given a hostname.

      getByName("Host name (repeatable)/

        character string ip address
      • "): Use the ip address string of other machines to generate an InetAddress object

      • static InetAddress

      ##getLocalHost

      ()

      Return to local host.

      • Other methods of InetAddress

      ## String##getHostName

      ##String

      ##getHostAddress()

      Returns the IP address string (in text representation).

      () Get the hostname of this IP address.

        The port number
      • Port number range: 0~65535
        • 0~1023 The port is already in use by the system
        Protocol
      • Characteristics of UDP protocol
        • Classes involved in UDP protocol communication
        • The data to be sent must be encapsulated into data packets before being sent.
        • The size of each data packet is limited to 64k.
        • Because it is connectionless, it is fast.
        • The UDP protocol does not distinguish between clients and The server only has a sender and a receiver.
        • Construction method
        • Class DatagramSocket
        • This class represents the socket used to send and receive datagram packets

          ##UDP protocol

      ##DatagramSocket

      ()

      Construct a datagram socket and bind it to any available port on localhost.

      • Member methods

      ## void

      close()

      Close this datagram socket.

      ##void

      ##send

      (DatagramPacket p) Send datagram packets from this socket.

      Parameters:

      p - DatagramPacket to be sent.

        Class DatagramPacket
      • This class represents a datagram packet

        • Construction method
      • UDP steps_Start the receiving end first, then start the sending end (data loss will occur if the opposite occurs)
        • To establish a UDP service, you need to listen to a port number

          DatagramSocket socket = new DatagramSocket(9090);

        • Create an empty container for receiving data

          byte[] buf = new byte[1024];

          DatagramPacket packet = new DatagramPacket(buf, buf.length);

        • Use an empty data packet to receive data

          socket.receive(packet); //This method is a blocking method. Receive data transmitted from the sending end. If no one sends a message to it, it will wait forever.

          System.out.println("The received data is: "+new String(buf) );

        • Close the resource

          socket.close();

        • Establish the UDP protocol service first

          DatagramSocket socket = new DatagramSocket();

        • Prepare the data, and then encapsulate the data into the container

          String str = "This is my first udp example";

          byte[] buf = str.getBytes();

          DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLocalHost(),9090);

          //Parameter one: the content of the data, parameter two: the length of the data (bytes), parameter three: ip address, parameter four: port

        • Use UDP service class to send

          socket.send(packet);

        • Close the resource (the function of closing the resource: release the port number)

          ##socket.close();

        • Sender_Steps:
        • Receiver_Steps :
      • #TCP protocol
        • Client_Class Socket
          This class implements client sockets (it can also be called "sockets"). A socket is the endpoint for communication between two machines.

        • Construction method

        • The data sent is sent based on the IO stream

        • The data sent has no size limit

        • The three-way handshake mechanism is used to ensure Data integrity

        • Because of the surface connection, the speed is slow

        • TCP protocol is Characteristics of the TCP protocol

        • Characteristics of the TCP protocol
          # #####

      DatagramPacket

      (byte[] buf, int length, InetAddress address, int port) Construct a datagram packet to send a packet of length length to the specified port number on the specified host.

      Parameters:

      buf - package data.

      length - Packet length.

      address - Destination address.

      port - Destination port number.

      #Socket(InetAddress address, int port)

      Creates a stream socket and connects it to the specified port number of the specified IP address.

      Parameters:

      address - IP address.

      port - Port number.

      • Member methods

      ##OutputStream
      ## InputStream

      ##getInputStream

      () Returns the input stream for this socket.

      ##getOutputStream

      () Returns the output stream of this socket.

      • Server_Class ServerSocket

        This class implements the server socket. The server socket waits for requests to come in over the network. It performs some action based on the request and may then return the results to the requester.

        • Construction method

      #ServerSocket(int port)

      Create a server socket bound to a specific port.

      Parameters:

      port - port number; or 0, which means use any free port.

      • Member methods

      • TCP steps_First start the server, then start the client (data loss will occur if the opposite occurs)
        • Establish a TCP protocol server and need to listen to a port

          ServerSocket serverSocket = new ServerSocket(9090);

        • Accept client request connection

          Socket socket = serverSocket.accept(); //This method is a blocking method. If If no client connects to it, it will wait forever.

        • Get the input stream and read the data transmitted by the client

          InputStream inputStream = socket.getInputStream();

          byte[] buf = new byte[1024];

          int length = 0;

          length = inputStream.read(buf);

          System.out.println("The server received data: "+ new String (buf,0,length));

        • Send a message to the client_The server writes data to the client

          //Send a message to the client and the server writes data to the client

          ##OutputStream out = socket.getOutputStream(); //Get the output stream object

          out.write("Client, I received it!".getBytes());

          Close server resources

          serverSocket.close();

        • Establish TCP protocol service

          Socket socket = new Socket(InetAddress.getLocalHost(), 9090);

        • Send a message to the server_The client writes data to the server

          OutputStream out = socket.getOutputStream();

          ##out.write("This is my first tcp example!" .getBytes());

        • Get the input stream and read the data transmitted by the server
          InputStream inputStream = socket.getInputStream();

          byte[] buf = new byte[1024];

          int length = inputStream.read(buf);

          System.out.println("Data received by the client: "+ new String(buf,0,length));

        • Close client resources
          socket.close();

        • Client_Steps:
        • ##Server_Steps:
      ## Socket

      ##accept

      () Listens for and accepts connections to this socket.

      Question:

      Why does ServerSocket not have a method corresponding to getOutputStream?

      Because the server is connected to multiple clients, in order to prevent confusion, the getOutputStream and getInputStream operations are performed by establishing a socket with each client

The above is the detailed content of Detailed explanation of the specific steps of network programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!