Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/mman.h>
- #include <time.h>
- #include <stdio.h>
- #include <time.h>
- #include <sys/time.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <inttypes.h>
- volatile int sum = 0;
- int main(int argc, char *argv[]) {
- int fd = socket(PF_INET, SOCK_STREAM, 0);
- if (fd < 0) {
- perror("socket");
- return 1;
- }
- // следующие два системных вызова предписывают освободить серверный порт
- // немедленно по окончании работы процесса
- int val = 1;
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
- setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
- // привязываем сокет к порту
- // входящий адрес - любой
- // номер порта - argv[1]
- struct sockaddr_in s1;
- s1.sin_family = AF_INET;
- s1.sin_port = htons(strtol(argv[1], NULL, 10));
- s1.sin_addr.s_addr = INADDR_ANY;
- int r = bind(fd, (struct sockaddr*) &s1, sizeof(struct sockaddr_in));
- if (r < 0) {
- perror("bind");
- return 1;
- }
- // переключаем сокет в режим прослушивания
- listen(fd, 5);
- while (1) {
- // получаем новое подключение
- struct sockaddr_in s2;
- socklen_t slen = sizeof(s2); // обратие внимание, что именно socklen_t, не size_t!
- int fd2 = accept(fd, (struct sockaddr*) &s2, &slen);
- int tmp = read(fd2, &tmp, sizeof(tmp));
- sum += tmp;
- if (tmp == 0) {
- close(fd2);
- break;
- }
- close(fd2);
- }
- printf("%d\n", sum);
- fflush(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement