Talk about sockets, TCP/IP, HTTP, FTP, and network programming

Source: Internet
Author: User
Tags ack ftp client file transfer protocol

Talk about sockets, TCP/IP, HTTP, FTP, and network programming
    SubmitMy message
      Load in

      have left a message



      1 What's all this?


      Since it is a network transmission, involving the interaction between several systems, the first thing to consider is how to accurately locate one or several hosts on the network, and the other is how to carry out reliable and efficient data transmission. This will use the TCP/IP protocol.


      1.1 TCP/IP protocol group


      TCP/IP (Transmission Control Protocol) consists of the IP protocol of the network layer and the TCP protocol of the Transport layer.


      The IP layer is responsible for the location of the network host, the routing of Data transmission, the IP address can uniquely determine a host on the Internet.

      The TCP layer is responsible for the application-oriented reliable or unreliable data transmission mechanism, which is the main object of network programming.


      TCP/IP is a protocol group that can be divided into three levels: the network layer, the transport layer, and the application layer:


      Network layer: IP protocol, ICMP protocol, ARP protocol, RARP protocol and BOOTP protocol;

      Transport layer: TCP protocol and UDP protocol;

      Application layer: FTP, HTTP, TELNET, SMTP, DNS and other protocols;



      HTTP is an application-layer protocol whose transmissions are packaged as TCP protocol transmissions. You can implement HTTP with a socket. A socket is a programming API that implements the Transport layer protocol, either TCP or UDP.


      1.2 TCP


      The Tcp-Transmission Control Protocol provides a connection-oriented, reliable byte-stream service. Before the customer and the server Exchange data with each other, a TCP connection must be established between the two parties before the data can be transferred. TCP provides time-out re-send, discard duplicate data, test data, flow control and other functions to ensure that data can be transmitted from one end to the other. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively shuts down the connection. When disconnected, both the server and the client can proactively initiate a request to disconnect a TCP connection.


      TCP is a connection-oriented protocol that guarantees reliable transmission. With the TCP protocol transmission, a sequential error-free data stream is obtained. A connection must be established between the sender and the receiver's paired two sockets to communicate on the basis of the TCP protocol, and when a socket (usually a server socket) waits for a connection, the other socket can require a connection. Once the two sockets are connected, they can carry out two-way data transfer and both can send or receive operations.

      TCP Features:


      TCP is a connection-oriented protocol, the connection is established through three handshakes, the connection is removed when the communication is complete, because TCP is a connection-oriented protocol, so it can only be used for point-to-point communication. and establishing a connection also consumes time and overhead.


      TCP transmits data with no size limit for large data transfer.


      TCP is a reliable protocol that ensures that the receiver can receive all the data sent by the sender in full and proper order.


      To understand TCP, be sure to know "three times handshake, four times goodbye" so-called three times handshake, is to send data before the connection must be established to call three times handshake, shook hands before the beginning of the hair, which is the meaning of connection-oriented.


      First handshake: The client sends a SYN packet (SYN=J) to the server and enters the Syn_send state, waiting for the server to confirm;


      Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=J+1), and also send itself a SYN packet (syn=k), that is, the Syn+ack packet, when the server enters the SYN_RECV state;


      Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and the server enter the established state, complete three handshake;


      "Where applicable"


      TCP sends the packet has the serial number, the other party receives the packet to give a feedback, if has not received the feedback to automatically perform the time-out resend, therefore the TCP biggest advantage is reliable. General Web page (HTTP), Mail (SMTP), remote connection (Telnet), file (FTP) transfer is used with TCP


      TCP has a strong vitality in network communications, such as remote Connection (Telnet) and file Transfer (FTP), which require the data to be reliably transmitted over an indefinite length. But reliable transmission is to pay a price, the correctness of the data content of the test must occupy the computer processing time and network bandwidth, so the efficiency of TCP transmission is not as high as UDP.


      1.3 UDP


      The udp-User Datagram Protocol is a simple non-connected Transport layer protocol for datagrams. UDP does not provide reliability, it simply sends the application to the IP layer's datagram, but does not guarantee that it will reach its destination. Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as time-out retransmission, the transmission speed is very fast.


      UDP is a non-connection-oriented protocol, each datagram is a separate information, including the full source address or destination address, it on the network with any possible path to the destination, so can reach the destination, the time to reach the destination and the correctness of the content is not guaranteed.

      UDP Features:


      UDP is a non-connection-oriented communication protocol, UDP data includes the destination port number and the source port number information, because the communication does not need to connect, so it can be implemented broadcast send .


      UDP transmits data with a size limit, and each transmitted datagram must be limited to 64KB.


      UDP is an unreliable protocol in which datagrams sent by the sender do not necessarily reach the receiver in the same order.


      "Where applicable"


      UDP is a message-oriented protocol, communication does not need to establish a connection, the transmission of data is naturally unreliable, UDP is generally used for multipoint communication and real-time data services, such as voice broadcast, video, QQ, TFTP (Simple File transfer), SNMP (Simple Network Management Protocol), RTP (Real-time delivery protocol) RIP (Routing Information protocol such as reporting stock market, aviation information), DNS (Domain name interpretation). Pay attention to the smooth speed.


      UDP is simple to operate and requires less monitoring, so it is often used for client/server applications in decentralized systems with high LAN reliability. For example, the video conferencing system, does not require audio and video data is absolutely correct, as long as the consistency can be guaranteed, in this case, it is obvious that using UDP is more reasonable.


      1.4 Socket


      Sockets are also commonly referred to as "sockets," which describe IP addresses and ports, and are a handle to a communication chain. The two programs on the network realize the exchange of data through a two-way communication connection, one end of this bidirectional link is called a socket, and a socket is determined only by an IP address and a port number. Applications typically make requests to the network through sockets or answer network requests. Socket is a very popular programming interface of TCP/IP protocol, but the type of protocol supported by socket is not only TCP/IP, so there is no necessary connection between the two. In the Java environment, socket programming mainly refers to the network programming based on TCP/IP protocol.


      Socket communication process: The server listens to a port for a connection request, the client sends a connection request to the server, and the server receives a connection request to send a message to the client, so that a connection is set up. Both the client and the server can communicate with each other by sending messages to each other.


      A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.


      Since the socket connection is usually a TCP connection, once the socket connection is established, the communication parties can start sending data content to each other until the two sides are disconnected. However, in real network applications, the client-to-server communication often needs to traverse multiple intermediary nodes, such as routers, gateways, firewalls, and so on, most firewalls will turn off long inactive connections and cause the Socket connection to be disconnected, so it needs to be polled to tell the network that the connection is active.


      1. Socket (socket) Concept: socket (socket) is the cornerstone of communication, is the basic operating unit of network communication support TCP/IP protocol. It is an abstract representation of the endpoint in the network communication process and contains five kinds of information that must be used for network communication: the protocol that the connection uses, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol port of the remote process. When the application layer communicates data through the transport layer, TCP encounters a problem that provides concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may require data to be transmitted over the same TCP protocol port. To differentiate between different application processes and connections, many computer operating systems provide a socket (socket) interface for applications interacting with the TCP/IP protocol. The application layer can communicate with the transport layer through the socket interface, differentiate the communication from different application processes or network connections, and realize the concurrent service of data transmission.


      2. Establishing a socket connection: establishing a socket connection requires at least one pair of sockets, one running on the client, called Clientsocket, and the other running on the server side, called ServerSocket. The connection between sockets is divided into three steps: Server listening, client request, connection acknowledgement.


        Server monitoring: Server-side sockets do not locate specific client sockets, but in the status of waiting for the connection, real-time monitoring network status, waiting for the client connection request;

        Client request: Refers to the client's socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.

        Connection confirmation: When a server-side socket hears or receives a connection request from a client socket, it responds to a client socket request, establishes a new thread, sends a description of the server-side socket to the client, and once the client confirms the description, the two sides formally establish the connection. While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets.


      3. When you create a socket connection with a TCP connection, you can specify the transport layer protocol that is used, the socket can support different transport layer protocols (TCP or UDP), and the socket is a TCP connection when you connect using the TCP protocol.

      "Where applicable"


      In many cases, the server side is required to proactively push data to the client, keeping the client and server data in real time and in sync. At this time, if the two sides established a socket connection, the server can directly transfer data to the client;


      1.5 HTTP


      HTTP protocol is an application based on the TCP protocol, the HTTP connection uses a "request-response" method, not only need to establish a TCP connection in the request, but also requires the client to make a request to the server, the request contains the request method, URI, protocol version, and related MIME-style messages , the server side can reply to the data, including the protocol version of the message, a success and failure code, and the associated MIME style message. After the request is finished, the connection is released actively. The process from establishing a connection to closing a connection is called a "one-time connection." Because HTTP is actively releasing the connection after each request ends, the HTTP connection is a "short connection", which requires constant connection requests to the server to maintain the client program's online status. As a general practice, there is no need to obtain any data immediately, and the client will keep a "keep-connected" request to the server at regular intervals, and the server responds to the client after receiving the request, indicating that the client is "online". If the server can not receive the client's request for a long time, it is considered that the client "offline", if the client cannot receive a reply from the server for a long time, it is considered that the network has been disconnected.


      Http/1.0 creates a new TCP link for each HTTP request/response, so a page containing HTML content and pictures will need to establish multiple short-term TCP links. The establishment of a TCP link will require a 3-time handshake.


      In addition, in order to obtain the appropriate transfer speed, TCP is required to spend additional loop link time (RTT). Each time the establishment of the link requires this recurring overhead, and it does not have the actual useful data, just to ensure the reliability of the link, so http/1.1 put forward a sustainable link implementation method. http/1.1 will use it to transmit a series of request/response messages repeatedly using only one TCP link, thus reducing the number of link builds and the frequent link overhead.


      Conclusion: HTTP is an application-layer protocol, and its transmission is packaged into TCP protocol transmission. You can implement HTTP with a socket. A socket is a programming API that implements the Transport layer protocol, either TCP or UDP.


      "Where applicable"


      If the two sides establish an HTTP connection, the server needs to wait until the client sends a request to send the data back to the client, so the client periodically sends a connection request to the server, not only to remain online, but also to "ask" the server if there is new data, and if so, pass the data to the client.


      1.6 FTP


      The File Transfer Protocol (Files Transfer Protocol, FTP) is the protocol that transmits files to two computers on a TCP/IP network, and FTP is one of the earliest protocols used on TCP/IP networks and the Internet, which belongs to the application layer of the Network protocol group. The FTP client can issue commands to the server to download files, upload files, and create or change directories on the server.


      2 N-Layer switching technology
      2.1 Two-layer switching


      Switching principle: The end-to-end data exchange is realized according to the MAC address of the second layer data link layer;

      Work Flow:


      (1) A port of the switch receives the packet, reads the source MAC address, obtains the source MAC address machine to connect the port;

      (2) Read the destination MAC address, find the corresponding port in the Address table;

      (3) If the Address table has the destination MAC address corresponding port, directly copy the data to this port;

      (4) If the Address table does not have the destination MAC address corresponding port, broadcast all the ports, when the destination machine responds, update the Address table, the next time you do not need to broadcast;


      Continuous cycle of the above process, the entire network of MAC address information can be learned, the second layer of the switch to learn and maintain its address table. The second layer of switch based on MAC selection port forwarding data, the algorithm is very simple, it is convenient to use inexpensive chip implementation, and fast.


      2.2 Three-layer switching


      Exchange principle: The end-to-end data exchange is done according to the IP address of the third layer network layer;


      Scenario: A (IP1) + three-layer switch =>b (IP2)


      Work Flow:


      (1) A Send data to B, according to the IP address of B + Subnet mask, a can determine whether B and oneself are in the same network segment;

      (2) b if and a in the same network segment, but a do not know the MAC address of B, a will send an ARP request to obtain the MAC address of B, and according to the MAC through the two layer switch to send data to B;

      (3) b if and a are not in the same network segment, and do not know the MAC address of B, a will send data packets to the gateway (A's local must have a Gateway MAC address). After the gateway receives the packet, the source MAC address is modified to the gateway's own MAC address, and the destination IP corresponds to the MAC address of the destination MAC address to complete the data exchange.


      It seems that the third layer of switches is a combination of the second layer of switch + routing capabilities, which is not true: after the data is forwarded through the third layer, the mapping between IP and Mac is recorded, and the next time you need to forward it, it will not go through the third layer device.


      2.3 Four-layer switching


      Two-layer and three-layer switching devices are based on end-to-end switching, an IP-and Mac-address-based switching technology that has very efficient transfer rates, but lacks the ability to dynamically exchange data based on the needs of the host application.


      The four-layer equipment not only can complete the end-to-end exchange, but also can allocate or limit its traffic according to the application characteristics of the target host;


      The four-layer device is based on the Transport Layer Packet exchange, is a kind of equipment built in the application layer of TCP/IP to realize user application requirements; It realizes the access control and quality assurance service of a class of application layer, so it is a software network management system rather than a hardware device.


      Four-layer switching core technology


        1. Packet filtering uses four layer information to define the filtering rules, can control the TCP/UDP communication of the specified port, it can be implemented in the high-speed chip, and greatly improve the packet filtering rate.


        2. Package priority level Three below the device only MAC,PORT,IP information, because the lack of four layer of information, unable to confirm TCP/IP and other four-tier priority information; The four-tier device allows for prioritization based on a combination of destination addresses/ports (that is, application services).


        3. Load balancer will attach the IP address of the Load Balancer service, make a cluster through different physical services, provide the same service, and define it as a separate virtual server; This virtual server is a logical server with independent IP, and the user traffic only needs to flow to the virtual server IP. Without communicating with the physical server;


          Only the network address translation (NAT) is performed through the switch to obtain real access;

          In the virtual server group, the conversion traffic is balanced, which is related to OSPF, RIP, VRRP and other protocols.


        4. The host standby connection is similar to the technology contained in (3), which can realize the automatic switching of the master and standby IP.

      2.4 Seven-layer switching


      Switching principle: more complex switching functions (e.g. routing based on HTTP messages) can be done based on the data packets of the application layer, which is further than the four layers. Since there is no specific standard for the seven-layer exchange, the article is not much expanded.


      3 JDK Socket


      There are two classes under the Java.net package: Sockets and ServerSocket. ServerSocket is used for server-side, socket is used when establishing network connection. When the connection succeeds, a socket instance is generated at both ends of the application, manipulating the instance to complete the required session. For a network connection, sockets are equal, and there is no difference, not due to different levels on the server side or on the client. Either the socket or the ServerSocket their work is done through the SocketImpl class and its subclasses.


      Lists a few common construction methods:




      In the constructor parameter, address, host, and port are the IP address, hostname, and port number of the other side of the two-way connection, and stream indicates whether the socket is a stream socket or datagram Socket,localport the port number of the local host. Localaddr and BINDADDR are the address of the local machine (the host address of the ServerSocket), Impl is the parent of the socket and can be used to create serversocket and to create sockets. Count indicates the maximum number of connections that can be supported by the server.


      Note: You must carefully select the port number. Each port provides a specific service, and only the correct port is given to obtain the appropriate service. The port number of the 0~1023 is reserved for the system, such as the port number of the HTTP service for the 80,telnet service port number is 23, so when we select the port number, it is best to select a number greater than 1023 to prevent a conflict.


      A few important socket methods:




      "Blocking" is a term that causes a program to run temporarily "stuck" in this place until a session is generated, and then the program continues; usually "blocking" is generated by loops.


      Note: Where both the getInputStream and Getoutputstream methods produce a ioexception, it must be captured because the stream object they return is usually used by another stream object.


      4 Basic Client/server Procedures


      The following is a basic client/server-side program code. The main implementation of the server side has been listening to a port, waiting for client connection request. The client connects to the server based on the IP address and port number, enters a line of information from the keyboard, sends it to the server side, receives the information returned by the server, and ends the session. This program can only accept one client connection at a time.


      Client program:




      Server-side programs:




      5 Multi-Client Connection server


      The server-side program above can only connect to one client at a time, which is obviously not possible in practical applications. The usual network environment is that multiple clients connect to a host for communication, so we need to retrofit the above program.


      Design ideas: The server-side main program listens to a port, the client initiates a connection request, the server-side main program receives the request, and constructs a thread class to take over the session. When a socket session is generated, the session is handed over to the thread for processing and the main program continues to listen.


      The following implementation process is: the client and the server to establish a connection, the client sends a message, the service side according to the message processing and return the message, if the client requests to close, the server closes the connection, the end of communication between the two sides.


      Client program:




      Server-side programs:




      6 mass information sharing


      Although more than one client and server connection is implemented, the message is still being propagated between a client and a server. Now we want to implement information sharing, that is, the server can send broadcast messages to multiple clients, and the client can also send messages to other clients. Similar to the function of a chat room, implementation information can be shared among multiple clients.


      Design idea: The client loop can send messages to the server without stopping, and a thread is dedicated to listen for messages sent from the server and print out the output. On server-side startup, start a thread that listens to when a message needs to be sent to the client. Each time a client connection request is accepted, a thread is restarted to process and the client information is stored in the public collection. When the client sends a message, the server side stores the message order in the queue, and when the output is required, the broadcast is taken from the queue to each client. The client Input Showuser command can view a list of online users and enter bye to request an exit connection from the server side.


      Client code:




      Server-side code:




      7 File Transfer


      The client transmits the file to the server side, the service side obtains the file name to save, obtains the size computation transfer progress, is relatively simple, the direct paste code.


      Client code:




      Server-side code:





      Featured Message

      The author of the article has been set to pay attention before leaving a message

      Write a message

        The author of the article has been set to pay attention before leaving a message

        Write a message

        Load inThe above message is screened by the public number and displayed

        Learn more about message features

        Sweep
        Follow the public number



        From for notes (Wiz)

        Talk about sockets, TCP/IP, HTTP, FTP, and network programming

        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.