void Server::run() { ENetEvent event; std::cout << "Actual port => " << _set.getCvar("sv_port") << std::endl; std::cout << "Quiet => " << _set.getCvar("sv_quiet") << std::endl; std::cout << "Debug => " << _set.getCvar("sv_debug") << std::endl; while ((enet_host_service(_server, &event, 50)) >= 0) { switch (event.type) { case ENET_EVENT_TYPE_CONNECT: connectClient(event.peer); break; case ENET_EVENT_TYPE_RECEIVE: handlePackets(event); break; case ENET_EVENT_TYPE_DISCONNECT: disconnectClient(event.peer); default: break; } updateClients(); } }
//连接客户端 void connect() { char ipBuff[15] = {0}; int Port = 0; int temp = 1; if(sockTCP != INVALID_SOCKET &&hAsyncSocketThread != NULL){ return ;} ConnectClient connectClient( &hAsyncSocketThread, &sockTCP); do { printf("Please enter the IP address and port, separated by Spaces:"); scanf("%s %d",ipBuff , &Port); if (inet_addr(ipBuff)==INADDR_NONE){ printf("Format is not correct, please enter again!\n"); if (temp > 2)return; } else break; temp++; } while (temp); if( connectClient.Connect(ipBuff , Port) ){ system("cls"); if (!connectClient.BeginReceive() ){ printf("Thread Error!\n"); } } }
int main(int argc, char **argv) { // Eg: /usr/local/bin/mTerm_client wedge if (argc != 2) { print_usage(); exit(1); } escHelp(); connectClient(argv[1]); return 0; }
void Server::incomingConnection(qintptr handle) { Socket *tempSock=new Socket(handle); QThread *tt=new QThread(tempSock); connect(tempSock,SIGNAL(readVariantMap(int,QString,quint16,const QVariantMap&)),this,SIGNAL(readVariantMap(int,QString,quint16,const QVariantMap&))); connect(tempSock,SIGNAL(sockDisconnect(const int,QString,quint16)),this,SLOT(sockDisconnectSlot(const int,QString,quint16))); connect(this,SIGNAL(sendVariantMap(const QVariantMap&,int)),tempSock,SIGNAL(sendVariantMap(const QVariantMap&,int))); connect(tempSock,SIGNAL(disconnected()),tt,SLOT(quit())); tempSock->moveToThread(tt); tt->start(); Clients->insert(handle,tempSock); emit connectClient(tempSock->socketDescriptor(),tempSock->peerAddress().toString(),tempSock->peerPort()); }
void MainWindow::connectEvents() { handler = new ctkEventDemo(); connect(ui->btnSend, SIGNAL(released()), this, SLOT(sendEvent())); connect(handler, SIGNAL(updateMessageSignal(QString)), this, SLOT(updateMessage(QString))); connect(ui->connectButton, SIGNAL(released()), this, SLOT(connectClient())); qDebug() << "connectEvents"; m_EventBus->publishSignal(handler, "receiveEventSignal(QVariantList)", "ctk/remote/eventBus/comunication/receive/xmlrpc"); ctkDictionary dic; dic.insert("event.topics","ctk/remote/eventBus/comunication/receive/xmlrpc"); m_EventBus->subscribeSlot(handler, "receiveEvent(QVariantList)", dic); }
int main(int argc, char** argv) { // Check if the port number was provided if(argc != 2) { printf("Proper usage: chatserver [port number]"); exit(1); } else { portNo = atoi(argv[1]); } // Set up the TCP socket serverSock = setSocket(&portNo); // Bind the socket bindSocket(); while(1) { // Establish a socket that listens for incoming reqests listenForReq(); // Accept a client request isConnected = connectClient(); // allocate memory to send and receive messages sendBuff = (char*) malloc(512); recBuff = (char*) malloc(512); quit = (char*) malloc(10); strcpy(quit, "\\quit\n"); getClientHandle(); while(1) { if(receiveMessage() == 0) { break; } if(sendMessage() == 0) { break; } } } close(serverSock); return 0; }
void MyTcpServer::incomingConnection(qintptr socketDescriptor) //多线程喜碧在此函数里捕获新连接 { myTcpSocket * tcpTemp = new myTcpSocket(socketDescriptor); connect(tcpTemp,&myTcpSocket::sockDisConnect,this,&MyTcpServer::sockDisConnectSlot);//NOTE:断开连接的处理,从列表移除,并释放断开的Tcpsocket,此槽必须实现,线程管理计数也是考的他 connect(this,&MyTcpServer::sentDisConnect,tcpTemp,&myTcpSocket::disConTcp);//断开信号 tcpTemp->moveToThread(ThreadHandle::getClass().getThread());//把tcp类移动到新的线程,从线程管理类中获取 tcpClient->insert(socketDescriptor,tcpTemp);//插入到连接信息中 emit connectClient(tcpTemp->socketDescriptor(),tcpTemp->peerAddress().toString(),tcpTemp->peerPort()); qDebug() << "Have New Connection! " << tcpTemp->peerAddress().toString() << ":" << tcpTemp->peerPort(); }
void OOClient::callShutdown() { if(!isConnected()) connectClient(); QTextStream out(&m_client); out << "save_and_exit" << endl; m_client.waitForBytesWritten(); m_client.disconnectFromHost(); m_client.waitForDisconnected(); }
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); QString str="Start Listen 19999"; ui->showWindow->setText(str); ser=new QTcpServer(this); if( ser->listen(QHostAddress::LocalHost,8000)) { qDebug()<<ser->errorString(); close(); } connect(ser,SIGNAL(newConnection()),this,SLOT(connectClient())); connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(sendMessage())); }
bool TCPServer::start(const quint16 &port) { if(server) return false; emit info(classname, "started at port " + QString::number(port)); server = new QTcpServer(this); bool result1 = QObject::connect(server, SIGNAL(newConnection()), this, SLOT(connectClient())); bool result2 = server->listen(QHostAddress::Any, port); if (!result1) emit error(classname, "TCP server could not connect newConnection"); if (!result2) emit error(classname, "TCP server failed to start listening"); return (result1 && result2); }
void MyTcpServer::incomingConnection(qintptr socketDescriptor) //多线程喜碧在此函数里捕获新连接 { myTcpSocket * tcpTemp = new myTcpSocket(socketDescriptor); //可以信号连接信号的,我要捕捉线程ID就独立出来函数了,使用中还是直接连接信号效率应该有优势 connect(tcpTemp,&myTcpSocket::readData,this,&MyTcpServer::readDataSlot);//接受到数据 connect(tcpTemp,&myTcpSocket::sockDisConnect,this,&MyTcpServer::sockDisConnectSlot);//NOTE:断开连接的处理,从列表移除,并释放断开的Tcpsocket,此槽必须实现,线程管理计数也是考的他 connect(this,&MyTcpServer::sentData,tcpTemp,&myTcpSocket::sentData);//发送数据 connect(this,&MyTcpServer::sentDisConnect,tcpTemp,&myTcpSocket::disConTcp);//断开信号 tcpTemp->moveToThread(ThreadHandle::getClass().getThread());//把tcp类移动到新的线程,从线程管理类中获取 tcpClient->insert(socketDescriptor,tcpTemp);//插入到连接信息中 qDebug() <<"incomingConnection THREAD IS:" <<QThread::currentThreadId(); //发送连接信号 emit connectClient(tcpTemp->socketDescriptor(),tcpTemp->peerAddress().toString(),tcpTemp->peerPort()); }
void TcpServer::incomingConnection(qintptr socketDescriptor) //多线程必须在此函数里捕获新连接. { if (tcpClient->size() > maxPendingConnections())//继承重写此函数后,QTcpServer默认的判断最大连接数失效,自己实现 { QTcpSocket tcp; tcp.setSocketDescriptor(socketDescriptor); tcp.disconnectFromHost(); return; } auto th = ThreadHandle::getClass().getThread(); auto tcpTemp = new TcpSocket(socketDescriptor); QString ip = tcpTemp->peerAddress().toString(); qint16 port = tcpTemp->peerPort(); connect(tcpTemp,&TcpSocket::sockDisConnect,this,&TcpServer::sockDisConnectSlot);//NOTE:断开连接的处理,从列表移除,并释放断开的Tcpsocket,此槽必须实现,线程管理计数也是考的他 connect(this,&TcpServer::sentDisConnect,tcpTemp,&TcpSocket::disConTcp);//断开信号 tcpTemp->moveToThread(th);//把tcp类移动到新的线程,从线程管理类中获取 tcpClient->insert(socketDescriptor,tcpTemp);//插入到连接信息中 emit connectClient(socketDescriptor,ip,port); }
/*The thread method, accepts a user struct pointer as an arguement that contains all the relevant info for the client side*/ void* thread_proc(void *arg) { int sock, option; char buffer[100]; user_t *user = (user_t *) arg; sock = user->sock; printf("client number %i connected.\n",user->id ); /*loop forever constantly accepting an option from the user and calling the relevant method*/ while(1) { read(sock, buffer, 100); option = atoi(buffer); switch(option) { case 1: connectClient(user); break; case 2: disconnectClient(user); break; case 3: depositMessage(user); break; case 4: retrieveMessages(user); break; case 5: checkUsers(user); break; default: printf("Invalid choice\n"); break; } }; return (void *)1; }
//クライアントの接続受付スレッド void* thread1( void* hs ) { int s , listen_s; u_short uport; int i=0; pid_t p_pid[100]; pthread_t th[100]; uport = setPort(); listen_s = listenStart( uport ); while( 1 ){ s = connectClient( listen_s ); //エラー処理 if( s < 0 ){ }else{ pthread_create( &th[i] , NULL , thread2 ,(void *)&s ); printf("threadID=%d\n" , th[i]); i++; } } }
void connectCB (GtkButton *button,gpointer user_data){connectClient();}
bool NetwalkWindow::newRandomMap() { int sdf = time(NULL); // cout << "rand: " << sdf << endl; srand ( sdf ); bool found = false; int x; int y; for( int i = 0; i < pNumCli; i++ ) { while( !found ){ x = rand() % pSizeX; y = rand() % pSizeY; if ( mapView.tiles(x, y).numObjects() == 1 ) { found = true; mapView.tiles(x, y).setNumObjects( 2 ); mapView.tiles(x, y).objectImage( 0 ) = -1; mapView.tiles(x, y).objectImage( 1 ) = 30; // cout << "Cli" << i << ": (" << x << ";" << y << ")" << endl; } } found = false; } while( !found ){ pServX = rand() % pSizeX; pServY = rand() % pSizeY; if ( mapView.tiles(pServX, pServY).numObjects() == 1 ) { found = true; mapView.tiles(pServX, pServY).setNumObjects( 2 ); mapView.tiles(pServX, pServY).objectImage( 0 ) = -1; mapView.tiles(pServX, pServY).objectImage( 1 ) = 28; // cout << "Server: (" << pServX << ";" << pServY << ")" << endl; } } bool complete; do { complete = true; for( y = 0; y < pSizeY; y++ ){ for( x = 0; x < pSizeY; x++ ){ if ( isClient(x, y) ) { if ( !clientIsConnected( x, y ) ) { // cout << "Conn Cli: (" << x << ";" << y << ")" << endl; if (!connectClient( x, y ) ) { // cout << "!!! Conn Cli: (" << x << ";" << y << ")" << endl; return false; /* debugMap("Failed"); mapView.tiles(x, y).setNumObjects( 1 ); mapView.tiles(x, y).objectImage( 0 ) = -1; removeDir(x+1,y,3); removeDir(x-1,y,1); removeDir(x,y+1,0); removeDir(x,y-1,2); complete = false; for( int y2 = 0; y2 < pSizeX; y2++ ){ for( int x2 = 0; x2 < pSizeX; x2++ ){ if ( ( mapView.tiles(x2, y2).objectImage( 0 ) > -1 ) && ( mapView.tiles(x2, y2).objectImage( 0 ) < 14 )) mapView.tiles(x2, y2).objectImage( 0 ) = -1; } }*/ } else { for( int y2 = 0; y2 < pSizeX; y2++ ){ for( int x2 = 0; x2 < pSizeX; x2++ ){ if ( ( mapView.tiles(x2, y2).objectImage( 0 ) > -1 ) && ( mapView.tiles(x2, y2).objectImage( 0 ) < 14 )) mapView.tiles(x2, y2).objectImage( 0 ) += 14; } } } } } } } if ( ! complete ) { found = false; while( !found ){ x = rand() % pSizeX; y = rand() % pSizeY; if ( mapView.tiles(x, y).numObjects() == 1 ) { found = true; mapView.tiles(x, y).setNumObjects( 2 ); mapView.tiles(x, y).objectImage( 0 ) = -1; mapView.tiles(x, y).objectImage( 1 ) = 30; } } } } while ( !complete ); for ( int i = 0 ; i < (pSizeX*pSizeY)/2; i++ ){ x = rand() % pSizeX; y = rand() % pSizeY; rotate(x,y,true); x = rand() % pSizeX; y = rand() % pSizeY; rotate(x,y,false); } checkFlow(); return true; }
int main(int argc, char *argv[]) { if (argc < 3) { printf("argument less"); exit(1); } //make and open fifo from argv[1] if (access(argv[1], F_OK) == -1) { if (mkfifo(argv[1], 0666) != 0) { perror("mkfio"); exit(1); } } int fd_sever = open(argv[1], O_RDONLY); if (fd_sever < 0) { perror("open fifo"); exit(1); } printf("open sever fifo\n"); List *client_list; initList(&client_list); Queue task_queue; initQueue(&task_queue); pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond_master, NULL); pthread_cond_init(&cond_slaver, NULL); //Create thread int index; int thread_num = atoi(argv[2]); pthread_t *th = (pthread_t *)calloc(thread_num, sizeof(pthread_t)); for (index = 0; index < thread_num; index++) { pthread_create(th + index, NULL, thread_handler, (void *)&task_queue); } fd_set set, back; FD_ZERO(&back); FD_SET(fd_sever, &back); struct timeval tm; tm.tv_sec = 10; tm.tv_usec = 0; int res; char buf[N]; while (1) { set = back; res = select(N, &set, NULL, NULL, &tm); if (res <= 0) { continue; } if (FD_ISSET(fd_sever, &set)) { memset(buf, 0, N); if (read(fd_sever, buf, N) != 0) { connectClient(buf, &client_list); } else { continue; } } traverseList(&client_list, &task_queue, &set, &back); } pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond_master); pthread_cond_destroy(&cond_slaver); close(fd_sever); unlink(argv[1]); return 0; }
bool NetwalkWindow::connectClient( const int x, const int y, int dir ) { if ( (x < 0) || ( x >= pSizeX) ) return false; if ( (y < 0) || ( y >= pSizeY) ) return false; bool triedUp = false; bool triedDown = false; bool triedRight = false; bool triedLeft = false; if ( dir == 0 ) triedDown = true; else if ( dir == 1 ) triedLeft = true; else if ( dir == 2 ) triedUp = true; else if ( dir == 3 ) triedRight = true; int r; while ( (!triedUp) || (!triedDown) || (!triedLeft) || (!triedRight) ) { r = rand() % 4; if ( (r == 0) && (!triedUp) ) { if ( (!isClient( x, y-1 )) && ( y-1 >= 0) ) { if ( ( mapView.tiles(x, y-1).objectImage( 0 ) == -1 ) || ( mapView.tiles(x, y-1).objectImage( 0 ) > 13 ) ) { if ( ( addDir(x, y, r) ) && ( addDir(x, y-1, (r + 2) % 4) ) ) { if ( mapView.tiles(x, y-1).objectImage( 0 ) > 13 ) { //mapView.tiles(x, y).objectImage( 0 ) += 14; return true; } else { if ( connectClient( x, y-1, r) ) { //mapView.tiles(x, y).objectImage( 0 ) += 14; return true; } } } removeDir(x, y, r); removeDir(x, y-1, (r + 2) % 4); } } triedUp = true; } else if ( (r == 1) && (!triedRight) ) { if ( (!isClient( x+1, y )) && ( x+1 < pSizeX) ) { if ( ( mapView.tiles(x+1, y).objectImage( 0 ) == -1 ) || ( mapView.tiles(x+1, y).objectImage( 0 ) > 13 ) ) { if ( ( addDir(x, y, r) ) && ( addDir(x+1, y, (r + 2) % 4) ) ) { if ( mapView.tiles(x+1, y).objectImage( 0 ) > 13 ) { // mapView.tiles(x, y).objectImage( 0 ) += 14; return true; } else { if ( connectClient(x+1, y, r) ) { // mapView.tiles(x, y).objectImage( 0 ) += 14; return true; } } } removeDir(x, y, r); removeDir(x+1, y, (r + 2) % 4); } } triedRight = true; } else if ( (r == 2) && (!triedDown) ) { if ( (!isClient( x, y+1 )) && ( y+1 < pSizeY) ) { if ( ( mapView.tiles(x, y+1).objectImage( 0 ) == -1 ) || ( mapView.tiles(x, y+1).objectImage( 0 ) > 13 ) ) { if ( ( addDir(x, y, r) ) && ( addDir(x, y+1, (r + 2) % 4) ) ) { if ( mapView.tiles(x, y+1).objectImage( 0 ) > 13 ) { // mapView.tiles(x, y).objectImage( 0 ) += 14; return true; } else { if ( connectClient(x, y+1, r) ) { // mapView.tiles(x, y).objectImage( 0 ) += 14; return true; } } } removeDir(x, y, r); removeDir(x, y+1, (r + 2) % 4); } } triedDown = true; } else if ( (r == 3) && (!triedLeft) ) { if ( (!isClient( x-1, y )) && ( x-1 >= 0) ) { if ( ( mapView.tiles(x-1, y).objectImage( 0 ) == -1 ) || ( mapView.tiles(x-1, y).objectImage( 0 ) > 13 ) ) { if ( ( addDir(x, y, r) ) && ( addDir(x-1, y, (r + 2) % 4) ) ) { if ( mapView.tiles(x-1, y).objectImage( 0 ) > 13 ) { // mapView.tiles(x, y).objectImage( 0 ) += 14; return true; } else { if ( connectClient(x-1, y, r) ) { // mapView.tiles(x, y).objectImage( 0 ) += 14; return true; } } } removeDir(x, y, r); removeDir(x-1, y, (r + 2) % 4); } } triedLeft = true; } } return false; }
virtual void SetUp() { startServer(); connectClient(); }
/* argv[1] is the relative or absolute file name */ int main(int argc,char *argv[]){ FileTransferServer server; connectClient(&server,argv[1]); }