-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionTest.cpp
80 lines (75 loc) · 2.2 KB
/
ConnectionTest.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
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include "grSim_Commands.pb.h"
#include "grSim_Packet.pb.h"
#define PORT 20011
using namespace std;
struct sockaddr_in address;
int sock = 0,valread;
struct sockaddr_in serv_addr;
void send_data(){
grSim_Packet packet;
bool yellow = false;
packet.mutable_commands()->set_isteamyellow(true);
packet.mutable_commands()->set_timestamp(0.0);
grSim_Robot_Command* command = packet.mutable_commands()->add_robot_commands();
command->set_id(0);
command->set_wheelsspeed(true);
command->set_wheel1(0.0);
command->set_wheel2(0.0);
command->set_wheel3(0.0);
command->set_wheel4(0.0);
command->set_veltangent(10.0);
command->set_velnormal(0.0);
command->set_velangular(0.0);
command->set_kickspeedx(0.0);
command->set_kickspeedz(0.0);
command->set_spinner(false);
std::ostringstream stream;
packet.SerializeToOstream(&stream);
std::string ooop = stream.str();
// packet.SerializeToString(&ooop);
std::string data = stream.str();
std::cout<<"str: "<<stream.str()<<"\n";
//Send the Command
send(sock , data.c_str() , strlen(data.c_str()) , 0 );
//Command Sent
printf("Data Sent.\n");
}
int main(){
char *hello = "Hello From Client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
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, "127.0.0.1", &serv_addr.sin_addr)<=0)
// {
// printf("\nInvalid address/ Address not supported \n");
// return -1;
// }
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
int x = 0;
while(x<1000){
printf("Sending\n");
send_data();}
send(sock , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
//valread = read( sock , buffer, 1024);
//printf("%s\n",buffer );
return 0;
}