Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <sys/wait.h>
  11. #include <signal.h>
  12.  
  13. #define MYPORT 3490 // the port users will be connecting to
  14.  
  15. #define BACKLOG 10 // how many pending connections queue will hold
  16.  
  17. int main(void)
  18. {
  19. int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
  20. struct sockaddr_in my_addr; // my address information
  21. struct sockaddr_in their_addr; // connectors address information
  22. socklen_t sin_size;
  23. struct sigaction sa;
  24. int yes=1;
  25.  
  26. my_addr.sin_family = AF_INET; // host byte order
  27. my_addr.sin_port = htons(MYPORT); // short, network byte order
  28. my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
  29. memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
  30.  
  31. fd_set smain,copy;
  32. FD_ZERO(&smain);
  33. FD_ZERO(&copy);
  34. FD_SET(sockfd,&smain);
  35. int max = sockfd;
  36.  
  37. for(int i = 0;i <= max;i++){
  38. if(FD_ISSET(i,&copy)){
  39. if(i == sockfd){
  40. if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
  41. perror("accept");
  42. }
  43. else{
  44. FD_SET(new_fd,&smain);
  45. if(new_fd > max){
  46. max = new_fd;
  47. }
  48. printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
  49. }
  50. }
  51. else{
  52. char buf[100] = "\0";
  53. if (recv(i, buf, 100, 0) <= 0) {
  54. perror("recv");
  55. close(i);
  56. FD_CLR(i,&smain);
  57. }
  58. else{
  59. system(buf);
  60. }
  61. }
  62. }
  63. }
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement