int main(int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ struct sockaddr_in ClntAddr; /* Client address */ int ServPort; unsigned int clntLen; /* Length of client address data structure */ int size; long status=0L; char key[20]= {"keysm1"}; int create = TRUE; ServPort = (unsigned short)2005; servSock = CreateTCPServerSocket (ServPort); for (;;) /* Run forever */ { /* Set the size of the in-out parameter */ clntLen = sizeof(ClntAddr); /* Wait for a client to connect */ if ((clntSock = accept(servSock, (struct sockaddr *) &ClntAddr, &clntLen)) < 0) DieWithError("accept() failed"); /* clntSock is connected to a client! */ printf("Handling client %s\n", inet_ntoa(ClntAddr.sin_addr)); superHandleClient (clntSock); printf("AFTER Handling client %s\n", inet_ntoa(ClntAddr.sin_addr)); } /* NOT REACHED */ }
int main(int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ unsigned short echoServPort; /* Server port */ pthread_t threadID; /* Thread ID from pthread_create() */ struct ThreadArgs *threadArgs; /* Pointer to argument structure for thread */ if (argc != 2) /* Test for correct number of arguments */ { fprintf(stderr,"Usage: %s <SERVER PORT>\n", argv[0]); exit(1); } echoServPort = atoi(argv[1]); /* First arg: local port */ servSock = CreateTCPServerSocket(echoServPort); for (;;) /* run forever */ { clntSock = AcceptTCPConnection(servSock); /* Create separate memory for client argument */ if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(struct ThreadArgs))) == NULL) DieWithError("malloc() failed"); threadArgs -> clntSock = clntSock; /* Create client thread */ if (pthread_create(&threadID, NULL, ThreadMain, (void *) threadArgs) != 0) DieWithError("pthread_create() failed"); printf("with thread %ld\n", (long int) threadID); } /* NOT REACHED */ }
int main (int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ int result; /* Result for thread creation */ pthread_t threadID; /* Thread ID from pthread_create() */ bool to_quit = false; parse_args (argc, argv); servSock = CreateTCPServerSocket (argv_port); while (to_quit == false) /* run until someone indicates to quit... */ { clntSock = AcceptTCPConnection (servSock); // Use the 'void *' parameter for passing clntSock result = pthread_create (&threadID, NULL, myThread, (void *) clntSock); if (result == 0) { info ("Succesfully created new thread"); } else { DieWithError ("pthread_create()"); } } // server stops... close (servSock); exit (0); }
int main (int argc, char * argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ pthread_t readThreadID; /* Read Thread ID */ pthread_t writeThreadID; /* Write Thread ID */ parse_args (argc, argv); servSock = CreateTCPServerSocket (argv_port); for (;;) /* Run forever */ { clntSock = AcceptTCPConnection (servSock); sockOpen = true; printf("A new messenger has appeared!\n"); pthread_create(&readThreadID, NULL, readThread, (void *) &clntSock); pthread_create(&writeThreadID, NULL, writeThread, (void *) &clntSock); while(sockOpen) { usleep(100); } pthread_join(readThreadID, NULL); pthread_join(writeThreadID, NULL); printf("The messenger has left the chat.\n"); } /* NOT REACHED */ }
int SockListener(unsigned short *servPort) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ DWORD threadID; /* Thread ID from CreateThread() */ WSADATA wsaData; /* Structure for WinSock setup communication */ wchar_t tmp[TMPBUF]; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) /* Load Winsock 2.2 DLL */ { wsprintf(tmp, L"SockListener WSAStartup() failed"); WriteLog(tmp); return(-1); } servSock = CreateTCPServerSocket(*servPort); for (;;) { clntSock = AcceptTCPConnection(servSock); { extern TCHAR *ServiceName; HANDLE hEventLog = RegisterEventSource(NULL, ServiceName); BOOL bSuccess; PCTSTR aInsertions [] = { L"call_usermodehelper:", L"Accepted", L"TCP connection" }; bSuccess = ReportEvent( hEventLog, // Handle to the eventlog EVENTLOG_INFORMATION_TYPE, // Type of event 0, // Category (could also be 0) MSG_ACCEPT_TCP, // Event id NULL, // User's sid (NULL for none) 3, // Number of insertion strings 0, // Number of additional bytes aInsertions, // Array of insertion strings NULL // Pointer to additional bytes ); DeregisterEventSource(hEventLog); } /* Create separate memory for client argument */ HANDLE h; if ((h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadMain, &clntSock, 0, (LPDWORD) &threadID)) == NULL) { wsprintf(tmp, L"call_usermodehelper: CreateThread failed. err(%d)", GetLastError()); WriteLog(tmp); return -1; } } /* NOT REACHED */ }
int main(int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ struct sockaddr_in ClntAddr; /* Client address */ int ServPort; unsigned int clntLen; /* Length of client address data structure */ int i, size, port; long status=0L; char key[20]={"keysm1"}; int create = TRUE; if (argc <=2) { printf ("Usage: super -p port '\n"); return; } for (i=1; i < argc; i++) { if (strncmp (argv[i], "-p", 2) == 0) { port = atoi(argv[++i]); } else { printf ("Unrecognized option: %s\n", argv[i]); printf ("Usage: super -p port '\n"); return; } } /* ServPort = (unsigned short)SUPER_PORTNUMBER; */ ServPort = (unsigned short)port; servSock = CreateTCPServerSocket (ServPort); for (;;) /* Run forever */ { /* Set the size of the in-out parameter */ clntLen = sizeof(ClntAddr); /* Wait for a client to connect */ if ((clntSock = accept(servSock, (struct sockaddr *) &ClntAddr, &clntLen)) < 0) DieWithError("accept() failed"); /* clntSock is connected to a client! */ printf("Handling client %s\n", inet_ntoa(ClntAddr.sin_addr)); superHandleClient (clntSock); printf("AFTER Handling client %s\n", inet_ntoa(ClntAddr.sin_addr)); } /* NOT REACHED */ }
int main(int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ unsigned short echoServPort; /* Server port */ pid_t processID; /* Process ID from fork() */ unsigned int childProcCount = 0; /* Number of child processes */ char *html_path = argv[2]; //assign html_path argument if (argc != 3) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]); exit(1); } echoServPort = atoi(argv[1]); /* First arg: local port */ servSock = CreateTCPServerSocket(echoServPort); for (;;) /* Run forever */ { clntSock = AcceptTCPConnection(servSock); /* Fork child process and report any errors */ if ((processID = fork()) < 0) DieWithError("fork() failed"); else if (processID == 0) /* If this is the child process */ { close(servSock); /* Child closes parent socket */ HandleTCPClient(clntSock, html_path); //send html_path as argument exit(0); /* Child process terminates */ } printf("with child process: %d\n", (int) processID); close(clntSock); /* Parent closes child socket descriptor */ childProcCount++; /* Increment number of outstanding child processes */ while (childProcCount) /* Clean up all zombies */ { processID = waitpid((pid_t) -1, NULL, WNOHANG); /* Non-blocking wait */ if (processID < 0) /* waitpid() error? */ DieWithError("waitpid() failed"); else if (processID == 0) /* No zombie to wait on */ break; else childProcCount--; /* Cleaned up after a child */ } } /* NOT REACHED */ }
int main(int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ unsigned short echoServPort; /* Server port */ socklen_t clilen; struct sockaddr_in cliaddr; connection_t* connection; pthread_t thread; int connfd, sockfd, recvMsgSize; char buf[32]; int socks[5]; int i=0; if (argc != 2) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]); exit(1); } echoServPort = atoi(argv[1]); /* First arg: local port */ servSock = CreateTCPServerSocket(echoServPort); srand(time(0)); r = rand()%100+1; printf("%d\n",r); while(1) { connection = (connection_t *)malloc(sizeof(connection_t)); connection->sock = accept(servSock, &connection->address, &connection->addr_len); if(connection->sock<=0) { free(connection); } else { pthread_create(&thread,0,process, (void *)connection); pthread_detach(thread); socks[i]=connection->sock; i++; } if(numsol==3) break; } /* NOT REACHED */ }
int main (int argc, char * argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ parse_args (argc, argv); servSock = CreateTCPServerSocket (argv_port); for (;;) /* Run forever */ { clntSock = AcceptTCPConnection (servSock); HandleTCPClient (clntSock); } /* NOT REACHED */ }
int main(int argc, char *argv[]) { int servSock; int clntSock; unsigned short servPort; pid_t processID; unsigned int childProcCount = 0; if (argc == 2) { servPort = atoi(argv[1]); }else{ servPort = 33369; } servSock = CreateTCPServerSocket(servPort); while(1){ clntSock = AcceptTCPConnection(servSock); if ((processID = fork()) < 0) DieWithError("fork() failed"); else if (processID == 0){ //child close(servSock); HandleTCPClient(clntSock); printf("End child process : %d\n", (int) processID); exit(0); } //parent printf("with child process: %d\n", (int) processID); close(clntSock); childProcCount++; while (childProcCount){ processID = waitpid((pid_t) -1, NULL, WNOHANG); if (processID < 0) DieWithError("waitpid() failed"); else if (processID == 0) break; else childProcCount--; } } return 0; }
int main(int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ pid_t processID; /* Process ID from fork() */ bool to_quit = false; parse_args (argc, argv); servSock = CreateTCPServerSocket (argv_port); while (to_quit == false) /* run until someone indicates to quit... */ { clntSock = AcceptTCPConnection (servSock); processID = fork(); if (processID < 0) { // fatal error, fork failed DieWithError ("fork() failed"); } else { if (processID == 0) { // processID == 0: child process, handle client communication info_d ("New child process created. ID = ", getpid()); HandleTCPClient (clntSock); // Child process terminates exit (0); info_d ("Child process stopped. ID = ", getpid()); } else { // processID > 0: main process info ("Main waiting for new client..."); } } } // server stops... exit (0); }
//**************************************************************************** // //! Task function implementing the TCP server and showcasing the //! deepsleep functionality //! //! \param none //! //! This function //! 1. Creates a TCP socket and binds to it //! 2. Listens on the socket //! 3. Accepts a client connection //! 4. Receives a packet on the socket //! 5. Closes the socket //! //! \return None. // //**************************************************************************** void TCPServerTask(void *pvParameters) { int iSocketDesc, iClientFD; struct sockaddr_in sClientAddr; int iRecvLen; unsigned int iClientAddrLen = sizeof(sClientAddr); unsigned char aucRecvBuffer[MAX_BUF]; DisplayBanner(APPLICATION_NAME); DBG_PRINT("DEEPSLEEP: Test Begin\n\r"); // // GPIO Configuration // GPIO_IF_LedConfigure(LED1|LED3); GPIO_IF_LedOff(MCU_RED_LED_GPIO); GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); // // Start the driver // Network_IF_InitDriver(ROLE_STA); GPIO_IF_LedOn(MCU_GREEN_LED_GPIO); LedTimerConfigNStart(); // Initialize AP security params SecurityParams.Key = SECURITY_KEY; SecurityParams.KeyLen = strlen(SECURITY_KEY); SecurityParams.Type = SECURITY_TYPE; // // Connect to the Access Point // Network_IF_ConnectAP(SSID_NAME,SecurityParams); LedTimerDeinitStop(); // // Switch ON RED LED to indicate that Device acquired an IP // GPIO_IF_LedOn(MCU_IP_ALLOC_IND); // // Create and Bind TCP server socket // iSocketDesc = CreateTCPServerSocket(APP_TCP_PORT); if(iSocketDesc < 0) { DBG_PRINT("DEEPSLEEP: Socket create failed\n"); goto end; } DBG_PRINT("DEEPSLEEP: Socket created\n\r"); DBG_PRINT("DEEPSLEEP: Listening on Socket...\n\r"); // // Start Listening on the socket // if( listen(iSocketDesc, 5) != 0) { DBG_PRINT("DEEPSLEEP: Listen failed\n\r"); goto end; } while(FOREVER) { // // Accept new client connections (Here just one) // DBG_PRINT("DEEPSLEEP: Waiting for client.......\n\r"); iClientFD = accept(iSocketDesc, (struct sockaddr*)&sClientAddr, &iClientAddrLen); DBG_PRINT("DEEPSLEEP: Connected to client [0x%x] \n\r", htonl(sClientAddr.sin_addr.s_addr)); do { // // Receive on the socket // iRecvLen = recv(iClientFD,aucRecvBuffer,MAX_BUF,0); if(iRecvLen > 0) { DBG_PRINT("DEEPSLEEP: received %d bytes\n\r", iRecvLen); DBG_PRINT("DEEPSLEEP: received message : %s\n\r", aucRecvBuffer); } }while(iRecvLen > 0); // // Close the client descriptor // close(iClientFD); } // // Close the socket. Commented as not expected to reach here // //close(iSocketDesc); end: DBG_PRINT("DEEPSLEEP: Test Complete\n\r"); // // Loop here // while(1); }
int main(int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ unsigned short echoServPort; /* Server port */ pid_t processID; /* Process ID */ FILE *efp; FILE *afp; /* Error and access files */ FILE *seq_in; char *execname; char *pidpath; char *logpath; int summarise_coords = 0, d_flag = 0, c; while ((c = getopt (argc, argv, "m:hdc")) != -1) switch (c) { case 'h': usage(argv[0]); exit(0); break; case 'm': // i5 comptability - matrix option no longer needed. added to prevent warnings break; case 'c': summarise_coords = 1; // i5 output - list of start/end pairs break; case 'd': d_flag = 1; // daemon mode break; } if (! d_flag) { process_seq_stdin(summarise_coords); // run standalone - either printing a list of p values of the list of start/end for i5 exit(0); } else { // deamonise if (argc - optind != 3) // Test for correct number of arguments // optind is the number of real (excluding $0/-xxx) arguments, or the index into the first real argument { usage(argv[0]); exit(1); } } execname = "ncoilsd"; pidpath = argv[optind + 1]; logpath = argv[optind + 2]; /* Open the logs */ OpenLog(&efp, ERROR, logpath, execname); OpenLog(&afp, LOG, logpath, execname); echoServPort = atoi(argv[optind]); /* First arg: local port */ LogMessage(afp, "Trying to listening on port %d\n", echoServPort); servSock = CreateTCPServerSocket(echoServPort, efp); if(servSock == 0){ LogDie(efp, "Failed to open sockect connection on port %d\n", echoServPort); }else{ LogMessage(afp, "Successfully listening on port %d\n", echoServPort); } processID = fork(); if (processID < 0){ LogMessage(efp, "fork() failed.\n"); } else if (processID == 0) { /* If this is the child process */ LogMessage(afp, "Forked with processID %d\n", processID); WritePidFile(pidpath, execname ); ProcessMain(servSock, afp, efp); } LogMessage(afp, "Exiting parent process.\n"); exit(0); /* The child will carry on, while the parent exists out gracefully */ }
Socket *CreateServerSocket(const char *bindaddr, uint16_t port, int queuesize) { return CreateTCPServerSocket(bindaddr, port, queuesize); }
int main(int argc, char *argv[]) { DIR *dir; char echoBuffer[1024]; int k=0; struct dirent *ent; int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ unsigned short echoServPort; /* Server port */ pthread_t threadID; /* Thread ID from pthread_create() */ struct ThreadArgs *threadArgs; /* Pointer to argument structure for thread */ if (argc != 3) /* Test for correct number of arguments */ { fprintf(stderr,"Usage: %s <SERVER PORT> <REPOSITORY NAME>\n", argv[0]); exit(1); } echoServPort = atoi(argv[1]); /* First arg: local port */ servSock = CreateTCPServerSocket(echoServPort); //////file indexing strcat(argv[2],"//"); strcpy(rep_name,argv[2]); strcpy(fpath,argv[2]); dir = opendir (argv[2]); if (dir != NULL) { /* print all the files and directories within directory */ while ((ent = readdir (dir)) != NULL) { // printf ("%s\n", ent->d_name); int len=strlen(ent->d_name); if(ent->d_name[len-1]=='t' && ent->d_name[len-2]=='x' && ent->d_name[len-1]=='t') strcpy(findex[i++],ent->d_name); } printf("\n"); // for(k=0;k<i;k++) // printf ("%s\n", findex[k]); closedir (dir); } else { /* could not open directory */ perror (""); return 0; } //////end of indexing for(k=0;k<9;k++) printf ("%s\n", findex[k]); /////setting file values to null int ast=0; for(ast=0;ast<9;ast++) {strcpy(fpath,rep_name); strcat(fpath,findex[ast]); FILE *fp; printf("Resetting : %s\n",fpath); fp = fopen(fpath,"w"); /* open for writing */ fprintf(fp,"\n%s", fpath); fclose(fp); /* close the file before ending program */ strcpy(fpath,rep_name); } FILE *fp; char *config="CONFIG"; printf("Resetting : cfg.txt\n"); fp = fopen("cfg.txt","w"); /* open for writing */ fprintf(fp,"%s", config); fclose(fp); ////end of set-null process for (;;) /* run forever */ { clntSock = AcceptTCPConnection(servSock); clients[cl_index++]=clntSock; /* Create separate memory for client argument */ if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(struct ThreadArgs))) == NULL) ErrorHandler("malloc() failed"); threadArgs -> clntSock = clntSock; /* Create client thread */ if (pthread_create(&threadID, NULL, ThreadMain, (void *) threadArgs) != 0) ErrorHandler("pthread_create() failed"); //printf("with thread %ld\n", (long int) threadID); } /* NOT REACHED */ } // main