Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <string.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #define STR "Hello tcp world\n"
- #define pexit(x) \
- do { \
- perror(x); \
- exit(EXIT_FAILURE); \
- } while (0)
- static struct sockaddr_in saddr;
- int main(void)
- {
- int sock, clisock;
- int len = strlen(STR);
- sock = socket(AF_INET, SOCK_STREAM, 0);
- saddr.sin_family = AF_INET;
- saddr.sin_addr.s_addr = htonl(INADDR_ANY);
- saddr.sin_port = htons(80);
- if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)))
- pexit("bind");
- if (listen(sock, 10))
- pexit("listen");
- for (;;) {
- if ((clisock = accept(sock, NULL, NULL)) == -1) {
- perror("accept");
- continue;
- }
- if (send(clisock, STR, len, 0) != len)
- perror("send");
- close(clisock);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement