so i have a client server program, and everything works perfectly, but now i have to modify my server code to allow for multiple clients, im not really sure if im using fork() correctly as i really dont understand what fork() does.
ive tried googling for about an hour and still dont understand it. although my program still runs im not sure if im testing it correctly since it runs no differently than before i added fork(),(other than the fact that typing q in my client no longer makes the server program end).
1) Am i using fork() correctly? if not can someone explain it to me?Code:#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <ctype.h> #define PORT 3132 //port for server being used main(){ int rc; //system cals return value int s; //socket descriptor int connected; //new connection's socket descriptor int bytes_received; //number of bytes received int size_csa; //size of clients address struct int pid; int i; //iterator char received[1024]; //array that stores received string char changed[1024]; //array to store the changes of the received data struct sockaddr_in sa; //internet address struct for server struct sockaddr_in ca; //internet address struct for client s = socket(AF_INET, SOCK_STREAM, 0); //checks for an error with socket() if (s < 0){ perror("Socket"); exit(1); } sa.sin_family = AF_INET; //using internet address family sa.sin_port = htons(PORT); //copy port number in network byte order sa.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY = Wild Card //checks for an error with socket() rc = bind(s, (struct sockaddr *)&sa, sizeof(sa)); if (rc){ perror("bind"); exit(1); } //checks to see if there is an error with listen() rc = listen(s,5); if (rc){ perror("listen"); exit(1); } while(1){ //accept a connection using the address of the client size_csa = sizeof(struct sockaddr_in); connected = accept(s, (struct sockaddr *)&ca, &size_csa); if (connected < 0){ perror("connect"); exit(1); } pid = fork(); if(pid < 0){ printf("fork error\n"); exit(1); } if(pid == 0){ while(1){ //clears the arrays received and changed for(i = 0; i < 1024; i++){ received[i] = '\0'; changed[i] = '\0'; } //adds a null terminator to the end of the received data bytes_received = recv(connected,received,1024,0); received[bytes_received] = '\0'; //changes the data in the array received to store data with all caps for(i = 0; i < strlen(received); i++) received[i] = toupper(received[i]); //takes the data from the array received in reverse order and stores it into the array changed for(i = 0; i < strlen(received); i++) changed[i] = received[strlen(received)-i-1]; //if the string that was received was "q" the program ends, otherwise it continues if (strcmp(changed , "Q\n") == 0){ close(connected); break; } else send(connected, changed,strlen(changed), 0); }//end while loop }//end if statement else continue; }//end while loop close(s); return 0; }//end function main()
2) when testing this in a linux terminal how can i test this program to make sure fork is working correctly?
EDIT: if necessary i can post my client code



3Likes
LinkBack URL
About LinkBacks



