Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <winsock2.h>
- #include <string>
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #pragma comment(lib, "ws2_32.lib") //Winsock Library
- using namespace std;
- int main() {
- cout << "Connecting to port...";
- unsigned short int s_port;
- cout << "Enter server port: ";
- cin >> s_port;
- cout << "OK. Port: " << s_port << endl;
- cout << "Initialization...";
- WSADATA wsaData;
- int res = WSAStartup(MAKEWORD(2,2), &wsaData);
- if (res != NO_ERROR)
- cout << "ERROR. " << WSAGetLastError() << endl;
- cout << "OK. Initialized" << endl;
- cout << "Creating socket...";
- SOCKET clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (clientSocket == INVALID_SOCKET) {
- cout << "ERROR. Invalid socket!" << endl;
- return 0;
- }
- cout << "OK. Socket created." << endl;
- cout << "Binding...";
- struct sockaddr_in client;
- client.sin_family = AF_INET;
- client.sin_port = htons(s_port);
- client.sin_addr.s_addr = inet_addr("10.0.1.3");
- int clientSize = sizeof(client);
- if (bind(clientSocket, (struct sockaddr*) &client, clientSize) == SOCKET_ERROR) {
- cout << "ERROR. Binding not completed!" << endl;
- }
- cout << "OK. Socket binded" << endl;
- const int part_size = 1024;
- char *FilenameBuffer = new char[part_size];
- while (1) {
- memset(FilenameBuffer, '\0', part_size);
- cout << "Trying to recieve data...";
- int recv_len;
- if ((recv_len = recvfrom(clientSocket, FilenameBuffer, part_size, 0, (struct sockaddr *) &client, &clientSize)) == SOCKET_ERROR) {
- cout << "ERROR. Recvfrom() failed" << endl;
- return 0;
- }
- cout << "New client connected!" << endl;
- cout << "Received filename from " << inet_ntoa(client.sin_addr) << " : " << ntohs(client.sin_port) << endl;
- cout << "Bytes Recv: " << recv_len << endl;
- cout << "Recieved data (filename): " << FilenameBuffer << endl;
- cout << "transfer???????" << endl;
- struct stat si;
- if (stat(FilenameBuffer, &si)) {
- perror("stat");
- if (sendto(clientSocket, "File not found", 15, 0, (struct sockaddr *)&client, clientSize) == SOCKET_ERROR) {
- cout << "ERROR. Sending error!" << endl;
- return 0;
- }
- return 0;
- }
- cout << "Si: " << si.st_size << endl;
- string tmp = stoi(si.st_size);
- cout << "Si size: " << tmp.length() << endl; //????????
- char *buf = new char[temp.length()];
- char tmp[part_size];
- buf = itoa(si.st_size, tmp, 10);
- int t = sendto(clientSocket, buf2, temp.length(), 0, (struct sockaddr *)&client, clientSize);
- if (t == SOCKET_ERROR) {
- cout << "ERROR. Sending file size error!" << endl;
- return 0;
- }
- cout << "OK. " << inet_ntoa(client.sin_addr) << " send bytes " << t << " by " << t << endl;
- int parts_count = si.st_size / part_size;
- int last_part_size = si.st_size % part_size;
- cout << "Parts_count: " << parts_count << endl << "Last_part_size: " << last_part_size << endl; //?????????
- FILE *file;
- if ((file=fropen(FilenameBuffer, "rb")) == NULL) {
- cout << "ERROR. Open file error!" << endl;
- if (sendto(clientSocket, "File not found", 15, 0, (struct sockaddr *)&client, clientSize) == SOCKET_ERROR) {
- cout << "ERROR. Sending error!" << endl;
- return 0;
- }
- return 0;
- } else {
- char *buffer = new char[part_size];
- for (int i = 0; i < parts_count; i++) {
- memset(buffer, '\0', part_size);
- if (fread(buffer, 1, part_size, file) != part_size) {
- cout << "ERROR. Fread error!" << endl;
- return 0;
- }
- if (sendto(clientSocket, buffer, part_size, 0, (struct sockaddr *) &client, clientSize) == SOCKET_ERROR) {
- cout << "ERROR. Sending file error!" << endl;
- }
- Sleep(1); //?????
- }
- if (last_part_size) {
- if (fread(buffer, 1, last_part_size, file) != last_part_size) {
- cout << "ERROR. Fread error!" << endl;
- return 0;
- }
- if (sendto(clientSocket, buffer, last_part_size, 0, (struct sockaddr *) &client, clientSize) == SOCKET_ERROR) {
- cout << "ERROR. Fread error!" << endl;
- return 0;
- }
- }
- delete[] buffer;
- fclose(f);
- }
- cout << "End of sending file." << endl;
- }
- closesocket(clientSocket);
- WSACleanup();
- cout << "Socket closed! End of program working." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement