-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (86 loc) · 2 KB
/
main.cpp
File metadata and controls
100 lines (86 loc) · 2 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
/**
* @file main.cpp
*
* @author William Longpré, Étienne Bellerose
* @brief
* @version 0.1
* @date 2019-11-20
*
* @copyright Copyright (c) 2019
*
*/
/*----------Includes----------*/
#include <math.h>
#include <stdint.h>
#include <iostream>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
/*----------Defines-----------*/
//#define BASESTATION_ADDR "192.168.1.1"
#define BASESTATION_ADDR "192.168.243.2"
#define PORT 8080
/*----------Usings------------*/
using namespace std;
/*---------Variables----------*/
int SocketID = 0;
struct sockaddr_in serv_addr;
/*---------Prototypes---------*/
void Routine(void);
void init(void);
int SendSocket(char* message);
int RecieveSocket(char* message[1024]);
int main(int argv, char** argc)
{
cout << "Starting..." << endl;
system("./ServerSim");
init();
Routine();
}
void init()
{
/*-------Initialization-------*/
//Open TCP socket client
if ((SocketID = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
cout << "Socket creation error" << endl;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
//Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, BASESTATION_ADDR, &serv_addr.sin_addr)<=0)
{
cout << "Invalid address/ Address not supported " << endl;
}
for(int i = 0; i<10; i++)
{
if(connect(SocketID, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
cout << "Connection Failed" << endl;
}
}
}
void Routine()
{
char message[] = {"Hello World"};
char* messageRecv[1024] = {};
while(true)
{
sleep(2);
SendSocket(message);
RecieveSocket(messageRecv);
}
}
int SendSocket(char* message)
{
send(SocketID, message, strlen(message), 0);
return 0;
}
int RecieveSocket(char* message[1024])
{
recv(SocketID , message, 1024, MSG_WAITALL);
cout << (*message) << endl;
return 0;
}