This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpClient.cpp
65 lines (57 loc) · 1.54 KB
/
TcpClient.cpp
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
#include "tcp.h"
#include "httpd.h"
#include <netinet/in.h>
#include <iostream>
using namespace std;
void TCP:: SetIp(string ip1)
{
ip=ip1;
}
void TCP:: SetPort(int port)
{
port=htons(port);
}
int TCP:: SendPkt(const char* buf,int size_pkt)
{
int bytesend=send(socketFd,buf,size_pkt,0);
if(bytesend<0)
{
cout<<"send failed";
}
else
cout<<"Send successful"<<bytesend<<endl;
return bytesend;
}
void TCP::RecevePkt(string type)
{
char rcvDataBuf[65536];
socklen_t sinSize = sizeof(struct sockaddr_in);
cout<<"Receiving response...";
memset(&rcvDataBuf, 0, 65536);
downloadCount=downloadCount + 1;
string filename=type+"Obj";
filename+=to_string(downloadCount);
http_downloader httpclient;
int contentlength=httpclient.ParseHeader(socketFd);
string type1=httpclient.type;
size_t found2 = type1.find_first_of("/");
string extension =type1.substr(found2);
filename+="."+extension.erase(0,1);
ofstream File_HTTP(filename);
int datarecv=0;int bytes=0;
cout<<"Downloading Object.....";
while(datarecv = recv(socketFd, rcvDataBuf, 65535, 0))
{
if(datarecv==-1)
{
perror("recieve");
exit(3);
}
File_HTTP.write(rcvDataBuf,datarecv);
bytes+=datarecv;
if(bytes==contentlength)
break;
}
File_HTTP.close();
cout<<"Download Complete"<<downloadCount;
}