fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <fcntl.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #define serv_tp 6880
  11. #define serv_ha "127.0.0.1"
  12.  
  13. int main(){
  14. int sockfd,newsockfd,clilen,n,fd;
  15. struct sockaddr_in ca,sa;
  16. char filename[25],buf[1000];
  17. if((sockfd=socket(AF_INET,SOCK_STREAM,0)<0))
  18. printf("server not opened at all\n");
  19. printf("opened successfully\n");
  20. sa.sin_family=AF_INET;
  21. sa.sin_addr.s_addr=htonl(INADDR_ANY);
  22. sa.sin_port=htons(serv_tp);
  23.  
  24. if((bind,(sockfd,(struct sockaddr*)&sa,sizeof(sa)))<0)
  25. printf("not bind properly\n");
  26. else
  27. printf("bound properly\n");
  28. listen(sockfd,5);
  29. printf("waiting for the client\n");
  30. for(;;){
  31. clilen=sizeof(ca);
  32. newsockfd=accept(sockfd,(struct sockaddr*)&ca,&clilen);
  33. if(newsockfd<0)
  34. printf("server error\n");
  35. else
  36. printf("server connected successfully\n");
  37. n=read(newsockfd,filename,25);
  38. filename[n]='\0';
  39. printf("server is found at %s ready to tranfer\n",filename);
  40. fd=open(filename,O_RDONLY);
  41. n=read(fd,buf,1000);
  42. buf[n]='\0';
  43. write(newsockfd,buf,n);
  44. close(newsockfd);
  45. exit(0);
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
opened successfully
bound properly
waiting for the client
server error
server is found at / ready to tranfer