add open(string host, int port)

This commit is contained in:
pehladik 2020-04-06 17:42:28 +02:00
parent de1629c147
commit 1d2df7017d
2 changed files with 42 additions and 4 deletions

View file

@ -31,7 +31,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
int sock = 0;
const char* host = "127.0.0.1";
string host = "127.0.0.1";
#define PORT 6699
#endif
@ -96,7 +96,7 @@ int ComRobot::Open(string usart) {
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, host, &serv_addr.sin_addr) <= 0) {
if (inet_pton(AF_INET, host.c_str(), &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
@ -127,6 +127,36 @@ int ComRobot::Open(string usart) {
#endif
}
int ComRobot::Open(string shost, int nport) {
#ifdef __SIMULATION__
struct sockaddr_in serv_addr;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 200000;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof tv);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(nport);
if (inet_pton(AF_INET, shost.c_str(), &serv_addr.sin_addr) <= 0) {
cout << "Invalid address/ Address not supported" << endl;
return -1;
}
if (connect(sock, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
return -2;
}
return 1;
#else
return -1
#endif
}
/**
* Close serial link
* @return Success if above 0, failure if below 0

View file

@ -59,6 +59,14 @@ public:
*/
int Open(string usart);
/**
* Open tcp client for the graphical simulator
* @param host Host address
* @param port Port number
* @return 1 if ok or -1
*/
int Open(string shost, int nport);
/**
* Close serial link
* @return Success if above 0, failure if below 0
@ -89,9 +97,9 @@ public:
*/
virtual void Write_Post() {
}
Message *SendCommand(Message* msg, MessageID answerID, int maxRetries);
static Message *Ping() {
return new Message(MESSAGE_ROBOT_PING);
}