java foundation (network programming --- Transfer Protocol UTP, TCP)

TCP transport protocol

  TCP: A connector faces (connection-oriented) reliable, the transport layer is based on byte stream (Transport layer) communication protocols.

    Features: a connection-oriented

       2. Point to Point Communications

       3. High reliability

       4. take up system resources, low efficiency

  The basic process:    

   Create a client
   1, to establish a connection: Socket created using the address and port of the client service +
   2: Enter the output stream operation
   3, the release of resources

  Create a server
   1, using the specified port ServerSocket creating server
   2, waiting for the connection blocking Accept
   . 3, operation: operation of the input and output stream
   4, releasing resources

UDP transport protocol

  UDP: connectionless transport layer protocol provides transaction-oriented messaging service simply unreliable.

    Features: 1 non-connection-oriented, reliable transmission is not, may be lost

       2. Regardless of whether the other party is ready to send, the recipient does not receive confirmation

         3. You can send broadcast

         4. Very simple protocol overhead small 

  UDP basic concepts

  DatagramSocket: a socket for receiving or transmitting data packets
  DatagramPacket: Packet
 
  The basic process:  

   The transmitting end
    1. Create DatagramSocket transmitting side designated port
    2. Prepare the data must be converted into a byte array
    3. The encapsulated package requiring a destination DatagramPacket
    4. The package sent Send (Packet DatagramPacket) *
    5. The release resources

  Receiving end

  * Address already in use: Connot bind port conflict does not allow the same protocol
  * 1. Create DatagramSocket designated port receiving end
  * 2. Prepare a packaged into a package DatagramPacket
  * 3. receive packages blocking the receive (Packet DatagramPacket)
  * 4. Analysis data:
    * byte [] the getData ()
    * getLength ()
  * 5. The release resources

Guess you like

Origin www.cnblogs.com/skyline1/p/11228561.html