2011-04-09 19:55:52 +04:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
2011-05-02 12:11:12 +04:00
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <fcntl.h>
|
2011-05-02 17:47:50 +04:00
|
|
|
|
#include <string.h>
|
2011-05-02 12:11:12 +04:00
|
|
|
|
|
|
|
|
|
#include "xerror.h"
|
2011-05-02 16:59:54 +04:00
|
|
|
|
#include "netfuncs.h"
|
2011-05-02 12:11:12 +04:00
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
#define NUM_THREADS 1
|
|
|
|
|
#define MAX_CONNECTIONS 1
|
2011-05-02 12:11:12 +04:00
|
|
|
|
|
|
|
|
|
void *serv_request(void *data)
|
2011-04-09 19:55:52 +04:00
|
|
|
|
{
|
2011-05-02 12:11:12 +04:00
|
|
|
|
struct connection_cb {
|
|
|
|
|
int dataSocket;
|
|
|
|
|
char data[256];
|
|
|
|
|
int dataSent;
|
|
|
|
|
int dataToSend;
|
|
|
|
|
int isReading;
|
|
|
|
|
struct connection_cb *next;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct connection_cb *connections = NULL;
|
|
|
|
|
|
2011-05-02 21:51:34 +04:00
|
|
|
|
int listenSocket = *(int *)data; // дескриптор сокета передаётся через аргумент pthread
|
|
|
|
|
if (fcntl(listenSocket, F_SETFL, O_NONBLOCK) < 0) // делает сокет неблокирующим
|
2011-05-02 12:11:12 +04:00
|
|
|
|
die("fcntl()", errno);
|
|
|
|
|
|
|
|
|
|
while (1) {
|
2011-05-02 21:51:34 +04:00
|
|
|
|
fd_set readFdSet; // набор дескрипторов сокетов, с кт. в данный момент идёт чтение
|
|
|
|
|
fd_set writeFdSet; // набор дескрипторов сокетов, в кт. в данный момент идёт запись
|
|
|
|
|
struct connection_cb *currentConn, **currentConnPtr, *tempConn; // выбранное соединение, ???, ???
|
|
|
|
|
int maxFdNum; // ???
|
2011-05-02 12:11:12 +04:00
|
|
|
|
|
2011-05-02 21:51:34 +04:00
|
|
|
|
FD_ZERO(&readFdSet); // очистка набора дескрипторов чтения
|
|
|
|
|
FD_ZERO(&writeFdSet); // очистка набора дескрипторов записи
|
2011-05-02 12:11:12 +04:00
|
|
|
|
|
|
|
|
|
/*
|
2011-05-02 21:51:34 +04:00
|
|
|
|
* Добавление дескриптора к множеству readFdSet
|
2011-05-02 12:11:12 +04:00
|
|
|
|
*/
|
2011-05-02 21:51:34 +04:00
|
|
|
|
FD_SET(listenSocket, &readFdSet); // слушающий порт добавляется в список читающих (сюда же входит connect())
|
|
|
|
|
maxFdNum = listenSocket; // ??? не разобрался
|
2011-05-02 12:11:12 +04:00
|
|
|
|
|
2011-05-02 21:51:34 +04:00
|
|
|
|
/*
|
|
|
|
|
* Разделение дескрипторов м/у множествами readFdSet и writeFdSet
|
|
|
|
|
*/
|
2011-05-02 12:11:12 +04:00
|
|
|
|
for (currentConn = connections; currentConn != NULL; currentConn = currentConn->next) {
|
|
|
|
|
if (currentConn->isReading)
|
|
|
|
|
FD_SET(currentConn->dataSocket, &readFdSet);
|
|
|
|
|
else
|
|
|
|
|
FD_SET(currentConn->dataSocket, &writeFdSet);
|
|
|
|
|
maxFdNum = currentConn->dataSocket > maxFdNum ? currentConn->dataSocket : maxFdNum;
|
|
|
|
|
}
|
2011-05-02 21:51:34 +04:00
|
|
|
|
|
2011-05-02 12:11:12 +04:00
|
|
|
|
/*
|
2011-05-02 21:51:34 +04:00
|
|
|
|
* Получение множества дескрипторов сокетов для обработки (ожидание изменения состояния на сокетах)
|
2011-05-02 12:11:12 +04:00
|
|
|
|
*/
|
|
|
|
|
if (select(maxFdNum + 1, &readFdSet, &writeFdSet, NULL, NULL) < 0) {
|
|
|
|
|
if (errno == EINTR)
|
|
|
|
|
continue;
|
|
|
|
|
die("select()", errno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentConnPtr = &connections;
|
|
|
|
|
|
|
|
|
|
while (*currentConnPtr != NULL) {
|
|
|
|
|
|
|
|
|
|
/*
|
2011-05-02 21:51:34 +04:00
|
|
|
|
* Проверка принадлежности дескриптора
|
|
|
|
|
* (*currentConnPtr)->dataSocket к множеству readFdSet
|
2011-05-02 12:11:12 +04:00
|
|
|
|
*/
|
|
|
|
|
if ((*currentConnPtr)->isReading && FD_ISSET((*currentConnPtr)->dataSocket, &readFdSet)) {
|
|
|
|
|
int result = recv((*currentConnPtr)->dataSocket, (*currentConnPtr)->data,
|
|
|
|
|
sizeof((*currentConnPtr)->data), 0);
|
|
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
|
if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
|
|
|
|
|
bark("recv()", errno);
|
|
|
|
|
close((*currentConnPtr)->dataSocket);
|
|
|
|
|
tempConn = *currentConnPtr;
|
|
|
|
|
*currentConnPtr = (*currentConnPtr)->next;
|
|
|
|
|
free(tempConn);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else if (result == 0) {
|
|
|
|
|
close((*currentConnPtr)->dataSocket);
|
|
|
|
|
tempConn = *currentConnPtr;
|
|
|
|
|
*currentConnPtr = (*currentConnPtr)->next;
|
|
|
|
|
free(tempConn);
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
(*currentConnPtr)->dataToSend = result;
|
|
|
|
|
(*currentConnPtr)->dataSent = 0;
|
|
|
|
|
(*currentConnPtr)->isReading = 0;
|
2011-05-02 16:59:54 +04:00
|
|
|
|
printf("Recieving as Slave Thread id = '%d' \n", (int)pthread_self());
|
2011-05-02 12:11:12 +04:00
|
|
|
|
}
|
|
|
|
|
} else
|
2011-05-02 21:51:34 +04:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Проверка принадлежности дескриптора
|
|
|
|
|
* (*currentConnPtr)->dataSocket к множеству writedFdSet
|
|
|
|
|
*/
|
2011-05-02 12:11:12 +04:00
|
|
|
|
if (FD_ISSET((*currentConnPtr)->dataSocket, &writeFdSet)) {
|
|
|
|
|
int result = send((*currentConnPtr)->dataSocket,
|
|
|
|
|
(*currentConnPtr)->data + (*currentConnPtr)->dataSent,
|
|
|
|
|
(*currentConnPtr)->dataToSend - (*currentConnPtr)->dataSent, 0);
|
|
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
|
if (errno != EINTR && errno != EAGAIN) {
|
|
|
|
|
bark("write()", errno);
|
|
|
|
|
close((*currentConnPtr)->dataSocket);
|
|
|
|
|
tempConn = *currentConnPtr;
|
|
|
|
|
*currentConnPtr = (*currentConnPtr)->next;
|
|
|
|
|
free(tempConn);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
(*currentConnPtr)->dataSent += result;
|
|
|
|
|
|
|
|
|
|
if ((*currentConnPtr)->dataSent >= (*currentConnPtr)->dataToSend)
|
|
|
|
|
(*currentConnPtr)->isReading = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentConnPtr = &((*currentConnPtr)->next);
|
2011-05-02 16:59:54 +04:00
|
|
|
|
printf("Sending as Slave Thread id = '%d' \n", (int)pthread_self());
|
2011-05-02 12:11:12 +04:00
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
Проверка принадлежности дескриптора listenSocket
|
|
|
|
|
к множеству readFdSet,т.е. необходимости обработать
|
2011-05-02 21:51:34 +04:00
|
|
|
|
вызов connect() от нового клиента.
|
2011-05-02 12:11:12 +04:00
|
|
|
|
*/
|
|
|
|
|
if (FD_ISSET(listenSocket, &readFdSet)) {
|
|
|
|
|
while (1) {
|
|
|
|
|
int result = accept(listenSocket, (struct sockaddr *)NULL, NULL);
|
2011-05-02 19:21:54 +04:00
|
|
|
|
|
2011-05-02 12:11:12 +04:00
|
|
|
|
if (result < 0) {
|
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
|
break;
|
|
|
|
|
die("accept()", errno);
|
|
|
|
|
} else {
|
|
|
|
|
*currentConnPtr = malloc(sizeof(struct connection_cb));
|
|
|
|
|
if (*currentConnPtr == NULL)
|
|
|
|
|
die("malloc()", 0);
|
|
|
|
|
|
|
|
|
|
if (fcntl(result, F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
|
die("fcntl()", errno);
|
|
|
|
|
|
|
|
|
|
(*currentConnPtr)->dataSocket = result;
|
|
|
|
|
(*currentConnPtr)->isReading = 1;
|
|
|
|
|
(*currentConnPtr)->next = 0;
|
|
|
|
|
currentConnPtr = &((*currentConnPtr)->next);
|
2011-05-02 16:59:54 +04:00
|
|
|
|
printf("Accepting as Master Thread id = '%d' \n", (int)pthread_self());
|
2011-05-02 12:11:12 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
/*
|
|
|
|
|
* Установка слушающего сокета на порту port
|
|
|
|
|
*/
|
2011-05-02 12:11:12 +04:00
|
|
|
|
int getServerSocket(unsigned short int port)
|
|
|
|
|
{
|
|
|
|
|
int listenSocket;
|
|
|
|
|
struct sockaddr_in listenSockaddr;
|
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// создание сокета
|
2011-05-02 12:11:12 +04:00
|
|
|
|
if ((listenSocket = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
|
|
|
die("socket()", errno);
|
2011-05-02 19:21:54 +04:00
|
|
|
|
|
|
|
|
|
// задание адреса сокета и его параметров
|
2011-05-02 12:11:12 +04:00
|
|
|
|
memset(&listenSockaddr, 0, sizeof(listenSockaddr));
|
|
|
|
|
listenSockaddr.sin_family = PF_INET;
|
|
|
|
|
listenSockaddr.sin_port = htons(port);
|
|
|
|
|
listenSockaddr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// привязка сокета listenSocket к адресу listenSockaddr
|
2011-05-02 12:11:12 +04:00
|
|
|
|
if (bind(listenSocket, (struct sockaddr *)&listenSockaddr, sizeof(listenSockaddr)) < 0)
|
|
|
|
|
die("bind()", errno);
|
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// установка очереди входящих соединений в MAX_CONNECTIONS
|
|
|
|
|
if (listen(listenSocket, MAX_CONNECTIONS) < 0)
|
2011-05-02 12:11:12 +04:00
|
|
|
|
die("listen()", errno);
|
|
|
|
|
|
|
|
|
|
return listenSocket;
|
2011-04-09 19:55:52 +04:00
|
|
|
|
}
|
2011-05-02 12:11:12 +04:00
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
/*
|
|
|
|
|
* Основная процедура создания слушающего сокета
|
|
|
|
|
* и нитей, обрабатывающих входящие соединения
|
|
|
|
|
*/
|
2011-05-02 12:11:12 +04:00
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int k;
|
|
|
|
|
int descSock;
|
2011-05-02 19:21:54 +04:00
|
|
|
|
char *service = "3425";
|
2011-05-02 12:11:12 +04:00
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// порт в argv[1]
|
2011-05-02 12:11:12 +04:00
|
|
|
|
switch (argc) {
|
|
|
|
|
case 1:
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
service = argv[1];
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("Usage: ./echo-server [port]\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// NUM_THREADS нитей
|
2011-05-02 12:11:12 +04:00
|
|
|
|
pthread_t p_thread[NUM_THREADS];
|
|
|
|
|
pthread_attr_t attr;
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// инициализация аттрибутов нитей
|
2011-05-02 12:11:12 +04:00
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
|
size_t stacksize = 512;
|
|
|
|
|
pthread_attr_setstacksize(&attr, stacksize);
|
|
|
|
|
pthread_attr_getstacksize(&attr, &stacksize);
|
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// создание слушающего сокета
|
2011-05-02 12:11:12 +04:00
|
|
|
|
descSock = getServerSocket(atoi(service));
|
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// создание NUM_THREADS нитей
|
2011-05-02 12:11:12 +04:00
|
|
|
|
for (k = 0; k < NUM_THREADS; k++) {
|
2011-05-02 16:59:54 +04:00
|
|
|
|
pthread_create(&p_thread[k], &attr, serv_request, (void *)&descSock);
|
2011-05-02 12:11:12 +04:00
|
|
|
|
printf("Thread %d started\n", k);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// уничтожение объекта атрибутов
|
2011-05-02 12:11:12 +04:00
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
|
|
2011-05-02 19:21:54 +04:00
|
|
|
|
// ожидание завершения работы нитей
|
2011-05-02 12:11:12 +04:00
|
|
|
|
for (k = 0; k < NUM_THREADS; k++) {
|
|
|
|
|
pthread_join(p_thread[k], NULL);
|
|
|
|
|
printf("Completed join with thread %d\n", k);
|
|
|
|
|
}
|
|
|
|
|
}
|