一、echo客户/服务器程序
1、网络结构模型
a、集中式系统----有一个大型的中央处理系统,有点是数据容易备份,不易感染病毒,日常生活中的ATM、POS等使用的都是集中式系统
b、分布式系统----具有高度的可靠性、均衡负载、满足不同的需要
c、对等网络模型----P2P网络,每一台主机处于同等地位,既作为客户端,又充当其他主机的服务器,有可非为无结构网络、有结构网络、混合式网络、集中式网络
2、echo客户程序
mySocketClient.cpp:
#include "stdafx.h" #include "winsock2.h" #include "iostream" #pragma comment(lib, "ws2_32") #define ECHO_DEF_PORT 7 //连接的默认端口 #define ECHO_BUF_SIZE 256 //缓冲区的大小 #define InitSockets() { \ WORD version; \ WSADATA wsaData; \ version = MAKEWORD( 2,0 ); \ WSAStartup(version, &wsaData); } #define CleanupSockets() WSACleanup() void DoSoming() { SOCKET echoSoc = 0; struct sockaddr_in servAddr; unsigned short port = ECHO_DEF_PORT; int result = 0; int sendLen = 0; char *testSendData = "hello world"; char recv_buf[ECHO_BUF_SIZE]; sendLen = strlen(testSendData); servAddr.sin_family = AF_INET; servAddr.sin_port = htons(port); servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); if (servAddr.sin_addr.s_addr == INADDR_NONE) { std::cout << "[ECHO] invalid address" << WSAGetLastError() << std::endl; exit(-1); } echoSoc = socket(AF_INET, SOCK_STREAM, 0); result = connect(echoSoc, (struct sockaddr *)&servAddr, sizeof(servAddr)); if (result == 0) { result = send(echoSoc, testSendData, sendLen, 0); result = recv(echoSoc, recv_buf, ECHO_BUF_SIZE, 0); } if (result > 0) { recv_buf[result] = 0; std::cout << "[Echo Client] receives : " << recv_buf << std::endl; } else { std::cout << "[Echo Client] error : " << WSAGetLastError() << std::endl; } closesocket(echoSoc); CleanupSockets(); } int _tmain(int argc, _TCHAR* argv[]) { DoSoming(); return 0; }
3、echo服务器程序
mySokcetSer.cpp:
#include "stdafx.h" #include "winsock2.h" #include "iostream" #pragma comment(lib, "ws2_32") #define ECHO_DEF_PORT 7 //连接的默认端口 #define ECHO_BUF_SIZE 256 //缓冲区的大小 #define InitSockets() { \ WORD version; \ WSADATA wsaData; \ version = MAKEWORD( 2,0 ); \ WSAStartup(version, &wsaData); } #define CleanupSockets() WSACleanup() void DoSoming() { SOCKET echoSoc = 0; SOCKET acptSoc = 0; struct sockaddr_in servAddr; struct sockaddr_in clntAddr; unsigned short port = ECHO_DEF_PORT; int result = 0; int addrLen = sizeof(struct sockaddr_in); char recvBuf[ECHO_BUF_SIZE]; echoSoc = socket(AF_INET, SOCK_STREAM, 0); servAddr.sin_family = AF_INET; servAddr.sin_port = htons(port); servAddr.sin_addr.s_addr = INADDR_ANY; result = bind(echoSoc, (struct sockaddr *)&servAddr, sizeof(servAddr)); if (result == SOCKET_ERROR) { std::cout << "[Echo Server] bind error : " << WSAGetLastError() << std::endl; closesocket(echoSoc); exit(-1); } listen(echoSoc, SOMAXCONN); std::cout << "[Echo Server] is running ... ..." << std::endl; while (true) { acptSoc = accept(echoSoc, (struct sockaddr *)&clntAddr, &addrLen); if (acptSoc == INVALID_SOCKET) { std::cout << "[Echo Server] bind error : " << WSAGetLastError() << std::endl; break; } result = recv(acptSoc, recvBuf, ECHO_BUF_SIZE, 0); if (result > 0) { recvBuf[result] = 0; std::cout << "[Echo Server] receives : " << recvBuf << "from : " << inet_ntoa(clntAddr.sin_addr) << std::endl; result = send(acptSoc, recvBuf, result, 0); } closesocket(acptSoc); } closesocket(acptSoc); CleanupSockets(); } int _tmain(int argc, _TCHAR* argv[]) { DoSoming(); return 0; }