-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
106 lines (77 loc) · 1.91 KB
/
client.cpp
File metadata and controls
106 lines (77 loc) · 1.91 KB
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
// including libraries
#include <iostream>
#include <fstream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main()
{
// Initialise Winsock
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
{
cout<<"Winsock initialization failed\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
// Create socket
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Socket==INVALID_SOCKET)
{
cout<<"Socket creation Failed.. error!\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
// Resolve IP address for hostname
struct hostent *host;
if((host=gethostbyname("localhost"))==NULL)
{
std::cout<<"Failed to resolve hostname.\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
// Setup our socket address structure
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(55958);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr=*((unsigned long*)host->h_addr);
// Attempt to connect to server
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr))!=0)
{
std::cout<<"Failed to establish connection with server\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
// enter the file path ...
char filepath[1024];
cout << "Please enter the path to the file you would like to upload" << endl;
cin>>filepath;
ifstream file;
file.open(filepath); //open file
if(file.is_open()){
file.seekg(0, ios::end);
//file size!
int size = file.tellg();
cout << "The file size is " << size << " Bytes" << endl;
//sets location back to beginning of file
file.seekg(0, ios::beg);
char* rbuffer =new char [size] ;
//write file to buffer
file.read(rbuffer, size);
cout<<rbuffer;
//send file to server
int j = send(Socket, rbuffer, strlen(rbuffer), 0);
}
// Shutdown our socket
shutdown(Socket,SD_SEND);
// Close our socket entirely
closesocket(Socket);
// Cleanup Winsock
WSACleanup();
system("PAUSE");
return 0;
}