1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
| #include <stdint.h> #include <WinSock2.h> #include <WS2tcpip.h> #include <iostream> #include <map> #include <mutex> #include <thread> #pragma comment(lib, "ws2_32.lib")
#define LOGI(format, ...) fprintf(stderr,"[INFO] [%s:%d]:%s() " format "\n", __FILE__,__LINE__,__func__,##__VA_ARGS__) #define LOGE(format, ...) fprintf(stderr,"[ERROR] [%s:%d]:%s() " format "\n", __FILE__,__LINE__,__func__,##__VA_ARGS__)
class Server; class Connection { public: Connection(Server* server, int clientFd) :m_server(server), m_clientFd(clientFd) { LOGI(""); } ~Connection() { LOGI(""); closesocket(m_clientFd); if (th) { th->join(); delete th; th = nullptr; } } public: typedef void (*DisconnectionCallback)(void*, int); void setDisconnectionCallback(DisconnectionCallback cb, void* arg) { m_disconnectionCallback = cb; m_arg = arg; }
int start() { th = new std::thread([](Connection* conn) { int size; uint64_t totalSize = 0; time_t t1 = time(NULL);
while (true) { char buf[1024]; memset(buf, 1, sizeof(buf)); size = ::send(conn->m_clientFd, buf, sizeof(buf), 0); if (size < 0) { printf("clientFd=%d,send error,错误码:%d\n", conn->m_clientFd, WSAGetLastError()); conn->m_disconnectionCallback(conn->m_arg, conn->m_clientFd); break; } totalSize += size;
if (totalSize > 62914560) { time_t t2 = time(NULL); if (t2 - t1 > 0) { uint64_t speed = totalSize / 1024 / 1024 / (t2 - t1);
printf("clientFd=%d,size=%d,totalSize=%llu,speed=%lluMbps\n", conn->m_clientFd, size, totalSize, speed); totalSize = 0; t1 = time(NULL); } } }
}, this); return 0; } int getClientFd() { return m_clientFd; } private: Server* m_server; int m_clientFd; std::thread* th = nullptr;;
DisconnectionCallback m_disconnectionCallback = nullptr; void* m_arg = nullptr; };
class Server { public: Server(const char *ip,uint16_t port); ~Server(); public: int start() { LOGI("TcpServer2 tcp://%s:%d", m_ip, m_port); m_sockFd = socket(AF_INET, SOCK_STREAM, 0); if (m_sockFd < 0) { LOGI("create socket error"); return -1; } int on = 1; setsockopt(m_sockFd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
SOCKADDR_IN server_addr; server_addr.sin_family = AF_INET; server_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(m_port);
if (bind(m_sockFd, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) { LOGI("socket bind error"); return -1; } if (listen(m_sockFd, 10) < 0) { LOGE("socket listen error"); return -1; }
while (true) { LOGI("阻塞监听新连接..."); int clientFd; char clientIp[40] = { 0 }; uint16_t clientPort;
socklen_t len = 0; struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); len = sizeof(addr);
clientFd = accept(m_sockFd, (struct sockaddr*)&addr, &len); if (clientFd < 0) { LOGE("socket accept error"); return -1; } strcpy(clientIp, inet_ntoa(addr.sin_addr)); clientPort = ntohs(addr.sin_port); LOGI("发现新连接:clientIp=%s,clientPort=%d,clientFd=%d", clientIp, clientPort, clientFd);
Connection* conn = new Connection(this, clientFd); conn->setDisconnectionCallback(Server::cbDisconnection, this); this->addConnection(conn); conn->start();
} return 0; } void handleDisconnection(int clientFd) { LOGI("clientFd=%d", clientFd); this->removeConnection(clientFd); } static void cbDisconnection(void* arg, int clientFd) { LOGI("clientFd=%d", clientFd); Server* server = (Server*)arg; server->handleDisconnection(clientFd); }
private: const char* m_ip; uint16_t m_port; int m_sockFd;
std::map<int, Connection*> m_connMap; std::mutex m_connMap_mtx; bool addConnection(Connection* conn) { m_connMap_mtx.lock(); if (m_connMap.find(conn->getClientFd()) != m_connMap.end()) { m_connMap_mtx.unlock(); return false; } else { m_connMap.insert(std::make_pair(conn->getClientFd(), conn)); m_connMap_mtx.unlock(); return true; } } Connection* getConnection(int clientFd) { m_connMap_mtx.lock(); std::map<int, Connection*>::iterator it = m_connMap.find(clientFd); if ( it != m_connMap.end()) { m_connMap_mtx.unlock(); return it->second; } else { m_connMap_mtx.unlock(); return nullptr; } } bool removeConnection(int clientFd) { m_connMap_mtx.lock(); std::map<int, Connection*>::iterator it = m_connMap.find(clientFd); if (it != m_connMap.end()) { m_connMap.erase(it); m_connMap_mtx.unlock(); return true; } else { m_connMap_mtx.unlock(); return false; } } };
Server::Server(const char* ip, uint16_t port):m_ip(ip),m_port(port),m_sockFd(-1) { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { LOGE("WSAStartup Error"); return; }
} Server::~Server() { if (m_sockFd > -1) { closesocket(m_sockFd); m_sockFd = -1; } WSACleanup(); }
int main() { const char* ip = "127.0.0.1"; uint16_t port = 8080; Server server(ip,port); server.start(); return 0; }
|