Java Network Programming----basics

Source: Internet
Author: User

1. Network Model

OSI Reference Model: Application Layer Presentation Layer Session Layer Transport Layer Network layer Data Link layer physical layer

TCP/IP reference model: Application layer Transport Layer Internetwork host to network layer

OSI Seven layer Brief:

Physical layer: The main definition of physical equipment standards, such as network cable interface type, fiber interface type, the transfer rate of various transmission media and so on

。 The main function is to transmit the bit stream (convert 1, 0 to current strength)

Data Link layer: The main is the data received from the physical layer of the MAC address of the package and package. This layer of work

Device is a switch

Network layer: The main data received from the lower layers are encapsulated and packaged with IP addresses. The primary device for this layer is the router.

Definition of the direction of packet propagation by routers

Transport Layer: Defines a number of protocols and port numbers (TCP/UDP) for transmitting data

Session Layer: Access to data transmission through the transport layer

Presentation layer: Interpret, encrypt and decrypt accepted data, compress and decompress

Application layer: The main corresponding computer application software, namely the terminal application

2. Network communication elements

IP Address

(1) Identification of devices in the network

(2) Hard to remember, available host name

(3) Local loopback address: 127.0.0.1 host name: localhost

Port number

(1) The logical address used to identify the process, and the identity of the different processes

(2) Valid port: 0~65535, where 0~1024 is the system use or reserved port

Transport protocol

(1) Communication rules

(2) Common protocol: TCP UDP

3, the difference between TCP and UDP protocol

TCP: Transmission Control Protocol calls, downloads data,

(1) Establish a connection to form a transmission data channel

(2) Large amount of data transfer in the connection

(3) through three handshake to complete the connection, is a reliable protocol (a: Dude, are you there?) B: I'm in. A: OK, I know you're here. )

(4) must establish a connection, the efficiency will be slightly lower


UDP: intercom QQ chat online video chat to speed-based, unreliable, the other party may not be on the line

(1) package data and source and destination into a packet, no need to establish a connection

(2) Limit the size of each packet within 64k

(3) Due to no connection, is an unreliable agreement

(4) No need to establish a connection, fast speed

4. What is socket

Socket ( socket)

(1) A socket is a mechanism for network services

(2) sockets on both ends of the communication

(3) network communication is actually the communication between sockets

(4) data is transmitted via IO between two sockets

The following is a simple socket communication using Java, the communication protocol is divided into TCP and UDP

In Java, the use of UDP for data transmission, the use of the API is mainly under the java.net package, code examples are as follows:

Send-side code:

import java.io.ioexception;import java.net.datagrampacket; import java.net.datagramsocket;import java.net.inet6address;public class udpsenddemo  {/* *  Create the send side of the UDP transport.   Ideas:  (1) To set up a UDP socket service   *  (2) package the data to be sent into the packet  *  (3) Send the data packets through the UDP socket service   *  (4) Close socket service  */public static void main (String[] args)  throws  ioexception {system.out.println ("Send side start ...");//Start Socket service datagramsocket socket =  New datagramsocket ();//data to be sent string str =  "Are you a * *?"; Byte[] bt = str.getbytes ();//The packet that needs to be sent, which encapsulates the data content, data size, destination IP address, port number of the destination application DATAGRAMPACKET DP  = new datagrampacket (Bt, bt.length,inet6address.getbyname ("fe80::c93c:a4cb:e167:cb7e%14") ,  10000); System.out.println (Dp.getaddress (). gethostaddress ());//Send packet Socket.send (DP);//Close Socket service socket.close ();}} 


Receive-Side code:

Import java.io.ioexception;import java.net.datagrampacket;import java.net.datagramsocket;public  class UDPReceiveDemo {/* *  the idea of establishing a UDP receiver:  *  (1) Establish Udp socket service   *  (2) Create a packet that stores the received data and facilitates the resolution of these data by means of a number of packet objects  *  (3) using the Receive method of the socket service to store the received data in a packet  *  (4) parse data in a packet by means of a packet   (5) Close resource  */public static void main (string[]  args)  throws ioexception {system.out.println ("Receive Side start ...");//Create socket service and set port number Datagramsocket  socket = new datagramsocket (10000);//Create a packet to be brave to receive data byte[] buf = new  BYTE[1024];D atagrampacket dp = new datagrampacket (buf, buf.length);//Receive Socket.receive (DP);//Parse out the contents of the packet and print it out string text = new string (Dp.getdata (),  0, dp.getlength ()); Int port = dp.getport (); String ip = dp.getaddress (). gethostaddress (); System.out.println (ip +  ":"  + port +  "said:"  + text);//Close Socket service socket.close ();}} 


Java Network Programming----basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.