Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <ws2tcpip.h>
- #include <WinSock2.h>
- #pragma comment(lib, "ws2_32.lib")
- int main() {
- WSADATA wsaData;
- int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- printf("WSAStartup failed: %d\n", iResult);
- return 1;
- }
- SOCKET lsn_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (lsn_socket == INVALID_SOCKET) {
- printf("Error creating listener socket! Error: %d", WSAGetLastError());
- WSACleanup();
- return -1;
- }
- sockaddr_in sock_in;
- sock_in.sin_family = AF_INET;
- sock_in.sin_port = htons(2137);
- sock_in.sin_addr.S_un.S_addr = INADDR_ANY;
- bind(lsn_socket, (sockaddr*)&sock_in, sizeof(sock_in));
- listen(lsn_socket, SOMAXCONN);
- sockaddr_in client_in;
- int clientSize = sizeof(client_in);
- SOCKET clientSock = accept(lsn_socket, (sockaddr*)&client_in, &clientSize);
- if (clientSock == SOCKET_ERROR) {
- std::cerr << "Error accept socket! " << WSAGetLastError() << std::endl;
- closesocket(lsn_socket);
- WSACleanup();
- return 2;
- }
- char host[1024];
- char serv[32];
- if (getnameinfo((sockaddr*)&sock_in, sizeof(sock_in), host, 1024, serv, 32, 0) == 0) {
- printf("Host: %s polaczony na porcie: %s", host , serv);
- }
- else {
- inet_ntop(AF_INET, &sock_in.sin_addr, host, NI_MAXHOST);
- std::cout << "Host: " << host << " polaczony na porcie:" << ntohs(sock_in.sin_port) << std::endl;
- }
- closesocket(lsn_socket);
- bool clientClose = false;
- char fileRequested[260];
- const int fileAvailable = 200;
- const int fileNotfound = 404;
- const int BUFFER_SIZE = 1024;
- char bufferFile[BUFFER_SIZE];
- std::ifstream file;
- do {
- memset(fileRequested, 0, FILENAME_MAX);
- int byRecv = recv(clientSock, fileRequested, FILENAME_MAX, 0);
- if (byRecv == 0 || byRecv == -1) {
- // error receive data - break loop
- clientClose = true;
- }
- // open file
- file.open(fileRequested, std::ios::binary);
- if (file.is_open()) {
- // file is available
- int bySendinfo = send(clientSock, (char*)&fileAvailable, sizeof(int), 0);
- if (bySendinfo == 0 || bySendinfo == -1) {
- // error sending data - break loop
- clientClose = true;
- }
- // get file size
- file.seekg(0, std::ios::end);
- long fileSize = file.tellg();
- // send filesize to client
- bySendinfo = send(clientSock, (char*)&fileSize, sizeof(long), 0);
- if (bySendinfo == 0 || bySendinfo == -1) {
- // error sending data - break loop
- clientClose = true;
- }
- file.seekg(0, std::ios::beg);
- // read file with do-while loop
- do {
- // read and send part file to client
- file.read(bufferFile, BUFFER_SIZE);
- if (file.gcount() > 0)
- bySendinfo = send(clientSock, bufferFile, file.gcount(), 0);
- if (bySendinfo == 0 || bySendinfo == -1) {
- // error sending data - break loop
- clientClose = true;
- break;
- }
- } while (file.gcount() > 0);
- file.close();
- }
- else {
- // Can't open file or file not found
- int bySendCode = send(clientSock, (char*)&fileNotfound, sizeof(int), 0);
- if (bySendCode == 0 || bySendCode == -1) {
- // error sending data - break loop
- clientClose = true;
- }
- }
- } while (!clientClose);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement