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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
#include <string.h>
#include <arpa/inet.h>
#include <iostream>
#include <fstream>
#include <errno.h>
#include<vector>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int main(int argc, char const *argv[])
{
int s, new_socket, valread;
struct sockaddr_in address;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket failed");
exit(1);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// attach socket to port
if (bind(s,(struct sockaddr *)&address , sizeof(address)) < 0)
{
perror("bind failed");
exit(1);
}
listen(s, 3);
if(new_socket= accept(s, (struct sockaddr *) 0, 0))
{puts("Connection accepted");}
char buff[24] ;
char *msg=(char*)"hi from server";
int received_bytes = 0;
int remaining_bytes = sizeof(buff);
send(new_socket , msg , strlen(msg) , 0 );
printf("Message sent ");
while((remaining_bytes > 0) ){
if (new_socket == -1)
{
perror("accept");
exit(1);
}
else
{
int res = recv(new_socket , &buff[received_bytes],remaining_bytes,0);
received_bytes += res;
remaining_bytes -= res;
}
}printf("%s\n\n",buff);
//send image sizes to client//
const char *dir = "/home/Desktop/pics";
if (chdir(dir) == -1) {
perror(dir);
return 1;
}
DIR *dirp = opendir(".");
for (struct dirent *dent; (dent = readdir(dirp)) != NULL; )
{
const char *nm = dent->d_name;
if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
continue;
struct stat file_stats;
if (stat(nm, &file_stats) == -1)
perror(nm);
else
printf("%9u: %s\n", (unsigned)file_stats.st_size, nm);
char folder[(unsigned)file_stats.st_size];
sprintf(folder, "home/Desktop/pics/%s", nm );
cout<<"here is the folder ------"<<folder<<endl;
long converted_number = htonl((unsigned)file_stats.st_size);
char buff_1[8] ;
int buff_1_size=sizeof(buff_1);
int i;
//for (i=0; i<=buff_1_size; i++)
//{
//if ((converted_number > 0))
//{
//int sb=send(new_socket, &converted_number, sizeof(converted_number), 0);
send(new_socket, &converted_number, sizeof(converted_number), 0);
//cout<<"bytes sent are ===="<<sb<<endl;
//}
//}
ifstream stream(folder, std::ios::in | std::ios::binary);
vector<char> imageDataVec((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
cout << "Size: " << imageDataVec.size() << " bytes";
long conv_num= htonl(imageDataVec.size());
//send(new_socket, &converted_number, sizeof(converted_number), 0);
//send(new_socket, &imageDataVec, imageDataVec.size() , 0);
size_t sent{};
while (sent < imageDataVec.size()) {
// send(new_socket, &converted_number, sizeof(conv_num), 0);
int nbytes = send(new_socket, &imageDataVec[sent], imageDataVec.size() - sent, 0);
if (nbytes <= 0) {
std::clog << "error: while sending image\n";
break;
sent += nbytes;
cout<<nbytes<<"================"<<endl;
}
closedir(dirp);
}
}
// send_image(new_socket);
close(s);
close(new_socket);
return 0;
}
| |