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 */ }
void ProcessMain (int servSock, FILE *lfp, FILE *efp) { int clntSock; /* Socket descriptor for client connection */ pthread_t threadID; /* Thread ID from pthread_create() */ struct ThreadArgs *threadArgs; /* Pointer to argument structure for thread */ for (;;) /* run forever */ { clntSock = AcceptTCPConnection(servSock, lfp, efp); /* Create separate memory for client argument */ if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(struct ThreadArgs))) == NULL){ LogDie(efp, "malloc() failed for ThreadArgs!"); } /* populate the newly generated struc */ threadArgs->clntSock = clntSock; threadArgs->logfp = lfp; threadArgs->errfp = efp; /* Create client thread and process the socket connection in the ThreadMain method*/ if (pthread_create(&threadID, NULL, ThreadMain, (void *) threadArgs) != 0){ LogDie(efp, "pthread_create() failed"); } //ThreadMain( (void *) threadArgs); } /* 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[]) { if (argc != 2) // Test for correct number of arguments DieWithUserMessage("Parameter(s)", "<Server Port/Service>"); char *servPort = argv[1]; // First arg: local port int servSock = SetupTCPServerSocket(servPort); if (servSock < 0) DieWithUserMessage("SetupTCPServerSocket() failed", "unable to establish"); for (;;) { // Run forever int clntSock = AcceptTCPConnection(servSock); // Create separate memory for client argument struct ThreadArgs *threadArgs = (struct ThreadArgs *) malloc( sizeof(struct ThreadArgs)); if (threadArgs == NULL) DieWithSystemMessage("malloc() failed"); threadArgs->clntSock = clntSock; // Create client thread pthread_t threadID; int returnValue = pthread_create(&threadID, NULL, ThreadMain, threadArgs); if (returnValue != 0) DieWithUserMessage("pthread_create() failed", strerror(returnValue)); 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 */ 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 main(int argc, char *argv[]) { if (argc != 2) // Test for correct number of arguments DieWithUserMessage("Parameter(s)", "<Server Port/Service>"); int servSock = SetupTCPServerSocket(argv[1]); // servSock is now ready to use to accept connections for (;;) { // Run forever // Wait for a client to connect int clntSock = AcceptTCPConnection(servSock); // Create an input stream from the socket FILE *channel = fdopen(clntSock, "r+"); if (channel == NULL) DieWithSystemMessage("fdopen() failed"); // Receive messages until connection closes int mSize; uint8_t inBuf[MAX_WIRE_SIZE]; VoteInfo v; while ((mSize = GetNextMsg(channel, inBuf, MAX_WIRE_SIZE)) > 0) { memset(&v, 0, sizeof(v)); // Clear vote information printf("Received message (%d bytes)\n", mSize); if (Decode(inBuf, mSize, &v)) { // Parse to get VoteInfo if (!v.isResponse) { // Ignore non-requests v.isResponse = true; if (v.candidate >= 0 && v.candidate <= MAX_CANDIDATE) { if (!v.isInquiry) counts[v.candidate] += 1; v.count = counts[v.candidate]; } // Ignore invalid candidates } uint8_t outBuf[MAX_WIRE_SIZE]; mSize = Encode(&v, outBuf, MAX_WIRE_SIZE); if (PutMsg(outBuf, mSize, channel) < 0) { fputs("Error framing/outputting message\n", stderr); break; } else { printf("Processed %s for candidate %d; current count is %llu.\n", (v.isInquiry ? "inquiry" : "vote"), v.candidate, v.count); } fflush(channel); } else { fputs("Parse error, closing connection.\n", stderr); break; } } puts("Client finished"); fclose(channel); } // Each client // 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 */ 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 */ 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[]) { if (argc != 2) ErrorWithUserMessage("Parameter(s), <Server Port>"); char *service = argv[1]; int servSock = SetupTCPServerSocket(service); if (servSock < 0) ErrorWithUserMessage("SetupTCPServerSocket() failed: unable to establish"); unsigned int childProcessCount = 0; while (1) { int clntSock = AcceptTCPConnection(servSock); pid_t processID = fork(); if (processID < 0) ErrorWithSystemMessage("fork() failed"); else if (processID == 0) { close(servSock); HandleTCPClient(clntSock); exit(EXIT_SUCCESS); } printf("with child process: %d\n", processID); close(clntSock); childProcessCount++; //clean up zombies while (childProcessCount) { processID = waitpid((pid_t) - 1, NULL, WNOHANG); if (processID < 0) ErrorWithSystemMessage("waitpid() failed"); else if (processID == 0) break; else childProcessCount--; } } }
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); }
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