How to realize TCP and UDP Network programming by C++ Socket
This article will explain in detail how C++ Socket implements TCP and UDP network programming. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Preface
Socket programming is divided into two modules: TCP and UDP, in which TCP is reliable and secure, and is often used to send files, while UDP is unreliable and insecure, and is often used for video calls.
As shown below:
Header files and libraries:
# include # pragma comment (lib, "ws2_32.lib")
Preparatory work:
After creating the project, first right-click the project and select properties
Then select CAccord Craft +-preprocessor-preprocessor definition
Add the string _ WINSOCK_DEPRECATED_NO_WARNINGS to it and click apply!
TCP
Connection process diagram:
Both the tcp server and client are created by following the steps in the figure above!
1)。 Server
Initialize socket library
Socket () in the corresponding figure
WORD wVersion;WSADATA wsaData;int err;// setting version, it can be understood as 1.1wVersion = MAKEWORD (1,1); / / example: MAKEWORD (a, b)-- > b | a b | a