forked from vaishraman/SocketDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketDemo3.cpp
More file actions
102 lines (86 loc) · 2.96 KB
/
socketDemo3.cpp
File metadata and controls
102 lines (86 loc) · 2.96 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
/** Socket Demo 2
* This iteration switches to automatic ports.
* The server picks an arbitrary unused port, and the client connects to that port.
*/
#include "headers.h"
#define dprint(x) do { cout<<#x<<"="<<x<<endl; } while(0);
const char ACK = 0x6;
void sender(int connection) {
while (true) {
string toSend;
getline(cin, toSend);
toSend += '\0';
send(connection, toSend.c_str(), toSend.size(), 0);
//Acknowledgement
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);
if (buf == '\0') break;
else sbuf += buf;
}
cout << sbuf << endl;
send(connection, &ACK, 1, 0);
}
}
int main(int argc, char* argv[])
{
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;
if (isServer) {
/* Using automatic ports
A port of zero requests that the OS pick its own port
*/
addr.sin_port = htons(0);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
::bind(sock, (sockaddr*)&addr, sizeof(addr));
listen(sock, 0);
/* Printing the Port
Because we've switched to automatic ports, we have
to ask the OS what port it decided to bind our server
to. This is accomplished with getsockname(), which gets
that addr struct OUT of the socket.
*/
sockaddr_in addr; //that addr struct where we specify the port and IP
socklen_t len = sizeof(addr); //because C
getsockname(
sock, //the socket file descriptor
(sockaddr*)&addr, //the structure we want our info put into
&len //the amount of data the OS put into said structure, because C
);
unsigned short port = ntohs(addr.sin_port); //have to convert from Big-endian back to little-endian. ntohs = "network to host short"
cout << "Server running on port " << port << endl;
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");
/*
We will specify the server's port to the client on stdin
*/
unsigned short portnum;
cout << "connect to port: ";
cin >> portnum;
string junk; //throw away the newline so we don't send it later
getline(cin, junk);
addr.sin_port = htons(portnum);
connect(sock, (sockaddr*)&addr, sizeof(addr));
sender(sock);
}
}