#include <sys/fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER_PORT 8080 /* По договоренности между клиентом и сервером */
#define BUF_SIZE 4096 /* Размер передаваемых блоков */
#define QUEUE_SIZE 10
int main(int argc, char *argv[])
{ int s, b, l, fd, sa, bytes, on = 1;
char buf[BUF_SIZE]; /* буфер для исходящего файла */
struct sockaddr_in channel; /* содержит IP-адрес */
/* Создать структуру адреса для привязки к сокету */
memset(&channel, 0, sizeof(channel)); /* Обнуление channel */
channel.sin_family = AF_INET;
channel.sin_addr.s_addr = htonl(INADDR_ANY);
channel.sin_port = htons(SERVER_PORT);
/* Пассивный режим. Ожидание соединения */
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* Создать сокет */
if (s <0) {printf("сбой вызова сокета"); exit(-1);}
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));
b = bind(s, (struct sockaddr *) &channel, sizeof(channel));
if (b < 0) {printf("сбой связывания"); exit(-1);}
l = listen(s, QUEUE_SIZE); /* Определение размера очереди */
if (l < 0) {printf("сбой ожидания"); exit(-1);}
/* Теперь сокет установлен и привязан. Ожидание и обработка соединения. */
while (1) {
sa = accept(s, 0, 0); /* Блокировка в ожидании запроса соединения */
if (sa < 0) {printf("сбой доступа"); exit(-1);}
read(sa, buf, BUF_SIZE); /* Считать имя файла из сокета */
/* Получить и возвратить файл. */