1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
//
// main.cpp
// UDPFtpClient
//
// Created by on 6/16/13.
// Copyright (c) 2013 . All rights reserved.
//
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <time.h>
#include <string>
#include <cstring>
#include <unistd.h>
#include <vector>
#include <sstream>
#include <fstream>
#include <poll.h>
#include <cmath>
#define BUFF_SIZE 1500
using namespace std;
int client_socket;
struct sockaddr_in destinationaddr;
//Loading the picture into binary format enough, have a buffer to hold the file.
int splitFile(vector<char> picture){
vector<vector<char>> packets;
int current = 0;
int packet_count = 0;
int total_packets;
bool first_time = true;
while (current != picture.size()){
if(packet_count == 0 && first_time){
vector<char> new_packet;
packets.push_back(new_packet);
first_time = false;
}
if(packets[packet_count].size() == BUFF_SIZE){
vector<char>new_packet;
packets.push_back(new_packet);
packet_count++;
}
packets[packet_count].push_back(picture[current]);
current++;
}
cout << "Total Packets Size: " << picture.size() << endl;
total_packets = int(packets.size());
for(int i=0; i<packets.size(); i++){
char packet_send[BUFF_SIZE];
for(int b=0; b<packets[i].size(); b++){
packet_send[b] = packets[i][b];
}
cout << "Length of packet: " << strlen(packet_send) << endl;
sendto(client_socket, packet_send, strlen(packet_send), 0, (struct sockaddr*)&destinationaddr, sizeof(destinationaddr));
}
char* packet_send = "COMPLETE\0";
sendto(client_socket, packet_send, strlen(packet_send), 0, (struct sockaddr*)&destinationaddr, sizeof(destinationaddr));
cout << "File Transfer Complete" << endl;
return 0;
}
int makeCopy(vector<char> picture){
ofstream new_file("new_picture.jpg", ios::binary);
new_file.write (&picture[0],picture.size());
cout << picture.size();
return 0;
}
int loadPicture(string picture){
fstream new_picture;
fstream::pos_type fileSize;
new_picture.open(picture, ios::in | ios::binary | ios::ate);
if(new_picture.is_open()){
vector <char> fileContents;
fileContents.resize(new_picture.tellg());
new_picture.seekg(0, ios::beg);
if(!new_picture.read(&fileContents[0], fileContents.size())){
cout << "Fail to read" << endl;
return 0;
}
splitFile(fileContents);
new_picture.close();
return 1;
}
cout << "Could not open file!" << endl;
return 0;
}
int main(int argc, const char * argv[])
{
bzero(&destinationaddr, sizeof(destinationaddr));
destinationaddr.sin_family = AF_INET;
destinationaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
destinationaddr.sin_port=htons(6666);
client_socket = socket(AF_INET, SOCK_DGRAM, 0);
loadPicture("real_iphone_picture.png");
return 0;
}
|