Advertisement
Guest User

server

a guest
Sep 19th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. /* A simple server in the internet domain using TCP
  2. The port number is passed as an argument
  3. This version runs forever, forking off a separate
  4. process for each connection
  5. */
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <string.h>
  14.  
  15. void dostuff(int); /* function prototype */
  16. void error(const char *msg)
  17. {
  18. perror(msg);
  19. exit(1);
  20. }
  21. FILE *ptr_myfile;
  22. int main(int argc, char *argv[])
  23. {
  24.  
  25. int sockfd, newsockfd, portno, pid;
  26. socklen_t clilen;
  27. struct sockaddr_in serv_addr, cli_addr;
  28.  
  29. if (argc < 2) {
  30. fprintf(stderr,"ERROR, no port provided\n");
  31. exit(1);
  32. }
  33. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  34. if (sockfd < 0)
  35. error("ERROR opening socket");
  36. bzero((char *) &serv_addr, sizeof(serv_addr));
  37. portno = atoi(argv[1]);
  38. serv_addr.sin_family = AF_INET;
  39. serv_addr.sin_addr.s_addr = INADDR_ANY;
  40. serv_addr.sin_port = htons(portno);
  41. if (bind(sockfd, (struct sockaddr *) &serv_addr,
  42. sizeof(serv_addr)) < 0)
  43. error("ERROR on binding");
  44. listen(sockfd,5);
  45. clilen = sizeof(cli_addr);
  46. while (1) {
  47. newsockfd = accept(sockfd,
  48. (struct sockaddr *) &cli_addr, &clilen);
  49. if (newsockfd < 0)
  50. error("ERROR on accept");
  51. pid = fork();
  52. if (pid < 0)
  53. error("ERROR on fork");
  54. if (pid == 0) {
  55. close(sockfd);
  56. dostuff(newsockfd);
  57. exit(0);
  58. }
  59. else close(newsockfd);
  60. } /* end of while */
  61. close(sockfd);
  62. return 0; /* we never get here */
  63. }
  64.  
  65. /******** DOSTUFF() *********************
  66. There is a separate instance of this function
  67. for each connection. It handles all communication
  68. once a connnection has been established.
  69. *****************************************/
  70. void dostuff (int sock)
  71. {
  72. //writing file to client
  73.  
  74. int n;
  75. int counter;
  76. char buffer[256];
  77. bzero(buffer,256);
  78.  
  79.  
  80.  
  81. ptr_myfile=fopen("test1.txt","r");
  82. if(!ptr_myfile){
  83. printf("file couldn't open");
  84. }
  85.  
  86. /* for(counter=1;counter <=10;counter++){
  87. fwrite(&buffer,sizeof(*buffer),1,ptr_myfile);
  88. n = read(sock,buffer,255);
  89. n = write(sock,buffer,18);};*/
  90.  
  91. //make list match each get text and match to user
  92. if (n < 0) error("ERROR reading from socket"){
  93. // if (!strcmp ( buffer, "i gay\n" )) n = write(sock,"you are gay",18);
  94. //else{ printf("Here is the message: %s\n",buffer);
  95. n = write(sock,buffer,18);};
  96. fread(&ptr_myfile,sizeof(*buffer),1,buffer);
  97.  
  98. n = write(sock,"all good",18);
  99. };
  100. if (n < 0) error("ERROR writing to socket");
  101. }
  102.  
  103.  
  104.  
  105.  
  106. /*
  107.  
  108. if (n < 0) error("ERROR reading from socket");
  109. printf("Here is the message: %s\n",buffer);
  110. n = write(sock,"got it",18);
  111. if (n < 0) error("ERROR writing to socket");*/
  112. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement