Phoenix2D-Library  0.10
Connect.cpp
Go to the documentation of this file.
1 /*
2  * Phoenix2D (RoboCup Soccer Simulation 2D League)
3  * Copyright (c) 2013 Ivan Gonzalez
4  *
5  * This file is part of Phoenix2D.
6  *
7  * Phoenix2D is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Phoenix2D is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Phoenix2D. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <iostream>
27 #include "Connect.h"
28 #include "Config.h"
29 namespace Phoenix
30 {
31 struct Socket {
32  int socketfd;
33  struct sockaddr_in server;
34 };
35 
37 
38 void connectToServer(const char *host, int port) {
39  int sockfd;
40  struct sockaddr_in server_addr;
41  struct hostent *host_0;
42  struct in_addr *addr_ptr;
43  if (inet_addr(host) == INADDR_NONE) {
44  host_0 = (struct hostent *)gethostbyname(host);
45  if (host_0 == 0) {
46  std::cerr << "Connect::connectToServer(const char *, int) -> can not get host" << std::endl; //Error: can not get host
47  return;
48  } else {
49  addr_ptr = (struct in_addr *)(*host_0->h_addr_list);
50  host = inet_ntoa(*addr_ptr);
51  }
52  }
53  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
54  if (sockfd < 0) {
55  std::cerr << "Connect::connectToServer(const char *, int) -> can not open socket" << std::endl; //Error: can not open socket
56  return;
57  }
58  bzero(&server_addr, sizeof(server_addr));
59  server_addr.sin_family = AF_INET;
60  server_addr.sin_port = htons(0);
61  sock.server.sin_addr.s_addr = htonl(INADDR_ANY);
62  if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
63  std::cerr << "Connect:connectToServer(const char *, int) -> can not bind host" << std::endl; //Error: can not bind host
64  return;
65  }
66  sock.socketfd = sockfd;
67  bzero(&sock.server, sizeof(sock.server));
68  sock.server.sin_family = AF_INET;
69  sock.server.sin_port = htons(port);
70  sock.server.sin_addr.s_addr = inet_addr(host);
71 }
72 
73 Connect::Connect(const char *host, int port) {
74  connectToServer(host, port);
75 }
76 
78  if (Config::VERBOSE) std::cout << "Connect out" << std::endl;
79 }
80 
82  shutdown(sock.socketfd, SHUT_RDWR);
83  close(sock.socketfd);
84 }
85 
86 bool Connect::sendMessage(std::string msg) {
87  int size = msg.size() + 1;
88  if (sendto(sock.socketfd, msg.c_str(), size, 0, (struct sockaddr *)&sock.server, sizeof(sock.server)) < 0) {
89  std::cerr << "Connect::sendMessage(string) -> error sending message" << std::endl;
90  return false;
91  }
92  return true;
93 }
94 
95 std::string Connect::receiveMessage() {
96  char msg[4096];
97  socklen_t servlen;
98  struct sockaddr_in serv_addr;
99  servlen = sizeof(serv_addr);
100  if (recvfrom(sock.socketfd, msg, 4096, 0, (struct sockaddr *)&serv_addr, &servlen) < 0) {
101  std::cerr << "Connect::receiveMessage() -> error reading message" << std::endl;
102  return "(error socket_failed)";
103  } else {
104  if (sock.server.sin_port != serv_addr.sin_port) {
105  sock.server.sin_port = serv_addr.sin_port;
106  }
107  return std::string(msg);
108  }
109 }
110 }
Socket sock
Definition: Connect.cpp:36
struct sockaddr_in server
Definition: Connect.cpp:33
void disconnect()
Definition: Connect.cpp:81
Connect(const char *host, int port)
Definition: Connect.cpp:73
bool sendMessage(std::string msg)
Definition: Connect.cpp:86
void connectToServer(const char *host, int port)
Definition: Connect.cpp:38
std::string receiveMessage()
Definition: Connect.cpp:95
static bool VERBOSE
Definition: Config.h:38