ClientSocket* ServerSocket::Accept() { ClientSocket* client = AllocClient(); uv_tcp_init(getSocket()->loop, client->getSocket()); if(uv_accept((uv_stream_t*)getSocket(), (uv_stream_t*) client->getSocket()) == 0) { client->getSocket()->data = client; OnConnection(client); } else { OnError("Accepting Client"); client->Close(); } return client; }
int main(){ for(int i = 0; i < 10; i++){ ClientSocket tmp; tmp.Connect("127.0.0.1", 6666); std::string s = randomString(); tmp.WriteLine(s); s = ""; tmp.ReadLine(s); printf("Client : "); tmp.Close(); printf("%d", i); puts(""); } }
void ServerSocket::on_new_connection(uv_stream_t *server, int status) { ServerSocket* serSock = (ServerSocket*)server->data; if (status == -1) { // Error close the lobby cout << "connection error" << '\n'; serSock->OnConnectionError(); return; } cout << "connection" << '\n'; ClientSocket* client = serSock->Accept(); if (client!=NULL) { client->BeginRead(); } else { client->Close(); } }