socket.c 480 B

123456789101112131415161718192021222324252627282930
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. int main() {
  8. int s;
  9. struct sockaddr_in sin;
  10. s = socket(AF_INET, SOCK_STREAM, 0);
  11. if (s == -1) {
  12. perror("socket");
  13. return 1;
  14. }
  15. sin.sin_family = AF_INET;
  16. sin.sin_addr.s_addr = INADDR_ANY;
  17. sin.sin_port = htons(80);
  18. if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
  19. perror("bind");
  20. return 1;
  21. }
  22. close(s);
  23. return 0;
  24. }