bool JackWinNamedPipeServerChannel::Execute() { if (!ClientListen()) { return false; } return ClientAccept(); }
int JackWinNamedPipeServerChannel::Open(const char* server_name, JackServer* server) { jack_log("JackWinNamedPipeServerChannel::Open"); snprintf(fServerName, sizeof(fServerName), server_name); // Needed for internal connection from JackWinNamedPipeServerNotifyChannel object if (ClientListen()) { fServer = server; return 0; } else { jack_error("JackWinNamedPipeServerChannel::Open : cannot create result listen pipe"); return -1; } }
void MainWindow::on_openIncoming_clicked() { if (ClientReceiveSetup() == 0) { QFile *file = new QFile(QFileDialog::getSaveFileName(this, tr("Save song as"), 0, tr("Music (*.wav)"))); if (file->fileName() != NULL) { ui->openIncoming->setEnabled(false); ui->disconnectIncoming->setEnabled(true); file->open(QIODevice::WriteOnly); ClientListen((HANDLE) _get_osfhandle(file->handle())); } else { ClientCleanup(); } } }
DWORD WINAPI ClientControlThreadSend(LPVOID lpParameter) { char *sendbuff = (char *)calloc(CONTROL_PACKET_SIZE + 1, sizeof(char)); char *recvbuff = (char *)calloc(CONTROL_PACKET_SIZE + 1, sizeof(char)); char *message = (char *)calloc(CONTROL_PACKET_SIZE + 1, sizeof(char)); int flag = (int)lpParameter; int sentb = 0, recvb = 1, totalb = 0, i = 0; wchar_t path[260]; switch (flag) { case GET_UPDATE_SONG_LIST: memset(recvbuff, 0, sizeof(recvbuff)); memset(message, 0, sizeof(message)); sendbuff[0] = flag; sentb = send(controlSock, sendbuff, CONTROL_PACKET_SIZE, 0); numSongs = 0; songRequestDone = 0; while(recvb != SOCKET_ERROR) { recvb = recv(controlSock, recvbuff, CONTROL_PACKET_SIZE, 0); strcat(message, recvbuff); totalb += recvb; if (totalb >= CONTROL_PACKET_SIZE) { if (message[0] == 0) { break; } songList[i] = new char[100]; strcpy(songList[i], message); i++; numSongs++; totalb -= CONTROL_PACKET_SIZE; memset(message, 0, sizeof(message)); } } songRequestDone = 1; break; case SEND_SONG_TO_SERVER: sendbuff[0] = flag; strcat(sendbuff, sendFileName); sentb = send(controlSock, sendbuff, CONTROL_PACKET_SIZE, 0); while(totalb < CONTROL_PACKET_SIZE && recvb != SOCKET_ERROR) { recvb = recv(controlSock, recvbuff, CONTROL_PACKET_SIZE, 0); totalb += recvb; } if (recvbuff[0] == 0) { sendSockClosed = ClientSendSetup(address, sendSock, SERVER_DEFAULT_PORT); ClientSend(hSendFile); } break; case GET_SONG_FROM_SERVER: sendbuff[0] = flag; strcat(sendbuff, recvFileName); mbstowcs(&path[0], recvFileName, strlen(recvFileName)); qDebug() << recvFileName; qDebug() << QString::fromWCharArray(path); DeleteFile(path); hReceiveFile = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); ShowLastErr(false); hReceiveClosed = 1; ClientReceiveSetup(listenSock, CLIENT_DEFAULT_PORT, acceptEvent); ClientListen(); sentb = send(controlSock, sendbuff, CONTROL_PACKET_SIZE, 0); break; default: qDebug() << "Invalid request"; break; } free(sendbuff); free(recvbuff); free(message); return TRUE; }
int ServerListen(struct server* st_server) { int32_t client_socket = -1; socklen_t client_addlen = sizeof(socklen_t); struct sockaddr client_addr; struct client* st_newclient; printf("Starting the server...\n"); /* Allocate the clients list. */ st_server->st_clients = VectorAllocate(100); if(st_server->st_clients == NULL) { SetError(STATUS_ERR_NOMEM); /* TODO: write an error message. */ return FAILED; } /* Allocate the files list. */ st_server->st_files = VectorAllocate(10); if(st_server->st_files == NULL) { SetError(STATUS_ERR_NOMEM); /* TODO: write an error message. */ return FAILED; } /* Loop indefinitely in order to establish a connection with clients. */ st_server->listening = TRUE; while(st_server->listening) { st_newclient = NULL; /* Wait for a client to connect. */ client_socket = accept(st_server->socket, &client_addr, &client_addlen); if(client_socket < 0) { /* If a bad file descriptor happens, it means the server was terminated. */ if(errno == EBADF) break; else goto ERROR; } /* Allocate the memory for the structure. */ st_newclient = ClientNew(st_server, client_socket, (struct sockaddr_in*)&client_addr); if(st_newclient == NULL) goto ERROR; /* Add the client into the list. */ if(VectorPushPointer(st_server->st_clients, st_newclient) == FAILED) { /* TODO: write an error message. */ goto ERROR; } /* Start listening to commands from the client. */ ClientListen(st_newclient); } return SUCCESS; ERROR: printf("An error happened.\n"); printf("Terminating the server...\n"); st_server->listening = FALSE; if(st_newclient != NULL) ClientDelete(st_newclient); ServerFreeSafe(st_server); return FAILED; }