建立能够持续请求的CS网络程序
生活随笔
收集整理的這篇文章主要介紹了
建立能够持续请求的CS网络程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1 建立能夠持續(xù)請求的CS網(wǎng)絡程序
- 1.1 概述
- 1.2 服務端代碼
- 1.3 客戶端代碼
1 建立能夠持續(xù)請求的CS網(wǎng)絡程序
1.1 概述
我們在上一次的代碼基礎,建立能夠持續(xù)請求的CS網(wǎng)絡程序:
用Socket API建立簡易TCP服務端:
//–循環(huán)5-6
用Socket API建立簡易TCP客戶端
//–循環(huán)3-4
1.2 服務端代碼
server.cpp:
#include <iostream>#define WIN32_LEAN_AND_MEAN#include <Windows.h> #include <WinSock2.h>#pragma comment(lib, "ws2_32.lib")using namespace std;int main() {WSADATA data;WORD version = MAKEWORD(2, 2);// 初始化Sokcet庫WSAStartup(version, &data);//-- 用Socket API建立簡易TCP服務端// 1 建立一個socketSOCKET _sock;_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (INVALID_SOCKET == _sock){cout << "server : create socket error !" << endl;WSACleanup();return -1;}else{cout << "server : create socket success !" << endl;}// 2 bind 綁定用于接受客戶端連接的網(wǎng)絡端口sockaddr_in _sockaddr;_sockaddr.sin_family = AF_INET;_sockaddr.sin_port = htons(4567);_sockaddr.sin_addr.S_un.S_addr = ADDR_ANY; //inet_addr("127.0.0.1");int ret = bind(_sock, (sockaddr*)&_sockaddr, sizeof(sockaddr_in));if (SOCKET_ERROR == ret){cout << "server : bind error!" << endl;WSACleanup();return -1;}else{cout << "server : bind success !" << endl;}// 3 listen 監(jiān)聽網(wǎng)絡端口ret = listen(_sock, 5);if (SOCKET_ERROR == ret){cout << "server : listen error !" << endl;WSACleanup();return -1;}else{cout << "server : listen success !" << endl;}sockaddr_in _sockaddr_in;int len = sizeof(sockaddr_in);// 4 accept 等待接受客戶端連接SOCKET clientSock = accept(_sock, (sockaddr*)&_sockaddr_in, &len);if (INVALID_SOCKET == clientSock){cout << "server : accept bad socket!" << endl;}else{cout << "server : new client join, IP : " << inet_ntoa(_sockaddr_in.sin_addr) << endl;}char buffer[] = "hello, client! I am server !";while (true){// 5 從客戶端接收數(shù)據(jù)并處理int recvLen = recv(clientSock, buffer, sizeof(buffer), 0);if (recvLen > 0){cout << "recv : " << buffer << endl;if (0 == strcmp(buffer, "getName")){char msgBuffer[] = "lemon";send(clientSock, msgBuffer, strlen(msgBuffer) + 1, 0);}else if (0 == strcmp(buffer, "getAge")){char msgBuffer[] = "18";send(clientSock, msgBuffer, strlen(msgBuffer) + 1, 0);}else{char msgBuffer[] = "??? What are you want to ask?";send(clientSock, msgBuffer, strlen(msgBuffer) + 1, 0);}}else{cout << "client quit ..." << endl;break;}}// 6 關閉套節(jié)字closesocketclosesocket(_sock);// 對Socket資源進行處理WSACleanup();getchar();return 0; }1.3 客戶端代碼
client.cpp:
#include <iostream>#define WIN32_LEAN_AND_MEAN#include <Windows.h> #include <WinSock2.h>#pragma comment(lib, "ws2_32.lib")using namespace std;int main() {WSADATA data;WORD version = MAKEWORD(2, 2);// 初始化Sokcet庫WSAStartup(version, &data);//------------//-- 用Socket API建立簡易TCP客戶端// 1 建立一個socketSOCKET _sock;_sock = socket(AF_INET, SOCK_STREAM, 0); // 客戶端的協(xié)議類型可以直接給0if (INVALID_SOCKET == _sock){cout << "client : create socket error !" << endl;WSACleanup();return -1;}else{cout << "client : create socket success !" << endl;}// 2 連接服務器 connectsockaddr_in _sockaddr;_sockaddr.sin_family = AF_INET;_sockaddr.sin_port = htons(4567);_sockaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");int ret = connect(_sock, (sockaddr*)&_sockaddr, sizeof(sockaddr_in));if (SOCKET_ERROR == ret){cout << "client : connect error !" << endl;WSACleanup();return -1;}else{cout << "client : connect success !" << endl;}char cmdBuffer[128] = { 0 };char recvBuffer[128] = { 0 };while (true){// 3 接收用戶輸入并發(fā)送到服務器,然后接收回應cin >> cmdBuffer;if (0 == strcmp(cmdBuffer, "quit")){break;}send(_sock, cmdBuffer, strlen(cmdBuffer) + 1, 0);int len = recv(_sock, recvBuffer, sizeof(recvBuffer), 0);if (len > 0){cout << "client recv : " << recvBuffer << endl;}else{break;}}// 4 關閉套節(jié)字closesocketclosesocket(_sock);// 對Socket資源進行處理WSACleanup();getchar();return 0; }參考資料:
總結(jié)
以上是生活随笔為你收集整理的建立能够持续请求的CS网络程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么人招蚊子咬?
- 下一篇: 发送结构化的网络消息