In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge of how C++ can achieve a simple UDP network chat room. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Project name: NetSrv
NetSrv.cpp
/ / Server-side # include#includevoid main () {/ / load socket library WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD (1Pol 1); err = WSAStartup (wVersionRequested,&wsaData); if (erratically loaded 0) {return;} if (LOBYTE (wsaData.wVersion)! = 1 | | HIBYTE (wsaData.wVersion)! = 1) {WSACleanup (); return;} / / create socket SOCKET sockSrv = socket (AF_INET,SOCK_DGRAM,0) / / the second parameter is Datagram socket SOCKADDR_IN addrSrv;// for the server, that is, the receiver, first perform a binding to define a variable addrSrv.sin_addr.S_un.S_addr=htonl (INADDR_ANY) of an address structure; / / assign a pair of members of the structure and convert the number of hosts into unsigned long integer network byte order with htonl. This function converts a 32-bit number from the host byte order to the network byte order. AddrSrv.sin_family=AF_INET;// address family addrSrv.sin_port=htons (100); / / port number / / bind socket bind (sockSrv, (SOCKADDR*) & addrSrv,sizeof (SOCKADDR)); / / bind local host address and port / / socket, pointer to address structure, length char recvBuf of address structure; / / character array, receive message char sendBuf; / / send message char tempBuf / / storing intermediate data SOCKADDR_IN addrClient;// obtains the information of the other party when receiving, the variable int len=sizeof (SOCKADDR) of the address structure; / / initializes, the length of the address structure is while (1) / / ensures that the communication process can continue {/ / wait and receive data recvfrom (sockSrv,recvBuf,100,0, (SOCKADDR*) & addrClient,&len) / / socket, receive data buf, its length, pointer to address structure, pointer to integer is mainly used to return the received address length if ("Q" = = recvBuf [0]) / / to determine whether the first character of the received data is Q, {sendto (sockSrv, "Q", strlen ("Q") + 1line 0, (SOCKADDR*) & addrClient,len) / / if the server also sends a Q character, socket, Q, an extra byte in length, the address uses the length just obtained, and the last one is the length of the address structure, and printf ("Chat end!") can be obtained through len; break;} / / if the received is not Q, then the data is formatted and put into tempBuf, dotted decimal ip address, the received data. Sprintf (tempBuf, "% s say:% s", inet_ntoa (addrClient.sin_addr), recvBuf); printf ("% s", tempBuf); / / print out the data / / send data printf ("please input data:"); gets (sendBuf); / / get a row of data from the standard input stream, and when we enter a line of data to enter, we put the data in the specified memory pointer. Sendto (sockSrv,sendBuf,strlen (sendBuf) + 1Power0, (SOCKADDR*) & addrClient,len); / / socket, the data to be sent (this is the data length, obtained by strlen), send an extra byte, the address sent,} / / close the socket closesocket (sockSrv); WSACleanup (); / / terminate the use of the socket}
NetClient.cpp
/ / client # include#includevoid main () {/ / load socket library WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD (1score1); err = WSAStartup (wVersionRequested,&wsaData); if (erratically loaded 0) {return;} if (LOBYTE (wsaData.wVersion)! = 1 | | HIBYTE (wsaData.wVersion)! = 1) {WSACleanup (); return;} / / create socket SOCKET sockClient = socket (AF_INET,SOCK_DGRAM,0) / /, type (Datagram), automatically select the appropriate protocol SOCKADDR_IN addrSrv / / upd-based client (sender) does not need to bind, but need to set the address information sent to the other machine / / server-side address information, because on the local machine, it is still 127.0.0.1), using inet_addr to convert dotted decimal to ulang type addrSrv.sin_addr.S_un.S_addr = inet_addr ("127.0.0.1"); addrSrv.sin_family = AF_INET AddrSrv.sin_port = htons (6000); / / sets the port on which the server program waits for data char recvBuf; / / receives data char sendBuf; / / sends data char tempBuf; / / temporary data storage int len = sizeof (SOCKADDR); / / receives the length of the returned address structure while (1) {/ / send data printf ("Please input data:"); gets (sendBuf) Sendto (sockClient,sendBuf,strlen (sendBuf) + 1Power0, (SOCKADDR*) & addrSrv,len); / / wait and receive data recvfrom (sockClient,recvBuf,100,0, (SOCKADDR*) & addrSrv,&len); if ("Q" = = recvBuf [0]) {sendto (sockClient, "Q", strlen ("Q") + 1jin0, (SOCKADDR*) & addrSrv,len); printf ("Chat end!"); break } sprintf (tempBuf, "% s say:% s", inet_ntoa (addrSrv.sin_addr), recvBuf); / / server-side ip address information (formatted), received data printf ("% s", tempBuf);} / / close socket closesocket (sockClient); WSACleanup ();}
Ws2_32.lib
I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.