-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsocketDemo2.cpp
86 lines (72 loc) · 2.31 KB
/
socketDemo2.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
/** Socket Demo 2
* This iteration implements acknowledgement messages.
* This way, the sender knows their messages have been recieved.
*/
#include "headers.h"
#define dprint(x) do { cout<<#x<<"="<<x<<endl; } while(0);
const char ACK = 0x6; //this is the Acknowledge byte in ASCII
void sender(int connection) {
while (true) {
string toSend;
getline(cin, toSend);
toSend += '\0'; //we need a definition of the "end of a message now"
send(connection, toSend.c_str(), toSend.size(), 0);
/* Acknowledgement
The client is allowed to recieve from the same connection
(sockets are bidirectional). Here, we wait for the
server to acknowledge that it got a message
*/
char val;
recv(connection, &val, sizeof(val), 0);
if (val == ACK) {
cout << "\tMessage received" << endl;
}
}
}
void reciever(int connection) {
while (true) {
char buf;
string sbuf;
while (true) {
size_t bytesWritten = recv(connection, &buf, sizeof(buf), 0);
/*
Here's where we check if we've gotten the whole message.
Simply keep recieving until we get a null byte.
*/
if (buf == '\0') break;
else sbuf += buf;
}
cout << sbuf << endl;
/* Acknowlegement
Here's where we send that ACK byte, over the same connection
*/
send(connection, &ACK, 1, 0);
}
}
int main(int argc, char* argv[])
{
unsigned short port = 3778;
bool isServer = argc <= 1;
auto sock = socket(AF_INET, SOCK_STREAM, 0);
int yes = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, 4);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (isServer) {
cout << "server mode" << endl;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
::bind(sock, (sockaddr*)&addr, sizeof(addr));
listen(sock, 0);
while (true) {
auto clientConnection = accept(sock, 0, 0);
reciever(clientConnection);
}
}
else {
cout << "client mode" << endl;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(sock, (sockaddr*)&addr, sizeof(addr));
sender(sock);
}
}