IncomingConnection* Server::Accept() { if (_terminated) throw AcceptException(); struct sockaddr_in *client_in = new struct sockaddr_in; struct sockaddr *clientptr = (struct sockaddr *) client_in; static socklen_t clientlen = sizeof(struct sockaddr_in); static int newsockfd = 0; bzero(client_in->sin_zero, 8 * sizeof(unsigned char)); if ((newsockfd = accept(sockfd, clientptr, &clientlen)) < 0) throw AcceptException(); Rebind(); return new IncomingConnection(newsockfd, client_in); }
void ScreenshotServer::process(SOCKET socket) { const int BUFFER_SIZE = 512; int result = 0; std::vector<char> buffer(BUFFER_SIZE); do { result = recv(socket, &buffer[0], buffer.size(), 0); if (result > 0) { log("Recieved " + std::to_string(result) + " bytes from " + std::to_string(socket)); assert(result == sizeof(int)); unsigned int recieved = *(&buffer[0]); log("As " + std::to_string(recieved)); if (recieved >= actions_.size()) { log("Bad number recieved: " + std::to_string(recieved) + " >= " + std::to_string(actions_.size())); continue; } log("Making action: " + actions_[recieved]); result = shutdown(socket, SD_RECEIVE); if (result == SOCKET_ERROR) { log("Could not shutdown socket connection: " + std::to_string(result)); closesocket(socket); WSACleanup(); throw AcceptException("Shutdown failed!"); } } else if (result == 0) { log("Closing connection with: " + std::to_string(socket)); } else { closesocket(socket); WSACleanup(); throw AcceptException("Recv failed on: " + std::to_string(socket) + "\n"); } } while (result > 0); }
void ScreenshotServer::accept() { SOCKET socket = listen(); while (true) { SOCKET clientSocket = INVALID_SOCKET; clientSocket = ::accept(socket, NULL, NULL); if (clientSocket == INVALID_SOCKET) { closesocket(clientSocket); WSACleanup(); throw AcceptException("Could not accept on client socket: " + std::to_string(WSAGetLastError())); } process(clientSocket); } }
TCPSocket *TCPSocket::accept() { if (this->_state != LISTENING) { throw SocketStateException("Wrong state for operation"); } struct sockaddr addr; socklen_t len = sizeof (addr); int n_sock = ::accept(this->_b->_sock, &addr, &len); #ifdef DEBUG fprintf(stderr, "socket %d accepted %d\n", this->_b->_sock, n_sock); #endif if (n_sock == -1) { if (errno == EAGAIN) throw EAGAINException(); else throw AcceptException(errno); } TCPSocket *s = new TCPSocket(n_sock, &addr); s->_state = CONNECTED; return s; }