Code:
#include <sstream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <winsock.h>
using namespace std;
string IntToString(int num);
bool StringToInt(const string &s, int &i);
void removetext(char *txt, int size, int leave);
int main()
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
{
cout<<"Error, WSAStartup failed.\n";
return 0;
}
char UI = 'x';
char test = 'n';
char ui = 'x';
unsigned short int PORT = 21987;
int sockFD, newFD;
cout<<"test?\n(y)es/(n)o\n#";
cin>>test;
cout<<"\n(l)isten\n(c)onnect\ne(x)it\n#";
cin>>UI;
if(UI == 'x')
return 0;
if(UI == 'l')
{
sockaddr_in mAddr;
sockaddr_in tAddr;
sockFD = socket(AF_INET, SOCK_STREAM, 0);
if(sockFD == -1)
{
cout<<"\nError with call to socket()";
cin.get();
return 0;
}
mAddr.sin_family = AF_INET;
mAddr.sin_port = htons(PORT);
if(test == 'y')
{
mAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
}
else
{
mAddr.sin_addr.s_addr = INADDR_ANY;
}
memset(&(mAddr.sin_zero), '\0', 8);
if(bind(sockFD, (sockaddr *)&mAddr, sizeof(sockaddr_in)) == -1)
{
cout<<"\nError with call to bind()";
cin.get();
return 0;
}
if(listen(sockFD, 5) == -1)
{
cout<<"\nError with call to listen()";
cin.get();
return 0;
}
int sin_size = sizeof(sockaddr_in);
newFD = accept(sockFD, (sockaddr *)&tAddr, (int *) &sin_size);
if(newFD == -1)
{
cout<<"\nError with call to accept()";
cin.get();
return 0;
}
shutdown(sockFD,2);
char *msg = "Connection Established";
int len = strlen(msg);
int bytes_sent = send(newFD, msg, len, 0);
char rcv[22];
int bytes_rcv = recv(newFD, rcv, 22, 0);
cout<<rcv;
cout<<"\n\n(s)end file\n(r)ecieve file\n#";
cin>>ui;
if(ui == 's') //I ALWAYS RUN LISTEN-RECEIVE SO YOU
//CAN IGNORE EVERYTHING IN THIS IF STATEMENT
{
bool last = 0;
bool small = 0;
char buff[512];
int size;
int tot_sent = 0;
char file[16];
cout<<"Filename\n#";
cin>>file;
{
FILE *pFile;
pFile = fopen(file, "rb");
fseek(pFile, 0, SEEK_END);
size = ftell(pFile);
fclose(pFile);
}
if(size < 512)
{
last = 1;
small = 1;
}
string s = IntToString(size);
const char *itsSize = s.c_str();
len = strlen(itsSize);
bytes_sent = send(newFD, itsSize, len, 0);
len = strlen(file);
bytes_sent = send(newFD, file, len, 0);
ifstream fin(file);
while(tot_sent < size)
{
if(!last)
{
fin.get(buff, 512);
bytes_sent = send(newFD, buff, 512, 0);
tot_sent += bytes_sent;
}
if(last)
{
if(small)
{
char *lst;
fin.get(lst, size);
bytes_sent = send(newFD, lst, size, 0);
tot_sent = size;
}
else
{
int left = size - tot_sent;
char *lst;
fin.get(lst, left);
bytes_sent = send(newFD, lst, left, 0);
tot_sent = size;
}
}
if((size - tot_sent) < 512)
last = 1;
}
} //S
if(ui == 'r') //I ALWAYS RUN THIS
{
bool last = 0;
bool small = 0;
char file[20];
char buff[512];
int size;
int tot_recv = 0;
char fileSize[10];
cout<<"\n1";
bytes_rcv = recv(newFD, fileSize, 10, 0);
cout<<"\n2";
removetext(fileSize, 10, bytes_rcv);
cout<<"\n3 "<<fileSize;
string t;
cout<<"\n4";
t.assign(fileSize);
cout<<"\n5";
StringToInt(t, size);
cout<<"\n6";
cout<<"\nsize: "<<size;
if(size < 512)
{
last = 1;
small = 1;
}
bytes_rcv = recv(newFD, file, 20, 0);
cout<<"\nfilename (no remtext): "<<file;
removetext(file, 20, bytes_rcv);
cout<<"\nfilename (remtext): "<<file;
ofstream fout;
fout.open(file, ofstream::out | ofstream::trunc | ofstream::binary);
char rcv_byte[1];
int i = 0;
while(i < size)
{
bytes_rcv = recv(newFD, rcv_byte, 1, 0);
fout.write(rcv_byte, 1);
if(bytes_rcv == -1)
cout<<"\n-1"<<" "<<i;
tot_recv += bytes_rcv;
i++;
}
cout<<"\ni: "<<i;
cout<<"\ntot_recv: "<<tot_recv;
cout<<"\nsize: "<<size;
} //R
shutdown(newFD, 2);
}
if(UI == 'c')
{
sockaddr_in mAddr;
sockFD = socket(AF_INET, SOCK_STREAM, 0);
if(sockFD == -1)
{
cout<<"\nError with call to socket()";
cin.get();
return 0;
}
char cAddr[16];
cout<<"\nConnect to address\n#";
cin>>cAddr;
mAddr.sin_family = AF_INET;
mAddr.sin_port = htons(PORT);
mAddr.sin_addr.s_addr = inet_addr(cAddr);
memset(&(mAddr.sin_zero), '\0', 8);
if(connect(sockFD, (sockaddr *)&mAddr, sizeof(sockaddr)) == -1)
{
cout<<"\nError with call to connect()";
cin.get();
return 0;
}
char rcv[22];
int bytes_rcv = recv(sockFD, rcv, 22, 0);
if(bytes_rcv == -1)
{
cout<<"\nError with call to recv()";
cin.get();
return 0;
}
char *snd = "Connection Established";
int len = strlen(snd);
int bytes_snd = send(sockFD, snd, len, 0);
cout<<rcv;
int bytes_sent;
cout<<"\n\n(s)end file\n(r)ecieve file\n#";
cin>>ui;
if(ui == 's') //I ALWAYS RUN THIS
{
bool last = 0;
bool small = 0;
char buff[512];
int size;
int tot_sent = 0;
char file[16];
cout<<"Filename\n#";
cin>>file;
{
FILE *pFile;
pFile = fopen(file, "rb");
fseek(pFile, 0, SEEK_END);
size = ftell(pFile);
fclose(pFile);
}
if(size < 512)
{
last = 1;
small = 1;
}
string s = IntToString(size);
const char *itsSize = s.c_str();
len = strlen(itsSize);
bytes_sent = send(sockFD, itsSize, len, 0);
bytes_sent = send(sockFD, file, 20, 0);
ifstream fin(file);
cout<<"\n8";
char send_byte[1];
int i = 0;
while(i < size)
{
fin.read(send_byte, 1);
bytes_sent = send(sockFD, send_byte, 1, 0);
if(bytes_rcv == -1)
cout<<"\n-1"<<" "<<i;
tot_sent += bytes_sent;
i++;
}
cout<<"\ni: "<<i;
cout<<"\ntot_sent: "<<tot_sent;
cout<<"\nsize: "<<size;
} //S
if(ui == 'r') //YOU CAN IGNORE THIS
{
bool last = 0;
bool small = 0;
char *file;
char buff[512];
int size;
int tot_recv = 0;
char *fileSize;
bytes_rcv = recv(sockFD, fileSize, 10, 0);
string t;
t.assign(fileSize);
StringToInt(t, size);
if(size < 512)
{
last = 1;
small = 1;
}
bytes_rcv = recv(sockFD, file, 16, 0);
ofstream fout(file);
while(tot_recv < size)
{
if((size - tot_recv) < 512)
last = 1;
if(last)
{
if(small)
{
char *lst;
bytes_rcv = recv(sockFD, lst, size, 0);
fout.write(lst, size);
tot_recv = size;
}
else
{
int left = size - tot_recv;
char *lst;
bytes_rcv = recv(sockFD, lst, left, 0);
fout.write(lst, left);
tot_recv = size;
}
}
if(!last)
{
bytes_rcv = recv(sockFD, buff, 512, 0);
fout.write(buff, 512);
}
tot_recv += bytes_rcv;
}
} //R
shutdown(sockFD, 2);
}
WSACleanup();
return 0;
}
string IntToString(int num)
{
ostringstream myStream;
myStream << num << flush;
return(myStream.str());
}
bool StringToInt(const string &s, int &i)
{
istringstream myStream(s);
if (myStream>>i)
return true;
else
return false;
}
void removetext(char *txt, int size, int leave)
{
for(int i = 0;i < (size-leave);i++)
{
txt[leave+i] = '\0';
}
}
EDIT: