void PeerInfo::setStatus( PeerInfo::Status status ) { d_func()->status = status; if ( status == Online ) { announce(); } else if ( status == Offline && controlConnection() ) { controlConnection()->removePeerInfo( weakRef().toStrongRef() ); } // we need this to update the DiagnosticsDialog on new peers // if we ever happen to have a central PeerInfo manager object // we better add it there, but so far this would be the only // usage emit sipPlugin()->peerStatusChanged( weakRef().toStrongRef() ); }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowClass) { ui->setupUi(this); socket = new JagunKET(this); connected = false; connect(ui->btnConexao,SIGNAL(clicked()),this,SLOT(controlConnection())); connect(socket,SIGNAL(connected()),this,SLOT(connectedServer())); connect(socket,SIGNAL(disconnected()),this,SLOT(disconnectServer())); connect(socket,SIGNAL(sentData()),this,SLOT(confirmSend())); connect(ui->btnEnviar,SIGNAL(clicked()),this,SLOT(sendData())); }
/****************************************************************************** ** startServer() ** Description: A function that listens on inputed port, and interacts with ** one client user, and remains open after the user connection terminates for ** more client users. Used in main function. ** Parameters: port that server user enters ** Output: none ******************************************************************************/ void startServer(int port){ // endpoint socket that receives requests int serverSocket; int status; // set up signal for handling ctrl c exit struct sigaction interrupt; // get server address struct sockaddr_in serverAddress; // specify what type of socket addressing used (IPv4) serverAddress.sin_family = AF_INET; // convert address host to network short serverAddress.sin_port = htons(port); // bind socket to INNADD_ANY (all available interfaces, not just localhost) // source: http://stackoverflow.com/questions/16508685/understanding-inaddr-any-for-socket-programming-c serverAddress.sin_addr.s_addr = INADDR_ANY; // set the server side socket serverSocket = socket(AF_INET, SOCK_STREAM, 0); if (serverSocket == -1) { perror("socket"); exit(1); } // bind the new server socket with the port status = bind(serverSocket, (struct sockaddr*) &serverAddress, sizeof(serverAddress)); if (status == -1) { perror("bind"); exit(1); } // listen for client user connections, up to 10 status = listen(serverSocket, 10); if (status == -1) { perror("listen"); exit(1); } // ready stopServer() to catch any interrupt signals interrupt.sa_handler = &stopServer; interrupt.sa_flags = 0; sigemptyset(&interrupt.sa_mask); status = sigaction(SIGINT, &interrupt, 0); if (status == -1) { perror("sigaction"); exit(1); } // start running on port, waiting for client user connections (data or control) printf("ftserver: Server open on port %d\n", port); while (1) { // variable for printing client IP (in decimal form) char *clientIPv4; // variables for command tags and filename buffers char userCommand[TAG_LENGTH + 1]; char filename[PAYLOAD_LENGTH + 1]; // server side socket endpoints int controlSocket, dataSocket; // client data port number int dataPort; // address struct length // http://pubs.opengroup.org/onlinepubs/7908799/xns/syssocket.h.html socklen_t addrLen; struct sockaddr_in clientAddress; // start file transfer connection with client addrLen = sizeof(struct sockaddr_in); controlSocket = accept(serverSocket, (struct sockaddr *) &clientAddress, &addrLen); if (controlSocket == -1) { perror("accept"); exit(1); } // clientIPv4 = inet_ntoa(clientAddress.sin_addr); printf("\nftserver: Control connection established with \"%s\"\n", clientIPv4); // controlConnection() to send/receive packets with client user requests status = controlConnection(controlSocket, userCommand, &dataPort, filename); // if packets understood, proceed to the data connection (where fileList or files are sent) if (status != -1) { int connectionAttempts; // set server side endpoint for data connection dataSocket = socket(AF_INET, SOCK_STREAM, 0); if (dataSocket == -1) { perror("socket"); exit(1); } // start data connection with client user clientAddress.sin_port = htons(dataPort); connectionAttempts = 0; do { status = connect(dataSocket, (struct sockaddr *) &clientAddress, sizeof(clientAddress)); // max number of connection attempts is set at 10 } while (status == -1 && connectionAttempts < 10); if (status == -1) { perror("connect"); exit(1); } printf("ftserver: Data connection established with \"%s\"\n", clientIPv4); // call dataConnection to transfer file list or files to client user dataConnection(controlSocket, dataSocket, userCommand, filename); // wait to receive packet from user that the trasfer or listing is DONE (in tag) receivePacket(controlSocket, NULL, NULL); // close the data connection, client can reconnect for more interactions status = close(dataSocket); if (status == -1) { perror("close"); exit(1); } printf("ftserver: Data connection closed\n"); } } }