static int server(void){ int ret=0; struct socket* _clientSocket; struct socket * _socket = createServerSocket(); if(!_socket) { printk("server socket created failed\n"); return -1; } while (1){ /*listen*/ ret=_socket->ops->listen(_socket,10); if(ret<0){ printk("server: listen error\n"); return ret; } printk("server:listen ok!\n"); ret=_socket->ops->accept(_socket,_clientSocket,10); if(ret<0){ printk("server:accept error!\n"); return ret; } printk("server: accept ok, Connection Established\n"); processConnection(_clientSocket); } sock_release(_socket); }
void nrf5UartProcess(void) { while (app_usbd_event_queue_process()) { } processConnection(); processReceive(); processTransmit(); }
void process() { const int MAX_EVENTS = 128; epoll_event events[MAX_EVENTS]; int nfds = 0; for (;;) { nfds = epoll_wait(mEpollFd, events, MAX_EVENTS, -1); for (int n = 0; n < nfds; ++n) processConnection(events[n].data.fd); } }
Ref<System> XMLLoader::load(std::istream& is) { // Context + load document LoadContext context; pugi::xml_parse_result result = context.doc.load(is); if (!result) { SPK_LOG_ERROR("XMLLoader::load(std::istream&) - Error while parsing XML : " << result.description() << " at character " << result.offset); return SPK_NULL_REF; } context.root = context.doc.document_element(); if (!context.root || std::string("SPARK") != context.root.name()) { SPK_LOG_ERROR("XMLLoader::load(std::istream&) - The root element is not conform (must be <SPARK>)"); return SPK_NULL_REF; } // Process nodes for(pugi::xml_node node = context.root.first_child(); node; node = node.next_sibling()) { if(node.name() == std::string("connect")) context.connections.push_back(node); else processNode(node, context); } // Process objects for(unsigned int t = 0; t < context.objects.size(); t++) { if(context.objects[t].obj) { XMLDeserializer xml(context.objects[t].node, context); context.objects[t].obj->getDescription().deserialize(xml); } } // Process connections for(unsigned int t = 0; t < context.connections.size(); t++) processConnection(t, context); // Return system if(!context.systemRef) { SPK_LOG_ERROR("XMLLoader::load(std::istream&) - No system found"); return SPK_NULL_REF; } return context.getObject<System>(context.systemRef); }
void server(int port) { int listenSocket = 0; int socket = 0; int processId = 0; char clientIp[16]; unsigned short *clientPort = NULL; // Set up the server initializeServer(&listenSocket, &port); // Loop to monitor the server socket while (1) { clientPort = (unsigned short*)malloc(sizeof(unsigned short)); // Block here and wait for new connections if ((socket = acceptConnectionIpPort(&listenSocket, clientIp, clientPort)) == -1) { systemFatal("Can't Accept Client"); } // Spawn process to deal with client processId = fork(); if (processId == 0) { close(listenSocket); // Process the child connection processConnection(socket, clientIp, (int)*clientPort); // Once we are done, exit free(clientPort); return; } else if (processId > 0) { // Since I am the parent, keep on going close(socket); free(clientPort); continue; } else { // Fork failed, should shut down as this is a serious issue systemFatal("Fork Failed To Create Child To Deal With Client"); } } printf("Server Closing!\n"); }
void Summoners::on_btn_login_clicked() { quint32 port = ui.edit_port->text().toUInt(); QHostAddress addr(ui.edit_addr->text()); Socket = new QTcpSocket; Socket->connectToHost(addr, port); connect(Socket, SIGNAL(connected()), this, SLOT(processConnection())); connect(Socket, SIGNAL(readyRead()), this, SLOT(receiveMessage())); timer_heart = startTimer(1000); //假设每秒1次心跳。不同的游戏,心跳间隔不一样。 QByteArray datagram; QDataStream iStream(&datagram, QIODevice::WriteOnly); iStream.setByteOrder(QDataStream::LittleEndian); iStream << CS_Login; Socket->write(datagram); }
CollabServer::CollabServer(const QString &nick, const QString &filename, quint16 port, QObject *parent) : Collaborate(nick, parent) , port(port) , readySignal(0) , disconnectSignal(0) { this->filename = filename; readySignal = new QSignalMapper(this); connect(readySignal, SIGNAL(mapped(int)), this, SLOT(readyRead(int))); disconnectSignal = new QSignalMapper(this); connect(disconnectSignal, SIGNAL(mapped(int)), this, SLOT(disconnected(int))); connect(&server, SIGNAL(newConnection()), this, SLOT(processConnection())); if (!server.listen(QHostAddress::Any, port)) emit error(server.serverError()); qDebug() << "Collaborate-server: created server"; }
// processConnection with a default buffer void WebServer::processConnection() { char request[WEBDUINO_DEFAULT_REQUEST_LENGTH]; int request_len = WEBDUINO_DEFAULT_REQUEST_LENGTH; processConnection(request, &request_len); }
void do_httpd_core(){ static int socketsInit = 0; if( ! socketsInit ){ if( strcmp("username:password", (const char*)auth_token_raw) == 0 ){ XPLMSpeakString("x-httpd; This is the first time you have used x-httpd, please change the password."); //dialog_ChangePassword(); } printf("Running sockets init...\n"); initSockets(); socketsInit = 1; } int c = 0; int clientCount = 0; do{ clientCount++; if( clientCount >= 10 ){ break; } //we want to give x-plane priority, 10 clients per frame event. struct sockaddr_in from; memset( &from, 0, sizeof( sockaddr_in )); socklen_t len; //printf( "foo\n"); c = accept( sock, (sockaddr*)&from, &len ); switch( c ){ case EBADF: printf("x-httpd error: Bad file descriptor.\n"); break; case ENOTSOCK: printf("x-httpd error: accept() error: Not a socket.\n"); break; case EOPNOTSUPP: printf("x-httpd error: accept() error: Socket is not a STREAM.\n"); break; case EFAULT: printf("x-httpd error: accept() error: Address parameter fault.\n"); break; case EWOULDBLOCK: printf( "x-httpd error: accept() error: Would block.\n" ); break; case EMFILE: printf( "x-httpd error: accept() error: Process file table is full.\n" ); break; case ENFILE: printf( "x-httpd error: accept() error: Would block.\n" ); break; case -1: break; default: //printf("accept()ed a connect: %i\n", c); char remoteAddress[32]; strcpy( remoteAddress, inet_ntoa(from.sin_addr) ); if( (strcmp( "127.0.0.1", remoteAddress ) == 0) || bAllowRemoteConnections ){ //ntohl( &(from.sin_addr) ); if( bLogDebugToConsole ){ printf( "*** Connection from: %s\n", remoteAddress ); } processConnection(c); }else{ close(c); } break; } }while( c != -1 ); }