fork download
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <time.h>
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. int listenfd = 0, connfd = 0;
  15. int yes =1;
  16. struct sockaddr_in serv_addr;
  17.  
  18. char sendBuff[1025];
  19.  
  20. if (listenfd = socket(PF_INET, SOCK_STREAM, 0)==-1){
  21. perror("listen");
  22. exit(-1);
  23. }
  24.  
  25. if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))==-1){
  26. perror("option");
  27. }
  28.  
  29. memset(&serv_addr, '0', sizeof(serv_addr));
  30. memset(sendBuff, '0', sizeof(sendBuff));
  31.  
  32. serv_addr.sin_family = AF_INET;
  33. serv_addr.sin_addr.s_addr = INADDR_ANY;
  34. serv_addr.sin_port = htons(52142);
  35.  
  36. if (bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1){
  37. perror("bind");
  38. exit(-1);
  39. }
  40.  
  41. listen(listenfd, 10);
  42.  
  43. while(1)
  44. {
  45. connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
  46. printf("Accepted\n");
  47. send(connfd,"Hallo",5,0);
  48. close (connfd);
  49. }
  50. close (listenfd);
  51. exit(0);
  52. }
Runtime error #stdin #stdout #stderr 0s 2288KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
option: Socket operation on non-socket
bind: Socket operation on non-socket