In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
The latter program, no matter the server side or the client side, has an achievement, that is, it immediately joined after disposing of a request, which does not have much practical significance. Can't you keep up with client requests like the Web server? Yes, just use while reincarnation.
Fix the response program so that the server can respond to the client's pleas from time to time.
Server server.cpp:
# include # include # pragma comment (lib, "ws2_32.lib") / / load ws2_32.dll # define BUF_SIZE 100 int main () {WSADATA wsaData; WSAStartup (MAKEWORD (2,2), & wsaData); / / create socket SOCKET servSock = socket (AF_INET, SOCK_STREAM, 0); / / bind socket sockaddr_in sockAddr; memset (& sockAddr, 0, sizeof (sockAddr)); / / populate sockAddr.sin_family = PF_INET with 0 in each byte / use IPv4 address sockAddr.sin_addr.s_addr = inet_addr ("127.0.0.1"); / / detailed IP address sockAddr.sin_port = htons (1234); / / Port bind (servSock, (SOCKADDR*) & sockAddr, sizeof (SOCKADDR)); / / enter listening mode listen (servSock, 20); / / accept client request SOCKADDR clntAddr; int nSize = sizeof (SOCKADDR); char buffer [BUF _ SIZE] = {0} / / buffer while (1) {SOCKET clntSock = accept (servSock, (SOCKADDR*) & clntAddr, & nSize); int strLen = recv (clntSock, buffer, BUF_SIZE, 0); / / accept the data sent by the client send (clntSock, buffer, strLen, 0); / / send the data as is to closesocket (clntSock); / / closed socket memset (buffer, 0, BUF_SIZE); / / reset buffer} / / closed socket closesocket (servSock) / / terminate the use of DLL WSACleanup (); return 0;}
Client client.cpp:
# include # pragma comment (lib, "ws2_32.lib") / / load ws2_32.dll # define BUF_SIZE 100int main () {/ / initialize DLL WSADATA wsaData; WSAStartup (MAKEWORD (2,2), & wsaData); / / request sockaddr_in sockAddr; memset (& sockAddr, 0, sizeof (sockAddr)) to the server; / / populate sockAddr.sin_family = PF_INET with 0 in each byte SockAddr.sin_addr.s_addr = inet_addr ("127.0.0.1"); sockAddr.sin_port = htons (1234); char bufSend [BUF _ SIZE] = {0}; char bufRecv [BUF _ SIZE] = {0}; while (1) {/ / create socket SOCKET sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); connect (sock, (SOCKADDR*) & sockAddr, sizeof (SOCKADDR)) / / get the string output by the user and send it to the server printf ("Input a string:"); gets (bufSend); send (sock, bufSend, strlen (bufSend), 0); / / accept the data returned by the server recv (sock, bufRecv, BUF_SIZE, 0); / / input the accepted data printf ("Message form server:% s\ n", bufRecv); memset (bufSend, 0, BUF_SIZE) / / reset buffer memset (bufRecv, 0, BUF_SIZE); / / reset buffer closesocket (sock); / / closed socket} WSACleanup (); / / terminate DLL return 0;}
Run the server first, and then the client. The consequences are as follows:
Input a string: c language
Message form server: c language
Input a string: C language Chinese website
Message form server: C language Chinese website
Input a string: a good website to learn more about CumberCraft + programming
Message form server: a good website to learn more about CumberCraft + programming
While (1) puts the code into the cycle of death, and unless the user closes the program, the server side will constantly listen to the client's pleas. The same is true of the client, which proposes convergence to the server from time to time.
It is important to note that misappropriating closesocket () in server.cpp will not only close the server's socket, but also tell the client that the connection has been disconnected, and the client will also liquidate the relevant capital of socket, so the client.cpp needs to put socket () outside the while reincarnation. Because the city liquidated socket at the end of each request, the demand will be re-created when the proposal is requested next time. We will stop the specific explanation later.
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.