1 #ifndef _MYSOCKET_H_ 2 #define _MYSOCKET_H_ 3 #include <string> 4 #include <WinSock2.h> 5 6 class MySocker 7 { 8 public: 9 enum TypeSocket 10 { 11 BlockingSocket, 12 UnBlockingSocket 13 }; 14 MySocker(); 15 MySocker(SOCKET new_socket); 16 virtual ~MySocker(); 17 std::string ReceiveLine(); 18 bool SendLine(std::string buffer); 19 bool operator==(unsigned long code) 20 { 21 return socket_ == code; 22 } 23 void Close(); 24 25 protected: 26 SOCKET socket_; 27 private: 28 static bool init_winsocket;//防止socket被重复初始化 29 }; 30 31 class SocketClient :public MySocker 32 { 33 public: 34 SocketClient(const std::string &host, int port); 35 }; 36 37 class SocketServer :public MySocker 38 { 39 public: 40 SocketServer(int port, int connections, TypeSocket type = BlockingSocket);//1、端口 2、接收的连接数 3、 41 MySocker* Accept(); 42 }; 43 44 #endif // _MYSOCKET_H_
1 #include "stdafx.h" 2 #include "MySocker.h" 3 4 bool MySocker::init_winsocket = false; 5 MySocker::MySocker() 6 { 7 if (!init_winsocket) 8 { 9 //保证单一性 10 WSADATA info; 11 if (WSAStartup(MAKEWORD(2, 2), &info)) 12 throw "can not start WSA"; 13 init_winsocket = true; 14 } 15 16 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 17 if (INVALID_SOCKET == socket_) 18 { 19 throw "Invalied sock"; 20 } 21 } 22 23 24 MySocker::MySocker(SOCKET new_socket) 25 { 26 socket_ = new_socket; 27 } 28 29 MySocker::~MySocker() 30 { 31 } 32 33 std::string MySocker::ReceiveLine() 34 { 35 std::string ret; 36 while (true) 37 { 38 char buf; 39 switch (recv(socket_, &buf, 1, 0)) 40 { 41 case 0: 42 return ret; 43 break; 44 case -1: 45 return ""; 46 break; 47 default: 48 break; 49 } 50 ret += buf; 51 if (buf == '\n') 52 { 53 break; 54 } 55 } 56 return ret; 57 } 58 59 bool MySocker::SendLine(std::string buffer) 60 { 61 buffer += '\n'; 62 return SOCKET_ERROR != send(socket_, buffer.c_str(), buffer.length(), 0); 63 } 64 65 void MySocker::Close() 66 { 67 closesocket(socket_); 68 } 69 70 SocketClient::SocketClient(const std::string &host, int port) 71 { 72 bool isok = false; 73 do 74 { 75 HOSTENT *he = gethostbyname(host.c_str()); 76 if (!he) 77 { 78 break; 79 } 80 sockaddr_in addr = { 0 }; 81 addr.sin_family = AF_INET; 82 addr.sin_port = htons(port); 83 addr.sin_addr = *((in_addr*)he->h_addr); 84 85 if (SOCKET_ERROR == connect(socket_, (sockaddr*)&addr, sizeof(sockaddr))) 86 { 87 break; 88 } 89 isok = true; 90 } while (false); 91 if (!isok) 92 { 93 char error_msg[MAXBYTE] = { 0 }; 94 DWORD erroc_coe = WSAGetLastError(); 95 strerror_s(error_msg, WSAGetLastError()); 96 throw error_msg; 97 } 98 } 99 100 SocketServer::SocketServer(int port, int connections, TypeSocket type /*= BlockingSocket*/) 101 { 102 sockaddr_in addr = { 0 }; 103 addr.sin_family = PF_INET; 104 addr.sin_port = htons(port); 105 if (type == UnBlockingSocket) 106 { 107 //设置非阻塞 108 unsigned long arg = 1; 109 ioctlsocket(socket_, FIONBIO, &arg); 110 } 111 112 if (SOCKET_ERROR == bind(socket_, (sockaddr*)&addr, sizeof(sockaddr))) 113 { 114 Close(); 115 throw "Bind Error"; 116 } 117 118 listen(socket_, connections); 119 } 120 121 MySocker* SocketServer::Accept() 122 { 123 MySocker *accept_socket = new MySocker(accept(socket_, nullptr, nullptr)); 124 if (*accept_socket == INVALID_SOCKET) 125 { 126 throw "Invalid socket"; 127 } 128 return accept_socket; 129 }
1 // WebServerDemo.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <iostream> 6 #include <process.h> 7 #include <fstream> 8 #include "MySocker.h" 9 10 #pragma comment(lib,"ws2_32.lib") 11 12 unsigned int __stdcall Connection(void *p) 13 { 14 MySocker *accept_socket = (MySocker*)p; 15 16 std::ifstream file("1.txt"); 17 std::string buf; 18 while (getline(file, buf)) 19 { 20 accept_socket->SendLine(buf); 21 } 22 23 accept_socket->SendLine("<html><body><h1>Hello Word</h1><p>Hello Word</p></body></html>"); 24 while (true) 25 { 26 //每次发送一个数据去浏览器,其实浏览器也有数据返回给服务器 27 std::string buffer = accept_socket->ReceiveLine(); 28 if (buffer.empty()) 29 { 30 break; 31 } 32 std::cout << buffer; 33 } 34 delete accept_socket; 35 return 0; 36 } 37 38 int _tmain(int argc, _TCHAR* argv[]) 39 { 40 /*try 41 { 42 SocketClient web_read("www.baidu.com", 80); 43 web_read.SendLine("GET / HTTP/1.0"); 44 web_read.SendLine("Host:www.baidu.com"); 45 web_read.SendLine(""); 46 while (true) 47 { 48 std::string buf = web_read.ReceiveLine(); 49 if (buf.empty()) 50 { 51 break; 52 } 53 std::cout << buf; 54 } 55 } 56 catch (const char *error_msg) 57 { 58 std::cout << error_msg << std::endl; 59 }*/ 60 61 try 62 { 63 SocketServer web_server(80, 10); 64 while (true) 65 { 66 MySocker *accept_socket = web_server.Accept(); 67 CloseHandle((HANDLE)_beginthreadex(nullptr, 0, Connection, accept_socket, 0, nullptr)); 68 //accept_socket->SendLine("<html><body><h1>Hello Word</h1><p>Hello Word</p></body></html>"); 69 } 70 } 71 catch (const char * e) 72 { 73 std::cout << e << std::endl; 74 } 75 catch (...) 76 { 77 std::cout << "Unknow Error" << std::endl; 78 } 79 80 return 0; 81 }