Добавил:
Kaz
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <winsock.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <fstream>
#include <ctime>
#pragma comment (lib, "ws2_32")
#define MAX_CONNECTION_COUNT 2
#define MESSAGE_LENGTH 1024 * 4
#define IP "192.168.1.2"
#define PORT 10001
using namespace std;
long sended = 0;
void Initialization()
{
WSADATA WsaDat;
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
printf("WSA Initialization failed. Error code: %d", WSAGetLastError());
}
SOCKET CreateSocket()
{
SOCKET sock;
if (INVALID_SOCKET == (sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP) ) )
{
printf("Create socket error. Error code: %d", WSAGetLastError());
return NULL;
}
else return sock;
}
void BindSocket(SOCKET sock)
{
sockaddr_in name;
ZeroMemory (&name, sizeof (name));
name.sin_family = AF_INET;
name.sin_addr.S_un.S_addr = inet_addr (IP);
name.sin_port = htons(PORT);
if (SOCKET_ERROR == bind (sock, (sockaddr* ) &name, sizeof (name)))
printf("Bind socket error. Error code: %d", WSAGetLastError());
}
void ListenSocket(SOCKET sock)
{
if (listen(sock, MAX_CONNECTION_COUNT) != 0)
{
printf("Listen socket error. Error code: %d", WSAGetLastError());
return;
}
}
SOCKET AcceptSocket(SOCKET listenSocket)
{
sockaddr_in clientData;
ZeroMemory(&clientData, sizeof (clientData));
int lengthData = sizeof (clientData);
SOCKET clientSocket = accept (listenSocket, (sockaddr* ) &clientData, &lengthData);
if(clientSocket == INVALID_SOCKET )
{
printf("Accept socket error. Error code: %d", WSAGetLastError());
return NULL;
}
else return clientSocket;
}
bool SendData(SOCKET clientSocket, char* serverMessage)
{
if(send(clientSocket, serverMessage, strlen(serverMessage), 0) == SOCKET_ERROR)
{
closesocket(clientSocket);
return false;
}
return true;
}
int GetFileData(SOCKET sock,string fileName, long position)
{
std::ifstream file(fileName, ios::in | ios::binary);
if (!file.is_open())
return NULL;
char memblock[MESSAGE_LENGTH+1];
file.seekg (position, ios::beg);
file.read (memblock, MESSAGE_LENGTH);
long readed = file.gcount();
file.close();
memblock[readed] = '\0';
if(readed > 0)
{
if(send(sock, memblock, readed, 0) == SOCKET_ERROR)
{
closesocket(sock);
return 1;
}
sended += readed;
return 0;
}
else
{
if(position == 0)
{
SendData(sock, "404 FILE NOT FOUND\r\n\0");
return 4;
}
SendData(sock, "102 ALL SENDED\r\n\0");
return 1;
}
return 0;
}
int ParseRequest(SOCKET clientSocket, string message)
{
if(message.length() < 1)
{
return 4;
}
string request[3] = {"","",""};
int index1 = 0, index2 = 0, i = 0;
while(1)
{
index2 = message.find("\n\r", index2);
if(index2 > 0 && index2 < message.length())
{
request[i] = message.substr(index1 ,index2 - index1);
index2 += 2;
index1 = index2;
i++;
if(i == 3) break;
}
}
srand(time(0));
int is_oob = rand() % 2;
if(is_oob == 0)
{
send(clientSocket, "a", 1, MSG_OOB);
printf("OOB Send, bytes sent: %ld \n", sended);
return 0;
}
if(request[0].compare("GET") == 0)
{
long position = atol(request[2].c_str());
return GetFileData(clientSocket,request[1], position);
}
else
{
SendData(clientSocket, "403 WRONG REQUEST\r\n\0");
return 4;
}
return 0;
}
string ReceiveData(SOCKET clientSocket)
{
char* tmpMessage = new char[MESSAGE_LENGTH];
int messageLength;
if ((messageLength = recv (clientSocket, tmpMessage, MESSAGE_LENGTH, 0)) == -1 )
{
printf("Recive message error. Error code: %d\n", WSAGetLastError());
return string();
}
string response(tmpMessage,messageLength);
delete []tmpMessage;
return response;
}
void ShowConnectInfo(SOCKET sock)
{
sockaddr_in addr;
int addrlen = sizeof(sockaddr);
getsockname(sock, (sockaddr*)&addr, &addrlen);
printf ("Connected: %s:%ld\n", inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));
}
int main(int argc, char *argv[])
{
do
{
printf ("Wait for incoming connections... ");
Initialization();
SOCKET listenSocket = CreateSocket();
if(listenSocket != NULL)
{
BindSocket(listenSocket);
ListenSocket(listenSocket);
SOCKET clientSocket = AcceptSocket(listenSocket);
sended = 0;
if(clientSocket != NULL)
{
ShowConnectInfo(clientSocket);
while(ParseRequest(clientSocket, ReceiveData(clientSocket)) == 0);
}
closesocket (listenSocket);
}
closesocket (listenSocket);
WSACleanup();
}while(1);
return 0;
}