FileBrowser::FileBrowser(QWidget *parent) { Q_UNUSED(parent); this->CreateDialogLayout(); connect(lstDir, SIGNAL(itemDoubleClicked()), this, SLOT(onLstDirDoubleClick())); connect(lstFile, SIGNAL(itemDoubleClicked()), this, SLOT(onLstFileDoubleClick())); connect(btnCancel, SIGNAL(pressed()), this, SLOT(onCancelPressed())); connect(txtFilename, SIGNAL(textEdited(const QString &)), this, SLOT(onFilenameChanged())); connect(txtFilename, SIGNAL(returnPressed()), this, SLOT(onAcceptPressed())); connect(cmbFilter, SIGNAL(activated(int)), this, SLOT(onFilterActivated(int))); connect(btnHome, SIGNAL(pressed()), this, SLOT(onHomePressed())); connect(btnMachine, SIGNAL(pressed()), this, SLOT(onMachinePressed())); connect(btnRemote, SIGNAL(pressed()), this, SLOT(onRemotePressed())); connect(btnAccessGrid, SIGNAL(pressed()), this, SLOT(onAccessGridPressed())); connect(cmbDirectory, SIGNAL(activated(int)), this, SLOT(onDirActivated(int))); connect(btnDirUp, SIGNAL(pressed()), this, SLOT(onDirUpPressed())); connect(cmbHistory, SIGNAL(activated(int)), this, SLOT(onHistoryActivated(int))); connect(btnAccept, SIGNAL(pressed()), this, SLOT(onAcceptPressed())); connect(this, SIGNAL(requestLists(QString, QString)), this->mTUIElement, SLOT(handleRequestLists(QString, QString))); connect(this, SIGNAL(filterChange(QString)), this->mTUIElement, SLOT(handleFilterUpdate(QString))); connect(this, SIGNAL(dirChange(QString)), this->mTUIElement, SLOT(handleDirChange(QString))); connect(this, SIGNAL(requestClients()), this->mTUIElement, SLOT(handleClientRequest())); connect(this, SIGNAL(locationChanged(QString)), this->mTUIElement, SLOT(handleLocationChange(QString))); connect(this, SIGNAL(requestLocalHome()), this->mTUIElement, SLOT(handleLocalHome())); connect(this, SIGNAL(reqDriveList()), this->mTUIElement, SLOT(handleReqDriveList())); connect(this, SIGNAL(fileSelected(QString, QString, bool)), this->mTUIElement, SLOT(sendSelectedFile(QString, QString, bool))); this->mLocationPath = ""; this->mFilename = new QStringList(); mRCDialog = new Ui_RemoteClients(); mRCDialog->setupUi(mRCDialog); this->mMode = FileBrowser::OPEN; }
static int handleClient(int handle, void *context) { int rc; ClientRef client = (ClientRef) context; rc = handleClientRequest(client); if (rc <= 0) { disconnectClient(client); } return rc; }
/*Thread Main Function Variable Definition: -- thread_arguments: arguments which thread should be used Return Value: NULL */ void *threadMain(void *thread_arguments){ int client_socket; //socket descriptor for client //Guarantees that thread resources are deallocated upon return pthread_detach(pthread_self()); //Pass the arguments client_socket = ((THREAD_ARGUMENTS*)thread_arguments)->client_socket; //Deallocate memory for argument free(thread_arguments); //Handle the client request handleClientRequest(client_socket); return (NULL); }
FileBrowser::FileBrowser(TUIFileBrowserButton *instance, QWidget *parent, int id) { Q_UNUSED(parent); this->mTUIElement = instance; this->CreateDialogLayout(); connect(lstDir, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(onLstDirDoubleClick(QListWidgetItem *))); connect(lstFile, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(onLstFileDoubleClick(QListWidgetItem *))); connect(lstFile, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(onLstFileClick(QListWidgetItem *))); connect(btnCancel, SIGNAL(pressed()), this, SLOT(onCancelPressed())); connect(txtFilename, SIGNAL(textEdited(QString)), this, SLOT(onFilenameChanged(QString))); connect(txtFilename, SIGNAL(returnPressed()), this, SLOT(onAcceptPressed())); connect(this, SIGNAL(fileNameEntered()), this, SLOT(onAcceptPressed())); connect(cmbFilter, SIGNAL(activated(int)), this, SLOT(onFilterActivated(int))); connect(btnHome, SIGNAL(pressed()), this, SLOT(onHomePressed())); connect(btnMachine, SIGNAL(pressed()), this, SLOT(onMachinePressed())); connect(btnRemote, SIGNAL(pressed()), this, SLOT(onRemotePressed())); connect(btnAccessGrid, SIGNAL(pressed()), this, SLOT(onAccessGridPressed())); connect(cmbDirectory, SIGNAL(activated(int)), this, SLOT(onDirActivated(int))); connect(btnDirUp, SIGNAL(pressed()), this, SLOT(onDirUpPressed())); connect(cmbHistory, SIGNAL(activated(int)), this, SLOT(onHistoryActivated(int))); connect(btnAccept, SIGNAL(pressed()), this, SLOT(onAcceptPressed())); connect(this, SIGNAL(requestLists(QString, QString)), this->mTUIElement, SLOT(handleRequestLists(QString, QString))); connect(this, SIGNAL(filterChange(QString)), this->mTUIElement, SLOT(handleFilterUpdate(QString))); connect(this, SIGNAL(dirChange(QString)), this->mTUIElement, SLOT(handleDirChange(QString))); connect(this, SIGNAL(requestClients()), this->mTUIElement, SLOT(handleClientRequest())); connect(this, SIGNAL(locationChanged(QString)), this->mTUIElement, SLOT(handleLocationChange(QString))); connect(this, SIGNAL(requestLocalHome()), this->mTUIElement, SLOT(handleLocalHome())); connect(this, SIGNAL(reqDriveList()), this->mTUIElement, SLOT(handleReqDriveList())); connect(this, SIGNAL(fileSelected(QString, QString, bool)), this->mTUIElement, SLOT(sendSelectedFile(QString, QString, bool))); this->mLocationPath = ""; this->mId = id; this->mFilename = new QStringList(); mRCDialog = new Ui_RemoteClients(); mRCDialog->setupUi(mRCDialog); this->mMode = FileBrowser::OPEN; QObject::connect(this->mRCDialog->buttonBox, SIGNAL(accepted()), this, SLOT(remoteClientsAccept())); QObject::connect(this->mRCDialog->buttonBox, SIGNAL(rejected()), this, SLOT(remoteClientsReject())); QObject::connect(this->mRCDialog->listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(handleItemClicked(QListWidgetItem *))); emit dirChange(this->mLocationPath); // std::string tmp = this->mLocationPath.toStdString(); //std::cerr << "FileBrowser: Path: " << tmp.c_str() << std::endl; }
int main(int argc, char **argv) { int sockfd; /* socket */ int clientfd; /* client */ int portno; /* port to listen on */ //int clientlen; /* byte size of client's address */ struct sockaddr_in serveraddr; /* server's addr */ int optval; /* flag value for setsockopt */ fd_set master; fd_set read_fds; int i; int fdmax; struct hostent* hret; /* * check command line arguments */ if (argc != 3) { fprintf(stderr, "usage: %s <IP> <port>\n", argv[0]); exit(1); } portno = atoi(argv[2]); /* * socket: create the parent socket */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) error("ERROR opening socket"); printf("Socket opened successfully"); fflush(stdout); /* setsockopt: Handy debugging trick that lets * us rerun the server immediately after we kill it; * otherwise we have to wait about 20 secs. * Eliminates "ERROR on binding: Address already in use" error. */ optval = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,(const void *)&optval , sizeof(int)); /* * build the server's Internet address */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; hret = gethostbyname(argv[1]); memcpy(&serveraddr.sin_addr.s_addr, hret->h_addr,hret->h_length); serveraddr.sin_port = htons((unsigned short)portno); /* * bind: associate the parent socket with a port */ if (bind(sockfd, (struct sockaddr *) &serveraddr,sizeof(serveraddr)) < 0) error("ERROR on binding"); else printf(" Bind successfully completed \n"); FD_SET(sockfd, &master);//add the server socker address in the master file descriptor set fdmax =sockfd;// set the maximum file descriptor initially to server sockfd /* * main loop: wait for a datagram, then echo it */ /* Wait for 2 seconds for ACK to arrive*/ int selectVal; while (1) { read_fds = master;//set the local file descriptor read_fds to master file descriptor file //select is used for Multiple I/O if((selectVal=select(fdmax+1, &read_fds, NULL, NULL, NULL)) == -1) { perror("select"); exit(4); } /* else if(selectVal==0) { printf("Timeout occurred"); blocknum=blocknum-1; fseek(fp,-1*result,SEEK_CUR); senddatapacket(&fp, blocknum, sendbuf, sockfd, clientaddr, clientlen ); }*/ for(i=0 ; i<=fdmax ; i++) { if(FD_ISSET(i, &read_fds)) { if(i == sockfd) {//new client connections clientfd=handleClientRequest(i); FD_SET(clientfd,&master); if(clientfd>fdmax) { fdmax=clientfd; } }//i==sockfd else { clientfd=handleClientRequest(i); } }//if FD_ISSET }//for loop }//while 1 }
/*This is the function on which server wait for new client connection. */ int ISocketCommunication::Socket_Process_Clients(void) { pid_t ichild_process_id; struct sockaddr_storage client_address; socklen_t addrlen; char addrStr[ADDRSTRLEN] = {0}, host[NI_MAXHOST] = {0}, service[NI_MAXSERV] = {0}; std::map<pid_t,int>::iterator it; int client_sock_fd = 0; if(sock_fd <= 0) { return -1; /* socket has not been initialized, hence return with error.. */ } /* This chat server is create one child process for each client request. All the child process are grouped under parent process. Hence parent and all child proecss are treated as one job in Linux/Unix OS If parent is terminated due to any signal (e.g. ctrl c), OS will kill, all the child processes. */ setpgid(getpid(), getpid()); addrlen = sizeof(struct sockaddr_storage); for (;;) { memset(&client_address, 0, sizeof(struct sockaddr_storage) ); /* Wait for connection from client */ client_sock_fd = accept(sock_fd, (struct sockaddr *) &client_address, &addrlen) ; if(client_sock_fd == -1 || errno) { if(errno == EINTR) { continue; } else { //if(errno == EBADF || errno == EINVAL) errno = ENOTCONN; break; } } if (getnameinfo((struct sockaddr *) &client_address, addrlen, host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) snprintf(addrStr, ADDRSTRLEN, "(%s, %s)", host, service); else snprintf(addrStr, ADDRSTRLEN, "(?UNKNOWN?)"); if(m_iCntClientConnections == socketCommunication_namespace::MAX_CLIENT_CONNECTION) { std::cout << std::endl << "Client Connection from : " << addrStr << " is refused, as maximum supported client is reached. " << std::endl; close(client_sock_fd); /* As maximum supported clients is reached, no more new client connection are allowed.. */ continue; } /* Handle each client request in a new child process */ switch (ichild_process_id = fork() ) { case -1: perror("\n Fork creation : "); close(client_sock_fd); /* Give up on this client */ break;/* May be temporary; try next client */ case 0: /* Child */ std::cout << std::endl << "Client Connection from : " << addrStr << std::endl; close(sock_fd); /* Unneeded copy of listening socket */ sock_fd = client_sock_fd; handleClientRequest(); /*two threads(for (Rx/Tx)) created for each client connection, This has a design of one process for each client connection, internally for each client process, two new thread are created, if client connection is lost for any reason, these two thread have to be cancelled, so that process can be closed and release its memory.. */ if(m_bThreadCreated) { thread_cancel(0); thread_cancel(1); } break; default: /* Parent */ char buf[128] = {0}; m_pSemaphore->sem_Wait(); m_childProcessMap.insert( std::make_pair(ichild_process_id, client_sock_fd) ); m_iCntClientConnections++; m_psharedMem->sharedMem_addClient(ichild_process_id); m_pSemaphore->sem_Post(); sprintf(buf, "Child is created : %d", ichild_process_id); m_pLogger->log_write(buf); /* If client requires, client can be informed events like new client connection established etc... */ //if(m_pChatEvtCmd) //m_pChatEvtCmd->executeChatEvtCmd(chatevtcmd_namespace::eNewClientCreated); close(client_sock_fd); /* Unneeded copy of connected socket */ break; /* Loop to accept next connection */ } /*End of switch case */ } /*End of for loop */ return 0; }