-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.cpp
124 lines (104 loc) · 2.81 KB
/
Client.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
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "RIPv0.h"
#include <pthread.h>
#include <time.h>
#include "RoutingTable.h"
using namespace std;
/*
As we assume the client will only be connected to one device at a time,
We will not maintain the fds array etc. We'll just send messages
or listen to one socket only.
*/
int sockfd;
pthread_t listener_thread;
/*
Listens to messages from the router it is connected to
*/
void *messageListener(void *Args)
{
sleep(2); // Give it some time to start
char msg_buff[128] = {0};
do
{
recv(sockfd, msg_buff, 127, 0);
printf("Received a message: %s\n", msg_buff);
printf("\n");
memset(msg_buff, 0, 128);
sleep(2);
} while (strcmp(msg_buff, "close") != 0);
}
int main()
{
int router_port;
int myip;
cout << ">> Which server do you want to connect to? ";
cin >> router_port;
cout << ">> Enter your unique IP: ";
cin >> myip;
cout << endl;
cout << "Starting connection to server..." << endl;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
assert(sockfd != -1);
//Set Address Information
struct sockaddr_in saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(router_port);
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//Link to router
int res = connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
assert(res != -1);
cout << "Creating listening thread..." << endl;
pthread_create(&listener_thread, NULL, messageListener, (void *)NULL);
TableRow myself(myip, -1, "CLIENT");
cout << "Sending my unique IP to server..." << endl;
string init_msg = constructNewMessage(UPDATE, myip, router_port, (void *)&myself);
send(sockfd, init_msg.c_str(), strlen(init_msg.c_str()), 0);
cout << endl;
char buff[128] = {0};
int receiver = -1;
while (buff != "close")
{
sleep(2);
cout << "Press 1 for Sending a message\nPress 2 for resolving a Domain Name\n";
int option = 0;
cin >> option;
string message ;
if(option == 1){
cout<<">> Enter the unique IP of the client you wish to talk to: ";
cin>>receiver;
memset(buff,0,128);
cout<<">> Type you message : ";
cin.ignore();
fgets(buff,128,stdin);
message = constructNewMessage(MESSAGE, myip, receiver, (void*)buff );
}
else if(option == 2){
int DNS_IP = -1;
cout << "Enter DNS IP : ";
cin >> DNS_IP;
cout << endl;
cout << "Enter Domain Name : \n>>";
cin.ignore();
fgets(buff,128,stdin);
string finalMessage = "DNS REQ ";
finalMessage += buff;
message = constructNewMessage(MESSAGE, myip, DNS_IP, (void*)finalMessage.c_str() );
}
send(sockfd,message.c_str(),message.size(),0);
if (strncmp(buff, "close", 3) == 0)
{
break;
}
memset(buff, 0, 128);
}
close(sockfd);
}