Socket Network Programming notes

What constitutes a network, the computer network is?

Network : In the computer field, the network information is transmitted, received, shared virtual platform, through which information can contact various points, surfaces (between the tissue), the body (e.g., a public app) together in order to achieve these resources shared. LAN : a plurality of devices (mobile computer, etc.) connected to a router, a LAN is formed, the final router or twisted pair connected by fibers to the WAN, the Internet achieved. Internet : countless local area networks, metropolitan area networks like the Internet

What is network programming?

Network programming is to transmit and receive information, by operating the corresponding hardware resource scheduling api computer and exchange data using the transfer pipes.

Seven-layer network model (from Baidu)

Base layers: the physical, data link layer, the network layer,
the transport layer: TCP-UDP protocol layer, socket, network programming is based on the higher-level layer is the transport layer: the session layer, presentation layer, application layer

Socket

Two programs on the network via a bidirectional communication to realize the exchange of data, called the connection end of a socket, he was described in conjunction with the ip address and protocol port, is a general term tcp / ip protocol related to the api. And for distinguishing network communication connections between different application processes.

Socket transmission principle

Network communication via IP address and port

Socket and tcp

  1. It is a connection-oriented protocol tcp
  2. Handshake to establish a connection, communication is accomplished by connecting three times to dismantle
  3. Since tcp is connection-oriented, end only be used for all communications, a time can be connected to a port

Socket与udp

  1. udp is connectionless
  2. udp data includes information destination port number and source port number
  3. Connectionless-oriented, it can be broadcast transmission, is not limited to end to end. The tcp unable to Broadcast Communications

C / S model

tcp / ip protocol, the main mode of communication between two processes for the C / S model (client-server) Objective : collaborative data sharing between computers on the network resources, service models, processes (such as site access redis Service)
common the C / S model : FTP, SMTP, HTTP

Packets

  1. Segment is a TCP / IP network transport protocol process, plays a role in navigation route. A process to transmit data to the process B, process A stores the original data message parsing, and then add the corresponding header byte, then the byte transmission header process B, process B byte packet header analysis obtained after segment, then the segment is passed to the application layer, the application layer packet and then the original data parsed into segments
  2. Segment for each network route to network queries, the address ip, ip packet switching protocol
  3. Packets during transmission will continue to be packaged into groups, packet, frame transmission

Transfer Protocol

Transfer Protocol is a kind of provision, a constraint .A and B make a call, you need A dialing, ringing, answer B, to follow these operations to make a call.

mac address

mac address position denoted by a network device, shaped like 45-45-33-36-00-00, we compare the identity card

UDP in the core API

DatagramSocket

  1. Class is used to send and udp, and both the server and the client is
  2. udp packet is not combined in the socket api 3.DatagramSocket (): create a simple example, and do not specify the IP port, the port will automatically select the locally available transmission 4.DatagramSocket (int port): Create an instance of a fixed listening port 5.DatagramSocket (int port, InetAddress localAddress) 6.receive (DatagramPacket d): receiving 7.send (DatagramPacket d): send
  3. setSoTimeout (int timeout): set the timeout time, in milliseconds
  4. close()

Datagram Packet

  1. For processing packet type, packet encapsulation packets and disassembly, which is the sending entity, the receiving entity is
  2. DatagramPacket (byte [] buf, int offset, int length, InetAddress address, int port); the first three parameters specify the use of the buff and buff interval, the latter two parameters are ip and port of the target machine, only these two parameters when a valid transmission, and DatagramPacket as used in the receiving entity is DatagramSocket ip and port.
  3. DatagramPacket (byte [] buf, int offset, int length, SocketAddress socketaddress), with two similar, SocketAddress equivalent InetAddress + port
  4. setData (byte [] buf, int offset, int length), the array can be set buf to be transmitted to the subject DatagramPacket
  5. setAddress (InetAddress address): Only valid when sending
  6. setPort (int port): Only valid when sending
  7. ...

UDP Unicast

Communication between end-

UDP multicast (multicast)

Transmitting information to a group of devices

UDP broadcast

All devices that send information to the network segment where the current device,

Guess you like

Origin blog.csdn.net/weixin_34314962/article/details/91390639