Code:
//------------server.cpp---------------
#include "serverInit.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>//for sleep()
#include <iostream>
using std::cout;
using std::endl;
using std::fstream;
using std::ios;
#define MAX_PATH 256
void dostuff(int); /* function prototype */
//int UploadFile(char* PathofFile);
void error(const char *msg)
{
perror(msg); exit(1);
}
int main(int argc, char *argv[]){
char PathofFile[MAX_PATH];
int sConnect, sListen, portno, pid;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
if (argc < 2) { cout << "ERROR, no port provided" << endl; //fprintf(stderr,"ERROR, no port provided\n"); exit(1);
}
sConnect = socket(AF_INET, SOCK_STREAM, 0);
sListen = socket(AF_INET, SOCK_STREAM, 0);
if (sConnect < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sListen, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sListen,SOMAXCONN);//5);
clilen = sizeof(cli_addr);
for(;; sleep(250)){
if(sConnect = accept(sListen, (struct sockaddr *) &cli_addr, &clilen))
{ cout << "Connection Established!" << endl;
gets(PathofFile);
UploadFile(PathofFile);
}
}
return 0;
}//End of Main
//------serverInit.h---------
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fstream>
#include <iostream>
using std::cout;
using std::endl;
using std::ifstream;
int sConnect, sListen;
using std::ios;
int UploadFile(char* PathofFile){
char* buffer;
int size;
ifstream file;
file.open(PathofFile, ios::in | ios::binary | ios::ate);
if(file.is_open()) {
file.seekg(0, ios::end);
size = file.tellg();
file.seekg(0, ios::beg);
buffer = new char[size];
file.read(buffer, size);
//send(sConnect, buffer, size, 0);
char buff[size]; // the buffer you want to send
// keep sending bits of buffer until it's all gone.
ssize_t n, remaining = size;
char *bp = buff;
while ( remaining > 0 && (n = send(sConnect, bp, remaining, 0)) > 0 ) {
bp += n;
remaining -= n;
}
file.close();
}return 0;
}
//--------client.cpp-------------
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>//for sleep()
#include <iostream>
#include <fstream>
using std::ofstream;
using std::cout;
using std::endl;
using std::ios;
void error(const char *msg){
perror(msg); exit(0);}
int main(int argc, char *argv[]){
int sConnect, portno, n;
int WS, errorCode;
char buffer[1000000];
struct sockaddr_in serv_addr;
struct hostent *server; //char buffer[256];
if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); }
portno = atoi(argv[2]);
sConnect = socket(AF_INET, SOCK_STREAM, 0);
if (sConnect < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) { cout << "ERROR, no such host" << endl; exit(0); }
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
for(;; sleep(250)){
if (connect(sConnect,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting");
if(errorCode != -1){//SOCKET_ERROR){
//WS = recv(sConnect, buffer, sizeof(buffer), 0);
int SIZE;
char buff[SIZE]; // the buffer you want to send
// keep sending bits of buffer until it's all gone.
ssize_t n, remaining = SIZE;
char *bp = buff;
while ( remaining > 0 && (n = recv(sConnect, bp, remaining, 0)) > 0 ) {
bp += n;
remaining -= n;
}
ofstream file;
file.open("test.jpeg", ios::out | ios::binary | ios::ate);
file.write(buffer, sizeof(buffer));
file.close();
}
}return 0;}//End of Main