Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <netdb.h>
  7. #include <stdio.h>
  8.  
  9. int main (int argc, char** argv)
  10. {
  11. int fd,cfd,i;
  12. socklen_t l;
  13. struct sockaddr_in saddr, caddr;
  14.  
  15. fd = socket(PF_INET, SOCK_STREAM, 0);
  16.  
  17. //printf("socket: %d\n", cfd);
  18.  
  19. saddr.sin_family = PF_INET;
  20. saddr.sin_port = htons(1234);
  21. saddr.sin_addr.s_addr = INADDR_ANY;
  22.  
  23. i = bind(fd, (struct sockaddr*)&saddr, sizeof(saddr));
  24.  
  25. printf("bind: %d\n", i);
  26. i = listen(fd, 5);
  27. printf("listen: %d\n", i);
  28.  
  29. while(1){
  30. l = sizeof(caddr);
  31. cfd = accept(fd, (struct sockaddr*)&caddr, &l);
  32. printf("accept: %d\n", cfd);
  33. write (cfd, "Hello world!\n",14);
  34. close(cfd);
  35. }
  36.  
  37. close (fd);
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement