class_tcp.cpp
Go to the documentation of this file.
1 // This file is part of the tcp/ip client library.
2 //
3 // Copyright (C) 2011 LAR - Laboratory for Automation and Robotics, ATLAS Project
4 // Department of Mechanical Engineering
5 // University of Aveiro
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2.1
10 // of the License, or (at your option) any later version.
11 //
12 // This program 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 this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 // 02110-1301, USA.
21 
27 #include <tcp_client/class_tcp.h>
28 
29 tcp_client::tcp_client(const char*ip,int port,bool async_comm)
30 {
31  err=0;//Clear the error variable
32 
33  connected=false;
34 
35  sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
36  if(sock<0)
37  {
38  sprintf(err_msg,"Cannot open socket");
39  err=-1;
40  return;
41  }
42 
43  async=async_comm;
44 
45  /* Construct the server sockaddr_in structure */
46  memset(&server, 0, sizeof(server)); /* Clear structure */
47  server.sin_family = AF_INET; /* Internet/IP */
48  server.sin_addr.s_addr = inet_addr(ip); /* IP address */
49  server.sin_port = htons(port); /* server port */
50 }
51 
53 {
54  Disconnect();
55 }
56 
58 {
59  err=0;
60 
61  ret=connect(sock,(struct sockaddr *) &server,sizeof(server));
62  if(ret<0)
63  {
64  sprintf(err_msg,"Failed to connect with server");
65  err=-2;
66  return -1;
67  }
68 
69  if(async)
70  {
71  fcntl(sock,F_SETOWN,getpid());
72  fcntl(sock,F_SETFL,O_ASYNC);
73  }
74 
75  connected=true;
76 
77  return 0;
78 }
79 
81 {
82  err=0;
83 
84  ret=close(sock);
85  if(ret<0)
86  {
87  sprintf(err_msg,"Failed to close connection");
88  err=-3;
89  return -1;
90  }
91 
92  connected=false;
93 
94  return 0;
95 }
96 
97 int tcp_client::Send(char*data,int size)
98 {
99  if(!connected)
100  {
101  sprintf(err_msg,"Connection not established");
102  err=-4;
103  return -1;
104  }
105 
106  err=0;
107  errno=0;
108  ret=send(sock,data,size,0);
109 
110  if(errno==EPIPE)
111  {
112  sprintf(err_msg,"Pipe broke");
113  err=-4;
114  return -1;
115  }else if(ret<size)
116  {
117  sprintf(err_msg,"Mismatch in number of sent bytes");
118  err=-4;
119  return -1;
120  }else if(errno!=0)
121  {
122  sprintf(err_msg,"Alarm! error not caught");
123  perror("send");
124  err=-3;
125  return -1;
126  }
127 
128  return 0;
129 }
130 
131 int tcp_client::Receive(char*data,int size,bool peek,int*number,int flags)
132 {
133  if(!connected)
134  {
135  sprintf(err_msg,"Connection not established");
136  err=-4;
137  return -1;
138  }
139 
140  err=0;
141 
142  //Get the current time stamp
143 // timestamp=carmen_get_time();
144 
145  //If peek mode is selected we don't erase the information from the buffer
146  if(peek)
147  {
148  ret=recv(sock,data,size, MSG_PEEK);
149  if(ret<0)
150  {
151  sprintf(err_msg,"Failed to peek data from server");
152  err=-5;
153  return -1;
154  }
155 
156  return 0;
157  }
158 
159  //Normal read
160  ret=recv(sock,data,size,flags);
161  if(ret<0)
162  {
163  sprintf(err_msg,"Failed to receive bytes from server");
164  err=-5;
165  return -1;
166  }else if(number!=NULL)
167  {
168  *number=ret;
169  }
170 
171  return 0;
172 }
173 
174 int tcp_client::perr(const char*text)
175 {
176  if(err<0)
177  {
178  cout<<"Error!! "<<text<<", "<<err_msg<<endl;
179  return -1;
180  }
181  return 0;
182 }
183 
185 {
186  return sock;
187 }
int Receive(char *data, int size, bool peek=false, int *number=NULL, int flags=0)
Read data.
Definition: class_tcp.cpp:131
int GetSocket()
Get socket value.
Definition: class_tcp.cpp:184
Main header for tcp communications.
int Disconnect(void)
Disconnect from server.
Definition: class_tcp.cpp:80
int Connect(void)
Connect to server.
Definition: class_tcp.cpp:57
~tcp_client()
Destructor of the class.
Definition: class_tcp.cpp:52
char err_msg[1024]
Last error message.
Definition: class_tcp.h:131
int err
Last error code.
Definition: class_tcp.h:128
tcp_client(const char *ip, int port, bool async_comm=false)
Constructor of the class.
Definition: class_tcp.cpp:29
bool connected
Status of the connection.
Definition: class_tcp.h:134
struct sockaddr_in server
Configuration structure.
Definition: class_tcp.h:141
int Send(char *data, int size)
Send data.
Definition: class_tcp.cpp:97
bool async
Asynchronous comm.
Definition: class_tcp.h:147
int sock
Socket identification.
Definition: class_tcp.h:138
int ret
Return value for functions.
Definition: class_tcp.h:144
int perr(const char *text)
Print error message.
Definition: class_tcp.cpp:174


tcp_client
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:53