Client/server sockets class based on multithreading

Source: Internet
Author: User
Tags error handling socket thread port number

This article mainly describes how to design a client/server socket (socket) class based on multithreading. The developer/designer can decide whether to choose another thread depending on the specific needs. There are many other sockets classes on the network, but none can provide callback functionality (event monitoring) to your application as this class does. The classes designed in this article provide you with the following event monitoring: Completion of link creation, abandonment of links, link failure, and receipt of data (including 0-byte packets).

Describe:

This article provides a new socket class that supports both TCP and UDP communications, and the classes that are designed in this article have many advantages over those that you might find in this site or elsewhere. First of all, this class does not have the restriction of the window handle as other classes require, and if you want only one control application, this limitation is not good, so the class in this article has no such limitations. It also provides you with the automatic thread support feature, which links or disconnects through a class handle, and provides other features that are not currently available for the classes I see. This class supports both client and server sockets, and a server-side socket can be designated as a socket that can accept many links, and a client socket can link a server-side socket. You can also use this class to communicate between two applications without creating a link. In the second case, you can create two UDP sockets (one for each program). This class helps reduce the amount of coding when creating a chat room program, or two or more IPC (interprocess communication) programs (processes). It also provides reliable communication support for both sides that have TCP/IP error handling. You might use a certain IP address to pass data to a destination that can be controlled (just for UDP), and this class of TCP can only communicate on both sides of the equation.

Now that you are unfamiliar with IP sockets, the following paragraphs provide a detailed description of how this class works. This is the goal of this article: to interpret the basic functionality of a socket object.

TCP/IP stack

TCP/IP stacks are shorter than the OSI stack.

TCP is a connection-oriented protocol, and UDP (User Datagram Protocol) is oriented to connectionless protocols.

IP datagram

The IP layer provides a connectionless, unreliable transmission mechanism. It thinks that each packet is independent of each other. The connection between any datagram must be supported by a higher layer. The IP layer provides a checksum that contains its own data header. The data header contains the source and destination addresses of the datagram. The IP layer handles routing over the network. For transmission, it also provides a mechanism to truncate large datagrams into many small datagrams and regroup them at the destination.

Udp

UDP is connectionless and unreliable. It adds the checksum (checksum) and port number of the datagram content to the IP address.

Tcp

TCP provides a logical, reliable, connectionless protocol on IP that provides a virtual loop in which two programs can communicate.

Internet address

In order to use a service, you first have to find it. The Internet uses address planning computers so that computers can be positioned. An IP address is a 32-bit integer that encodes a network ID and a considerable number of addresses. Depending on the size of the network address, the network ID is divided into many classes.

Network address

A class network uses 8 bits as the network address, and the remaining 24 digits represent other addresses. Class B network using 16-bit as a network address, C-Class network using 24-bit as the network address, D-Class network using all 32 bits as a network address

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.