void Network::Initialize() { WSADATA wsa; if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { printf("Failed. Error Code : %d", WSAGetLastError()); exit(EXIT_FAILURE); } if ((socketThis = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR) { printf("Could not create socket : %d", WSAGetLastError()); exit(EXIT_FAILURE); } threadsRunning = true; memset((char *)&localConnection.socketAddr, 0, sizeof(localConnection.socketAddr)); localConnection.socketAddr.sin_family = AF_INET; localConnection.socketAddr.sin_addr.s_addr = INADDR_ANY; if (server) { InitializeServer(); } else { InitializeClient(); } InitializeThreads(); }
const BOOL CBonDriver_TSTask::OpenTuner() { TSTask::CBlockLock Lock(m_Lock); if (!m_Buffer.Reserve(m_BufferSize)) return FALSE; InitializeClient(); return TRUE; }
//This is the main function of the Client int main(int argc, char *argv[]) { int qHandlerPID, fMonitorPID, RC; if(argc <= 1) { printf("--ERROR, incomplete parameter list\n"); printf("--Correct Format\n\t./FndFl <<Server IP>> [<<Current Port>>]\n"); return 0; } strcpy(ServerIP, argv[1]); if(argc >= 3) { strcpy(MyPort, argv[2]); qPort = atoi(MyPort); } uPort = 40000; if(InitializeClient() < 0) { printf("--ERROR - Could not start the peer application, please restart the application or retry after restarting the system if problem persists\n"); exit(0); } //Start the query handler if((qHandlerPID = fork()) == 0) { StartQHandler(); } //Start the file monitor if((fMonitorPID = fork()) == 0) { fMonitor(); } printf("\n---------------------Node Ready------------------------\n"); printf("\tSearch and get the files you want.\n \tThe app is read ,enjoy!\n"); //Stay in this loop forever while(1) { if(GetUserInPut() == 0) { if((RC = SearchFile()) == 0) { printf("--Could not locate the file \"%s\"\n",InFileName); } else if(RC < 0) { printf("--Could not locate the file \"%s\"\n, RC was %d",InFileName, RC); } else { printf("--\tFile found, transfer started from %s\n",SourceIP); if((RC = GetFile()) == 0) { printf("--ERROR - File transfer was interrupted, restart the search\n"); } else if(RC < 0) { printf("--ERROR - File transfer was interrupted, restart the search\n"); } else { printf("--\tSuccess: file \"%s\" copied to the shared directory\n--\n", InFileName); } } } } //The program control should never come here printf("Program is in an inconsistant state, terminating the Node application\n"); exit(0); }
int main(void) { SOCKET ClientSockDesc; // Kliento pagrindinio soketo-klausytojo deskriptorius. char UserInput [BUFFLEN] = {0}; // Masyvas vartotojo komandoms nuskaityti. int SendResult; // Siuntimo funkcijos resultatui saugoti. int ReceiveResult; // Siuntimo funkcijos resultatui saugoti. char Packet [BUFFLEN] = {0}; // Buferiu masyvas duomenims gauti. int jCounter, i; // Skaitliukai. unsigned int ParseResult; // Komandu analizes rezultatui saugoti. fd_set MainSocketSet, TempSet; //pagrindine soketu aibe; soketu aibe, kuri turi duomenu, paruostu nuskaitymui unsigned int MaxKnownSocketDesc, iCounter;// maksimalus deskriptoriu kintamasis (soketo nr), skaitliukas (soket�) struct timeval TimeVal; // Laiko struktura dirbti su select(). WSADATA wsaData; if(WSAStartup(0x202, &wsaData) == 0) { } else { printf("ERROR: Initialization failure.\n"); } if ( INVALID_SOCKET == (ClientSockDesc = InitializeClient ()) ) { printf ("CNLabs Client error: client initialization failed.\n"); goto EXIT; } // Inicializuoti soketu aibes. FD_ZERO(&MainSocketSet); FD_ZERO(&TempSet); // Inicializuojame laiko struktura. 0 - reiskia, kad select() funkcija turi blokuotis laukdama, kol atsiras bent vienas aktyvus soketas. TimeVal.tv_sec = 10; //sekundes TimeVal.tv_usec = 500; //tukstantosios FD_SET(ClientSockDesc, &MainSocketSet); MaxKnownSocketDesc = ClientSockDesc; while ( 1 ) { // Kiekvienoje iteracijoje inicializuokime pagalbine soketu aibe pagrindine. TempSet = MainSocketSet; // Isrenkame is soketu aibes tuos deskriptorius, kurie apraso soketus, turincius duomenu, paruostu skaitymui. //select()tinklo primityvo panaudojimas sinchroniniu budu, kada blokuojamasi laukiant aktyviu soketu, turinciu info, kuria per juos galima skaityti is tinklo. //tikrina zemesnius uz pirmuoji nr pazymetus, todel +1, pirmos eiles soketu aibe (2,3 eiles soketu aibe), blokavimas ribota laika if ( SOCKET_ERROR == select (MaxKnownSocketDesc + 1, &TempSet, NULL, NULL, &TimeVal) ) { exit(EXIT_FAILURE); } for ( iCounter = 0; iCounter <= MaxKnownSocketDesc; iCounter++ ) { if (FD_ISSET(iCounter, &TempSet)) { if ( SOCKET_ERROR == ReceivePacket (&ClientSockDesc, Packet) ) { closesocket (ClientSockDesc); return INVALID_SOCKET; } UnmarshalPacket (Packet); printf ("%s\n", Packet); } else if(FD_ISSET(0, &TempSet)) { //fflush ( stdin ); memset (UserInput, 0, sizeof (UserInput)); fgets (UserInput, sizeof (UserInput), stdin); UserInput [strlen (UserInput) - 1] = '\0'; //ParseCommandInput (UserInput); MarshalPacket(UserInput); SendPacket (&ClientSockDesc, UserInput,strlen(UserInput)); } } } closesocket (ClientSockDesc); EXIT: WSACleanup(); return 1; }