void varDecl2(int X) { switch (X) { int A=1; // expected-warning {{never executed}} case 1: dostuff(&A); break; case 2: dostuff(&A); break; } }
void varDecl1(int X) { switch (X) { int A; // No warning here. case 1: dostuff(&A); break; case 2: dostuff(&A); break; } }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen, pid; struct sockaddr_in serv_addr, cli_addr; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); dostuff(newsockfd); close(newsockfd); } } /* end of while */
static void test (struct A *a) { dostuff (a); if (a->foo ()!= 2) __builtin_abort (); }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen, pid,n; struct sockaddr_in serv_addr, cli_addr; char command[10]; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); while(1) { newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); do { bzero(command,10); do { n=read(newsockfd,command,10); if (n < 0) error("ERROR reading from socket"); }while(n<0); printf("command:%s###\n",command); n=write(newsockfd,"received",10); if (n < 0) error("ERROR writing to socket"); dostuff(newsockfd,command); }while(strcmp(command,"exit")!=0); close(newsockfd); exit(0); } else { close(newsockfd); continue; } } /* end of while */ //close(sockfd); return 0; /* we never get here */ }
// task2 does fops at a different rate void task2(int id) { struct RastPort rp1; struct BitMap bm1; uint32_t col = colours[id]; int count = 0; float t2_total = 0.f; int t2_itotal = 0; dprintf("task %08x colour %06x sp =~ %08x\n", id, col, &rp1+1); rp1 = *rp; bm1 = *rp->drawable.bitmap; rp1.drawable.bitmap = &bm1; while (1) { dostuff(&rp1, id, col); count++; if (count > 3) { t2_total += 1; t2_itotal += 1; if ((int)(t2_total) != t2_itotal) { dprintf("fpu funny in task 1\n"); } count = 0; } } }
int main() { L a; std::cout << "calling dostuff" << std::endl; dostuff([a]() { std::cout << "worker work" << std::endl; }, []() { std::cout << "work complete" << std::endl; }); std::this_thread::sleep_for(std::chrono::seconds(2)); }
static void test (struct A *a) { dostuff (a); static_cast<B*>(a)->~B(); new(a) B(); test2(a); }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; struct sigaction sa; // for signal SIGCHLD if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); /****** Kill Zombie Processes ******/ sa.sa_handler = sigchld_handler; // reap all dead processes sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { perror("sigaction"); exit(1); } /*********************************/ while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); //create a new process if (pid < 0) error("ERROR on fork"); if (pid == 0) { // fork() returns a value of 0 to the child process close(sockfd); dostuff(newsockfd); exit(0); } else //returns the process ID of the child process to the parent close(newsockfd); // parent doesn't need this } /* end of while */ return 0; /* we never get here */ }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; if (argc < 2) { fprintf(stderr,"ERROR, no hay ningĂșn puerto\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR de apertura de socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR en la conexion"); listen(sockfd,5); clilen = sizeof(cli_addr); while (1) { newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR en aceptar"); pid = fork(); if (pid < 0) error("ERROR en fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); } close(sockfd); return 0; }
int main (int argc, char *argv[]) { int port; if (argc != 3 || !convertint (argv[2], &port)) fatal << "usage: ex2 <hostname> <port>\n"; dostuff (argv[1], port, wrap (finish)); amain (); }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; char* program; program = (char*)malloc(2048); /* Handle SIGCHLD by calling clean_up_child_process. */ struct sigaction sigchld_action; memset (&sigchld_action, 0, sizeof (sigchld_action)); sigchld_action.sa_handler = &clean_up_child_process; /* sigaction (SIGCHLD, &sigchld_action, NULL); */ /* zombies ain't so bad after all... better than threads */ if (argc < 3) { if (argc < 2) fprintf(stderr,"ERROR, no port provided\n"); fprintf(stderr,"ERROR, no application path\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); sprintf(program, "%s", argv[2]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(program, newsockfd); exit(0); } else { close(newsockfd); /* continue */ } } /* end of while */ close(sockfd); return 0; /* we never get here */ }
void exp05() { const int x = 42; int y = (int)x; int *p; p = (int *)&x; // cert-EXP05-C const int data[] = {1,2,3,4}; dostuff(data); // cert-EXP05-C }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen, pid; struct sockaddr_in serv_addr, cli_addr; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); return 0; } void dostuff (int sock) { int n; char buffer[256]; bzero(buffer,256); n = read(sock,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); n = write(sock,"I got your message",18); if (n < 0) error("ERROR writing to socket"); }
int server(char *argv[]){ int sockfd, newsockfd, portno; socklen_t clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); while(true){ dostuff(newsockfd); /*bzero(buffer,256); n = read(newsockfd,buffer,255); if (strcmp("exit\n",buffer) == 0) { printf("%s\n","Tencant Conexio"); close(newsockfd); close(sockfd); return 0; } if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); printf("Please enter the message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); //n = write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket");*/ } printf("%s\n","out"); close(newsockfd); close(sockfd); return 0; }
void unusualSyntax(int *P1, struct XY *P2) { 10[P1] = 0; // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: confusing array subscript expression, usually the index is inside the [] // CHECK-FIXES: P1[10] = 0; 10[P2->X] = 0; // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: confusing array subscript expression // CHECK-FIXES: P2->X[10] = 0; dostuff(1["abc"]); // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: confusing array subscript expression // CHECK-FIXES: dostuff("abc"[1]); dostuff(1[ABC]); // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: confusing array subscript expression // CHECK-FIXES: dostuff(ABC[1]); dostuff(0[0 + ABC]); // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: confusing array subscript expression // CHECK-FIXES: dostuff(0[0 + ABC]); // No fixit. Probably the code should be ABC[0] }
// task4 just flashes colours void task4(int id) { struct RastPort rp1; struct BitMap bm1; uint32_t col = colours[id]; dprintf("task %08x colour %06x sp =~ %08x\n", id, col, &rp1+1); rp1 = *rp; bm1 = *rp->drawable.bitmap; rp1.drawable.bitmap = &bm1; while (1) { dostuff(&rp1, id, col); } }
int main(int argc, char *argv[]) /* Creating Socket for multiple Client */ { int num; int sin_size; int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); } /* end of while */ close(sockfd); return 0; /* we never get here */ }
void* start_server_socket(void *arg) { //TCPDUMP server port no int server_port = 50999; int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { socket_error("Error in opening tcpdump socket"); __android_log_print(ANDROID_LOG_INFO, "TcpdumpSocket","error in opening tcpdump socket"); } bzero((char *) &serv_addr, sizeof(serv_addr)); portno = server_port; __android_log_print(ANDROID_LOG_INFO, "TcpdumpSocket","portno = %d", portno); serv_addr.sin_family = PF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { socket_error("Error in binding to tcdump socket"); __android_log_print(ANDROID_LOG_INFO, "TcpdumpSocket","Error in binding to tcpdump socket"); } __android_log_print(ANDROID_LOG_DEBUG, "TcpdumpSocket","socket listening"); listen(sockfd, 5); clilen = sizeof(cli_addr); while (1) { printf(" inside tcpdump server socket \n"); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (-1 == newsockfd) { if (bearerChangedvalue > 0) { /* Restart accept to try bearer change on socket */ continue; } else { __android_log_print(ANDROID_LOG_INFO, "TcpdumpSocket","Error in accept"); socket_error("Error in accept Socket_server"); } } else { __android_log_print(ANDROID_LOG_INFO, "TcpdumpSocket","dostuff"); dostuff(newsockfd); } break; } close(sockfd); return 0; }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen, n, pid; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; // if (argc < 2) { // fprintf(stderr,"ERROR, no port provided\n"); // exit(1); // } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) //AF_UNIX, SOCK_DGRAM error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); //Sets all values in a buffer to zero portno = 4001;//atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(portno); serv_addr.sin_addr.s_addr = INADDR_ANY; //Gets the IP address of the machine on which the server is running. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); while(1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); } return 0; }
void grabframe(IplImage *frm) { if(!Processing) { cvResize(frm,camimg); cvFlip( camimg, camimg,0); dostuff(camimg); if(show) dovisstuff(); } else { printf("skiping frame \n"); } }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; if (argc < 3) { fprintf(stderr,"Usage: ./simple_server.c ip_address port\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[2]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(argv[1]); serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); } /* end of while */ close(sockfd); return 0; /* we never get here */ }
void thisWorks(int port) { int sockfd, newsockfd, portno; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; pid_t pid; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = port; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); printf("Server listening on port %d\n", portno); while (1) { if (-1 == (newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen))) error("oh no\n"); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); } /* end of while */ close(sockfd); return; /* we never get here */ }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, pid; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); /*argument checking*/ exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); /*create new sockfd*/ if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); //intialization with zero of all address portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; /*assign header*/ serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, /*bind the address*/ sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); /*listen to correspondng socketfd*/ clilen = sizeof(cli_addr); /*size of client address*/ while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); /*accept the address of client*/ if (newsockfd < 0) error("ERROR on accept"); pid = fork(); /*run till we didn't stop it manually ctrl+c every time it's make new thread for connection*/ if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); } /* end of while */ close(sockfd); return 0; /* we never get here */ }
int main (int argc, char* argv[]) { int done=0; SDL_Event event; fullscreen=0; sdlglinit(640,480,BPP,fullscreen); initsound(); ourinit(); oursoundinit(); resetobjects(); while(!done) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_VIDEORESIZE: surface=SDL_SetVideoMode(event.resize.w,event.resize.h,BPP,videoflags); if(!surface) error("Couldn't get surface after resize: %s\n",SDL_GetError()); initgl(); /* after setting video mode, we lost every gl setting. set again. */ resizeglscene(event.resize.w,event.resize.h); break; case SDL_KEYDOWN: keys[event.key.keysym.sym]=1; handlekeypress(&event.key.keysym); break; case SDL_KEYUP: keys[event.key.keysym.sym]=0; break; case SDL_QUIT: goto theend; } } /* check for held keys */ heldkeys(); dostuff(); drawglscene(); SDL_Delay(1000/70.0); } theend: shutdownsound(); killglwindow(); return 0; }
int libraryFunction2() { int c, n; int dig; char *cp = reply_string; int pflag = 0; int code; for (;;) { dig = n = code = 0; while ((c = getc(cin)) != '\n') { if (dig < 4 && dostuff()) code = code * 10 + (c - '0'); if (!pflag && code == 227) pflag = 1; if (n == 0) n = c; if (c == EOF) return(4); if (cp < &reply_string[sizeof(reply_string) - 1]) *cp++ = c; // no-warning } } }
// task3 does an illegal instruction after a while void task3(int id) { struct RastPort rp1; struct BitMap bm1; uint32_t col = colours[id]; int count = 0; dprintf("task %08x colour %06x sp =~ %08x\n", id, col, &rp1+1); rp1 = *rp; bm1 = *rp->drawable.bitmap; rp1.drawable.bitmap = &bm1; while (1) { dostuff(&rp1, id, col); // some contrived examples to check the code. count++; if (count > 5) { asm("cdp p7, #15, c0, c0, c0, #7"); dprintf("I'm suspended now .. shouldn't see this\n"); count = 0; } } }
int main(int argc, char *argv[]) { //sockfd and newsockfd are file descriptors //they store the values returned by the socket system call int sockfd, newsockfd, portno, pid; socklen_t clilen; //a struct containing the internet address struct sockaddr_in serv_addr, cli_addr; //if the user does not provide a port # give error: if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } //creating a new socket //AF_INET is the unix domain for two processes that share a common file system //SOCK_STREAM is the internet domain for any two hosts on the Internet sockfd = socket(AF_INET, SOCK_STREAM, 0); //if a socket call fails it returns -1 if (sockfd < 0) error("ERROR opening socket"); //bzero sets all values in a buffer to zero //takes a pointer to the buffer, and the size of the buffer bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); //adding the fields of the sevr_addr struct serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(portno); //htons converts it to nertwork byte order serv_addr.sin_addr.s_addr = INADDR_ANY; //bind() binds a socket to an address //takes the current addr of the current hose + the port # if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); //liste system call to listen for connections listen(sockfd,5); //accept wakes up the process on successful connection clilen = sizeof(cli_addr); //we want to be able to have multiple connections established while(1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); //we make sure the new connection fire descriptor is not -1 (error) if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if(pid < 0) error("ERROR on FORK()"); if(pid == 0) { close(sockfd); dostuff(newsockfd, pid); exit(0); } else close(newsockfd); }//end of while(1) close(sockfd); return 0; }
int main(int argc, char *argv[]){ //**Variable Declaration** //File descriptors, i.e. array subscripts into the file descripter table. int sockfd, newsockfd; //Stores the port number on which server accepts connections int portno; //Stores the size of the addess of the client. //This is needed for the accept system call. int clilen; //The return value for write() and read() calls. //It contains the number of characters written or read. int n; //Server reads characters from the socket connection into this buffer char buffer[256]; //A structure containing an internet addess. //This structure is defined in <netinet.h> //Struct definition // struct sockadd_in{ // short sin_family; // u_short sin_port; // struct in_addr sin_addr; // char sin_zero[8]; // } //in_addr structure, defined in the same header file, //contains only one field, a unsigned long called s_addr. //Will contain the address of the server. struct sockaddr_in serv_addr; //Will contain the address of the client which connects to the server. struct sockaddr_in cli_addr; //**End of Variable Declaration** //Displays an error message if the user fails to pass in the port number // on which the server will accept connection as an arguement if(argc < 2){ fprintf(stderr, "ERROR, no port provided\n"); exit(1); } //Socket system call creates a new socket. //First agruement is the domain of the socket // AF_INET: a constant used for the address domain for any two hosts on the internet //Second Arguement is the type of socket // SOCK_STREAM: a constant for a stream socket in which characters are read in a continuous stream as if from a file or pipe. //Third arguement is the protocol. //Should almost always be 0 sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0 ) error("Error opening socket\n"); //bzero sets all values in a buffer to zero. //Takes two arguements // First: a pointer to the buffer // Second: Size of the buffer bzero((char *) &serv_addr, sizeof(serv_addr)); //Uses atoi to convert the value of arg[1] into an integer // to be used a the port number which the server will listen for connections. portno = atoi(argv[1]); //sin_family should always be set to the symbolic constant AF_INET serv_addr.sin_family = AF_INET; //Must use htons() to convert a port number in host byte orer to a port number in network byte order serv_addr.sin_port = htons(portno); //Use INADDR_ANY to set the IP address of the machine on which the server is running serv_addr.sin_addr.s_addr = INADDR_ANY; //bind() binds a socket to an address. //In this case the address of the current host and port number on which the server will run //Takes 3 Arguements // First: The socket file descripter, the adress to which is bound and the size of the address to which it ibount // Second: a pointer to a structure of type sockaddr, but what is passed in is a structure of type aockaddr_in, so it must be cast correctly if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); //listen() allows the process to listen on the socket for connections. //First arguement is the socket file descriptor //Second arguemtn is the size of the backlog queue // which is the number of connection that can be waiting while the process is handling a particular connection listen(sockfd,5); //accept() causes the process to block until a client conects to the server. //Wakes up the process when a connection from a client has been successfully established. //Returns a new file descriptor and all communication on this connection should be done using the new file descriptor //Second arguement is a reference pointer to the address of the client on the other end //Third arguemtn is the size of this structure clilen = sizeof(cli_addr); printf("Would you like the server to accept multiple connection?(y/n)\n"); char multiple; scanf("%c", &multiple); if(multiple == 'y'){ while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); int pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0){ close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); } } else { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if(newsockfd < 0) error("ERROR on accept"); //This code will only execute once a client has successfully connected to the server //Initializes the buffer. //Reads from the socket //read() will block until there is something for it to read in the socket //Will read either the total number of characters in the socket or 255, whichever is less and return the number bzero(buffer, 256); n = read(newsockfd, buffer, 255); if(n < 0) error("ERROR reading from socket"); printf("Here is the message %s\n", buffer); //Writes to the client //Last arguement of write is the size of the message n = write(newsockfd, "I got your message", 18); if(n < 0) error("ERROR writing to socket"); } return 0; }
int main() { dostuff(); return 0; }