Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<sys/socket.h>
  5. #include<arpa/inet.h>
  6. #include<unistd.h>
  7.  
  8. #define QUEUE 1 //Queue for incoming connections
  9. #define BUFFLENGTH 512 //Send buffer length
  10.  
  11. void fileops(int clientsock);
  12.  
  13. int main(int argc , char *argv[])
  14. {
  15. int sock_fd, len, newsock;
  16. struct sockaddr_in server, client;
  17.  
  18. if(argc < 2)
  19. {
  20. printf("Too few arguments. \nUsage : myftpd <port>\n");
  21. exit(1);
  22. }
  23.  
  24. int port = atoi(argv[1]);
  25.  
  26. sock_fd = socket(AF_INET , SOCK_STREAM , 0);
  27.  
  28. if (sock_fd == -1)
  29. {
  30. printf("Could not create socket!\n");
  31. }
  32.  
  33. //Prepare the sockaddr_in structure
  34. server.sin_family = AF_INET;
  35. server.sin_addr.s_addr = INADDR_ANY;
  36. server.sin_port = htons(port);
  37.  
  38. //Bind
  39. if( bind(sock_fd,(struct sockaddr *)&server , sizeof(server)) < 0)
  40. {
  41. printf("Server bind failed!\n");
  42. }
  43.  
  44. listen(sock_fd , QUEUE);
  45. printf("Waiting for SYN\n");
  46.  
  47. while (newsock = accept(sock_fd, (struct sockaddr *)&client, (socklen_t*)&len)){
  48. printf("New connection ....\n");
  49. fileops(newsock);
  50. }
  51. if (newsock < 0 ){
  52. printf("Something went wrong\n");
  53. }
  54. printf("File sent. So long, and thanks for the fish... \n");
  55. close(sock_fd);
  56. close(newsock);
  57. }
  58.  
  59. void fileops(int newsock)
  60. {
  61. FILE *fp;
  62. char *file = "oven.f";
  63. char buffr[BUFFLENGTH];
  64. int readsize;
  65.  
  66. fp = fopen(file, "r");
  67.  
  68. if(fp == NULL)
  69. {
  70. printf("File 404'd \n");
  71. return;
  72. }
  73.  
  74. bzero(buffr, BUFFLENGTH);
  75. while((readsize = fread(buffr, sizeof(char), BUFFLENGTH, fp)) > 0)
  76. {
  77. if((send(newsock, buffr, readsize, 0)) < 0)
  78. {
  79. printf("Send to client failed.\n");
  80. }
  81. printf("Size = %d\n", readsize);
  82. printf("Items = %c\n", buffr[2]);
  83. bzero(buffr, BUFFLENGTH);
  84. }
  85. return;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement