-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaninet.cpp
129 lines (107 loc) · 3.07 KB
/
vaninet.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
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
#include <algorithm>
#include <vector>
#include <iostream>
#include <map>
#include "vaninet.hpp"
using namespace std;
void dump( string s)
{
for (unsigned int n=0; n<s.length(); ++n)
{
char c = s[n];
cout << (int) c << ",";
}
cout << endl;
}
/**
* SETTING UP SOCKET
*/
pair<int, struct sockaddr_in> setUpSocket(const char *IP, int port)
{
struct sockaddr_in address;
address.sin_family = AF_INET;
inet_pton(AF_INET, IP, &(address.sin_addr));
if(port <= 1024)
{
perror("Cannot use a port number <= 1024 ");
perror("Assigning another port . . . ");
port = 0; //So that the system selects the lowest unused port automatically
}
address.sin_port = htons(port);
int socketRet = socket(AF_INET, SOCK_STREAM, 0);
if(socketRet == -1)
{
perror("Could not create socket!");
exit(1);
}
int bindSuccess = bind(socketRet, (const sockaddr *)&address, sizeof(sockaddr_in));
if(bindSuccess < -1)
{
perror("Could not bind socket to specified address!");
exit(1);
}
pair<int, struct sockaddr_in> retPair = make_pair(socketRet, address);
return retPair;
}
/**
* SEND DATA
*/
int sendData(char *buffer, long long int sendLength, int sendersSocket)
{
//cout<<"send length is "<<sendLength<<"\n";
char *ptrToBuffer = buffer;
//cout << "VC: " << buffer << "\n";
//PKB
send(sendersSocket, &sendLength, sizeof(long long int), 0);
//cout<<"sending these bytes \n"<<buffer<<"\n";
long long int bytesSent;
while(sendLength > 0)
{
bytesSent = send(sendersSocket, ptrToBuffer, sendLength, 0);
//cout << bytesSent << "\n";
if(bytesSent < 1)
{
return(-1);
}
//cout<<ptrToBuffer<<" bytes sent\n";
//cout<<bytesSent<<" bytes were sent\n";
ptrToBuffer += bytesSent;
sendLength -= bytesSent;
}
//cout << "VC Final\n";
return 0;
}
/**
* RECEIVE DATA
*/
char* receiveData(long long int bufferSize, int receiversSocket)
{
//PKB
recv(receiversSocket, &bufferSize, sizeof(long long int), 0);
//cout << "PKB: " << bufferSize << "\n";
char *bufferStorage = (char*)malloc(sizeof(char)*bufferSize);
//char bufferStorage[bufferSize];
long long int bytesRecvd = 1; //just an initial value
long long int totalBytes = 0;
char *buffer;
buffer = bufferStorage;
while(bytesRecvd > 0 && totalBytes < bufferSize)
{
bytesRecvd = recv(receiversSocket, bufferStorage, bufferSize, 0);
//cout << "bytes received = " << bytesRecvd << "\n";
if(bytesRecvd == -1)
{
perror("Error while receiving data ");
exit(1);
}
bufferStorage += bytesRecvd;
//cout<<"got "<<bytesRecvd<<" bytes\n";
totalBytes += bytesRecvd;
//cout << "TB: " << totalBytes << "\n";
//cout<<"received these bytes: "<<buffer<<"\n";
//cout<<"dump for receiver buffer: \n";
//dump(string(buffer));
}
//cout << "VC: " << buffer << "\n";
return buffer;
}