| 1 |
|
|---|
| 2 |
/* |
|---|
| 3 |
* +---------------------------------------------------------+ |
|---|
| 4 |
* | Easy Socket library - version 0.1 | |
|---|
| 5 |
* | (C) 1999-2000, Erich Roncarolo <erich@roncarolo.eu.org> | |
|---|
| 6 |
* +---------------------------------------------------------+ |
|---|
| 7 |
* |
|---|
| 8 |
* This file is part of Easy Socket library. |
|---|
| 9 |
* |
|---|
| 10 |
* This program is free software; you can redistribute it and/or modify |
|---|
| 11 |
* it under the terms of the GNU Lesser General Public License as published by |
|---|
| 12 |
* the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 |
* (at your option) any later version. |
|---|
| 14 |
* |
|---|
| 15 |
* This program is distributed in the hope that it will be useful, |
|---|
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 |
* GNU Lesser General Public License for more details. |
|---|
| 19 |
* |
|---|
| 20 |
* You should have received a copy of the GNU Lesser General Public License |
|---|
| 21 |
* along with this program; if not, write to the Free Software |
|---|
| 22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|---|
| 23 |
*/ |
|---|
| 24 |
|
|---|
| 25 |
#include <unistd.h> |
|---|
| 26 |
#include <stdio.h> |
|---|
| 27 |
#include <stdlib.h> |
|---|
| 28 |
#include <errno.h> |
|---|
| 29 |
#include <netinet/in.h> |
|---|
| 30 |
#include <netdb.h> |
|---|
| 31 |
#include <string.h> |
|---|
| 32 |
#include <sys/socket.h> |
|---|
| 33 |
#include <sys/types.h> |
|---|
| 34 |
#include <ctype.h> |
|---|
| 35 |
|
|---|
| 36 |
#include "easy_sock.h" |
|---|
| 37 |
|
|---|
| 38 |
/* |
|---|
| 39 |
* Type of a error handler |
|---|
| 40 |
*/ |
|---|
| 41 |
typedef void (*easy_error_t)(int); |
|---|
| 42 |
|
|---|
| 43 |
/* |
|---|
| 44 |
* Default signal handler |
|---|
| 45 |
*/ |
|---|
| 46 |
void easy_default_error_handler(ssize_t err) {} |
|---|
| 47 |
|
|---|
| 48 |
/* |
|---|
| 49 |
* Actual error handler |
|---|
| 50 |
*/ |
|---|
| 51 |
easy_error_t err_handler = easy_default_error_handler; |
|---|
| 52 |
|
|---|
| 53 |
/* |
|---|
| 54 |
* Installs the error handler. |
|---|
| 55 |
*/ |
|---|
| 56 |
void easy_error(easy_error_t handler) { |
|---|
| 57 |
err_handler = handler; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
/* |
|---|
| 61 |
* This is a very useful function used to connect an host on a port. |
|---|
| 62 |
* 'host' is the hostname |
|---|
| 63 |
* 'port' is the port number |
|---|
| 64 |
* return a socket descriptor |
|---|
| 65 |
*/ |
|---|
| 66 |
int easy_tcp_connect(char *host, int port) { |
|---|
| 67 |
//int i; |
|---|
| 68 |
int sock; |
|---|
| 69 |
struct sockaddr_in rsock; |
|---|
| 70 |
struct hostent * hostinfo; |
|---|
| 71 |
struct in_addr * addp; |
|---|
| 72 |
//struct sockaddr_in sockname; |
|---|
| 73 |
|
|---|
| 74 |
memset ((char *)&rsock,0,sizeof(rsock)); |
|---|
| 75 |
|
|---|
| 76 |
if ( (hostinfo=gethostbyname(host)) == NULL ) { |
|---|
| 77 |
sprintf(easy_sock_err_msg,"Cannot find %s - %s\n",host,strerror(errno)); |
|---|
| 78 |
return -1; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
sock=socket(AF_INET,SOCK_STREAM,0); |
|---|
| 82 |
if ( sock == -1 ) { |
|---|
| 83 |
sprintf(easy_sock_err_msg,"Can't create socket - %s\n",strerror(errno)); |
|---|
| 84 |
return -1; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
addp=(struct in_addr *)*(hostinfo->h_addr_list); |
|---|
| 88 |
rsock.sin_addr=*addp; |
|---|
| 89 |
rsock.sin_family=AF_INET; |
|---|
| 90 |
rsock.sin_port=htons(port); |
|---|
| 91 |
|
|---|
| 92 |
if ( connect(sock,(struct sockaddr *)(&rsock),sizeof(rsock)) == -1 ) { |
|---|
| 93 |
sprintf(easy_sock_err_msg,"Can't connect %s on port %i - %s\n",host,port,strerror(errno)); |
|---|
| 94 |
return -1; |
|---|
| 95 |
} |
|---|
| 96 |
return sock; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
/* |
|---|
| 100 |
* This is a very useful function used to bind an host on a port. |
|---|
| 101 |
* 'host' is the hostname (if it is NULL bind any address) |
|---|
| 102 |
* 'port' is the port number |
|---|
| 103 |
* 'backlog' define the maximum length the queue of pending connections |
|---|
| 104 |
* may grow to. |
|---|
| 105 |
* return a socket descriptor |
|---|
| 106 |
*/ |
|---|
| 107 |
int easy_tcp_bind(char *host, int port, int backlog) { |
|---|
| 108 |
const int on = 1; |
|---|
| 109 |
int sock; |
|---|
| 110 |
struct hostent * hostinfo; |
|---|
| 111 |
struct in_addr * addp; |
|---|
| 112 |
struct sockaddr_in sockname; |
|---|
| 113 |
|
|---|
| 114 |
memset ((char *)&sockname,0,sizeof(sockname)); |
|---|
| 115 |
|
|---|
| 116 |
if (host == NULL) { |
|---|
| 117 |
hostinfo = NULL; |
|---|
| 118 |
} else if ( (hostinfo=gethostbyname(host)) == NULL ) { |
|---|
| 119 |
sprintf(easy_sock_err_msg,"Cannot find %s - %s\n",host,strerror(errno)); |
|---|
| 120 |
return -1; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
if( (sock=socket(AF_INET,SOCK_STREAM,0)) == -1 ) { |
|---|
| 124 |
sprintf(easy_sock_err_msg,"Error opening socket - %s\n",strerror(errno)); |
|---|
| 125 |
return -1; |
|---|
| 126 |
} |
|---|
| 127 |
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
|---|
| 128 |
|
|---|
| 129 |
if (hostinfo != NULL) { |
|---|
| 130 |
addp=(struct in_addr *)*(hostinfo->h_addr_list); |
|---|
| 131 |
sockname.sin_addr=*addp; |
|---|
| 132 |
} else { |
|---|
| 133 |
sockname.sin_addr.s_addr=INADDR_ANY; |
|---|
| 134 |
} |
|---|
| 135 |
sockname.sin_family=AF_INET; |
|---|
| 136 |
sockname.sin_port=htons(port); |
|---|
| 137 |
|
|---|
| 138 |
if ( (bind(sock,(struct sockaddr *)&sockname,sizeof(sockname))) == -1 ) { |
|---|
| 139 |
close (sock); |
|---|
| 140 |
sprintf(easy_sock_err_msg,"Cannot bind port %i at %s -%s\n",port,host,strerror(errno)); |
|---|
| 141 |
return -1; |
|---|
| 142 |
} |
|---|
| 143 |
listen (sock,backlog); |
|---|
| 144 |
return (sock); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
/* |
|---|
| 148 |
* This is a function that read a char from a socket. |
|---|
| 149 |
* It expect |
|---|
| 150 |
*/ |
|---|
| 151 |
char read_char(int sock) { |
|---|
| 152 |
char c; |
|---|
| 153 |
ssize_t err; |
|---|
| 154 |
|
|---|
| 155 |
err = read(sock,&c,sizeof(c)); |
|---|
| 156 |
|
|---|
| 157 |
if (err == 0) { |
|---|
| 158 |
sprintf(easy_sock_err_msg,"End of file reached reading char.\n"); |
|---|
| 159 |
err_handler(EOF); |
|---|
| 160 |
} else if (err < 0) { |
|---|
| 161 |
easy_sock_err = errno; |
|---|
| 162 |
sprintf(easy_sock_err_msg,"Error reading char: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 163 |
err_handler(easy_sock_err); |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
return (c); |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
/* |
|---|
| 170 |
* This is a function that read a short from a socket. |
|---|
| 171 |
*/ |
|---|
| 172 |
short read_short(int sock) { |
|---|
| 173 |
short s; |
|---|
| 174 |
ssize_t err; |
|---|
| 175 |
|
|---|
| 176 |
err = read(sock,&s,sizeof(s)); |
|---|
| 177 |
|
|---|
| 178 |
if (err == 0) { |
|---|
| 179 |
sprintf(easy_sock_err_msg,"End of file reached reading short.\n"); |
|---|
| 180 |
err_handler(EOF); |
|---|
| 181 |
} else if (err < 0) { |
|---|
| 182 |
easy_sock_err = errno; |
|---|
| 183 |
sprintf(easy_sock_err_msg,"Error reading short: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 184 |
err_handler(easy_sock_err); |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
return (s); |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
/* |
|---|
| 191 |
* This is a function that read an integer from a socket. |
|---|
| 192 |
*/ |
|---|
| 193 |
int read_int(int sock) { |
|---|
| 194 |
int i; |
|---|
| 195 |
ssize_t err; |
|---|
| 196 |
|
|---|
| 197 |
err = read(sock,&i,sizeof(i)); |
|---|
| 198 |
|
|---|
| 199 |
if (err == 0) { |
|---|
| 200 |
sprintf(easy_sock_err_msg,"End of file reached reading int.\n"); |
|---|
| 201 |
err_handler(EOF); |
|---|
| 202 |
} else if (err < 0) { |
|---|
| 203 |
easy_sock_err = errno; |
|---|
| 204 |
sprintf(easy_sock_err_msg,"Error reading int: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 205 |
err_handler(easy_sock_err); |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
return (i); |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
/* |
|---|
| 212 |
* This is a function that read a long from a socket. |
|---|
| 213 |
*/ |
|---|
| 214 |
long read_long(int sock) { |
|---|
| 215 |
long l; |
|---|
| 216 |
ssize_t err; |
|---|
| 217 |
|
|---|
| 218 |
err = read(sock,&l,sizeof(l)); |
|---|
| 219 |
|
|---|
| 220 |
if (err == 0) { |
|---|
| 221 |
sprintf(easy_sock_err_msg,"End of file reached reading long.\n"); |
|---|
| 222 |
err_handler(EOF); |
|---|
| 223 |
} else if (err < 0) { |
|---|
| 224 |
easy_sock_err = errno; |
|---|
| 225 |
sprintf(easy_sock_err_msg,"Error reading long: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 226 |
err_handler(easy_sock_err); |
|---|
| 227 |
} |
|---|
| 228 |
|
|---|
| 229 |
return (l); |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
/* |
|---|
| 233 |
* This is a function that read a float from a socket. |
|---|
| 234 |
*/ |
|---|
| 235 |
float read_float(int sock) { |
|---|
| 236 |
float f; |
|---|
| 237 |
ssize_t err; |
|---|
| 238 |
|
|---|
| 239 |
err = read(sock,&f,sizeof(f)); |
|---|
| 240 |
|
|---|
| 241 |
if (err == 0) { |
|---|
| 242 |
sprintf(easy_sock_err_msg,"End of file reached reading float.\n"); |
|---|
| 243 |
err_handler(EOF); |
|---|
| 244 |
} else if (err < 0) { |
|---|
| 245 |
easy_sock_err = errno; |
|---|
| 246 |
sprintf(easy_sock_err_msg,"Error reading float: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 247 |
err_handler(easy_sock_err); |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
return (f); |
|---|
| 251 |
} |
|---|
| 252 |
|
|---|
| 253 |
/* |
|---|
| 254 |
* This is a function that read a double from a socket. |
|---|
| 255 |
*/ |
|---|
| 256 |
double read_double(int sock) { |
|---|
| 257 |
double d; |
|---|
| 258 |
ssize_t err; |
|---|
| 259 |
|
|---|
| 260 |
err = read(sock,&d,sizeof(d)); |
|---|
| 261 |
|
|---|
| 262 |
if (err == 0) { |
|---|
| 263 |
sprintf(easy_sock_err_msg,"End of file reached reading double.\n"); |
|---|
| 264 |
err_handler(EOF); |
|---|
| 265 |
} else if (err < 0) { |
|---|
| 266 |
easy_sock_err = errno; |
|---|
| 267 |
sprintf(easy_sock_err_msg,"Error reading double: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 268 |
err_handler(easy_sock_err); |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
return (d); |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
/* |
|---|
| 275 |
* This is a function that read a string from a socket. |
|---|
| 276 |
* return a new 'mallocated' string or NULL if an error occours. |
|---|
| 277 |
* |
|---|
| 278 |
* Details: this function reads first the string length (int) then |
|---|
| 279 |
* use malloc() to allocate new string so it can be read. |
|---|
| 280 |
* Remember! It's your own responsibility free() the string. |
|---|
| 281 |
*/ |
|---|
| 282 |
char* read_string(int sock) { |
|---|
| 283 |
size_t len; |
|---|
| 284 |
ssize_t err; |
|---|
| 285 |
char* string; |
|---|
| 286 |
|
|---|
| 287 |
err = read(sock,&len,sizeof(len)); |
|---|
| 288 |
|
|---|
| 289 |
if (err == 0) { |
|---|
| 290 |
sprintf(easy_sock_err_msg,"End of file reached reading string length.\n"); |
|---|
| 291 |
err_handler(EOF); |
|---|
| 292 |
return NULL; |
|---|
| 293 |
} else if (err < 0) { |
|---|
| 294 |
easy_sock_err = errno; |
|---|
| 295 |
sprintf(easy_sock_err_msg,"Error reading string length: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 296 |
err_handler(easy_sock_err); |
|---|
| 297 |
return NULL; |
|---|
| 298 |
} |
|---|
| 299 |
if (len == 0) return NULL; |
|---|
| 300 |
|
|---|
| 301 |
string = (char*)malloc(len+1); |
|---|
| 302 |
memset(string,0,len+1); |
|---|
| 303 |
|
|---|
| 304 |
err = read(sock,string,len); |
|---|
| 305 |
|
|---|
| 306 |
if (err == 0) { |
|---|
| 307 |
sprintf(easy_sock_err_msg,"End of file reached reading string.\n"); |
|---|
| 308 |
err_handler(EOF); |
|---|
| 309 |
return NULL; |
|---|
| 310 |
} else if (err < 0) { |
|---|
| 311 |
easy_sock_err = errno; |
|---|
| 312 |
sprintf(easy_sock_err_msg,"Error reading string: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 313 |
err_handler(easy_sock_err); |
|---|
| 314 |
return NULL; |
|---|
| 315 |
} |
|---|
| 316 |
string[len] = (char)0; |
|---|
| 317 |
|
|---|
| 318 |
return (string); |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
/* |
|---|
| 322 |
* This is a function that write a char from a socket. |
|---|
| 323 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 324 |
*/ |
|---|
| 325 |
int write_char(int sock, char c) { |
|---|
| 326 |
ssize_t err; |
|---|
| 327 |
|
|---|
| 328 |
err = write(sock,&c,sizeof(c)); |
|---|
| 329 |
|
|---|
| 330 |
if (err == 0) { |
|---|
| 331 |
easy_sock_err = 0; |
|---|
| 332 |
sprintf(easy_sock_err_msg,"No char written: why?\n"); |
|---|
| 333 |
} else if (err < 0) { |
|---|
| 334 |
easy_sock_err = errno; |
|---|
| 335 |
sprintf(easy_sock_err_msg,"Error writing char: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 336 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 337 |
} |
|---|
| 338 |
|
|---|
| 339 |
return (err); |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
/* |
|---|
| 343 |
* This is a function that write a short to a socket. |
|---|
| 344 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 345 |
*/ |
|---|
| 346 |
int write_short(int sock, short s) { |
|---|
| 347 |
ssize_t err; |
|---|
| 348 |
|
|---|
| 349 |
err = write(sock,&s,sizeof(s)); |
|---|
| 350 |
|
|---|
| 351 |
if (err == 0) { |
|---|
| 352 |
easy_sock_err = 0; |
|---|
| 353 |
sprintf(easy_sock_err_msg,"No short written: why?\n"); |
|---|
| 354 |
} else if (err < 0) { |
|---|
| 355 |
easy_sock_err = errno; |
|---|
| 356 |
sprintf(easy_sock_err_msg,"Error writing short: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 357 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 358 |
} |
|---|
| 359 |
|
|---|
| 360 |
return (err); |
|---|
| 361 |
} |
|---|
| 362 |
|
|---|
| 363 |
/* |
|---|
| 364 |
* This is a function that write an integer from a socket. |
|---|
| 365 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 366 |
*/ |
|---|
| 367 |
int write_int(int sock, int i) { |
|---|
| 368 |
ssize_t err; |
|---|
| 369 |
|
|---|
| 370 |
err = write(sock,&i,sizeof(i)); |
|---|
| 371 |
|
|---|
| 372 |
if (err == 0) { |
|---|
| 373 |
easy_sock_err = 0; |
|---|
| 374 |
sprintf(easy_sock_err_msg,"No int written: why?\n"); |
|---|
| 375 |
} else if (err < 0) { |
|---|
| 376 |
easy_sock_err = errno; |
|---|
| 377 |
sprintf(easy_sock_err_msg,"Error writing int: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 378 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 379 |
} |
|---|
| 380 |
|
|---|
| 381 |
return (err); |
|---|
| 382 |
} |
|---|
| 383 |
|
|---|
| 384 |
/* |
|---|
| 385 |
* This is a function that write a long from a socket. |
|---|
| 386 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 387 |
*/ |
|---|
| 388 |
int write_long(int sock, long l) { |
|---|
| 389 |
ssize_t err; |
|---|
| 390 |
|
|---|
| 391 |
err = write(sock,&l,sizeof(l)); |
|---|
| 392 |
|
|---|
| 393 |
if (err == 0) { |
|---|
| 394 |
easy_sock_err = 0; |
|---|
| 395 |
sprintf(easy_sock_err_msg,"No long written: why?\n"); |
|---|
| 396 |
} else if (err < 0) { |
|---|
| 397 |
easy_sock_err = errno; |
|---|
| 398 |
sprintf(easy_sock_err_msg,"Error writing long: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 399 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 400 |
} |
|---|
| 401 |
|
|---|
| 402 |
return (err); |
|---|
| 403 |
} |
|---|
| 404 |
|
|---|
| 405 |
/* |
|---|
| 406 |
* This is a function that write a float from a socket. |
|---|
| 407 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 408 |
*/ |
|---|
| 409 |
int write_float(int sock, float f) { |
|---|
| 410 |
ssize_t err; |
|---|
| 411 |
|
|---|
| 412 |
err = write(sock,&f,sizeof(f)); |
|---|
| 413 |
|
|---|
| 414 |
if (err == 0) { |
|---|
| 415 |
easy_sock_err = 0; |
|---|
| 416 |
sprintf(easy_sock_err_msg,"No float written: why?\n"); |
|---|
| 417 |
} else if (err < 0) { |
|---|
| 418 |
easy_sock_err = errno; |
|---|
| 419 |
sprintf(easy_sock_err_msg,"Error writing float: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 420 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 421 |
} |
|---|
| 422 |
|
|---|
| 423 |
return (err); |
|---|
| 424 |
} |
|---|
| 425 |
|
|---|
| 426 |
/* |
|---|
| 427 |
* This is a function that write a double from a socket. |
|---|
| 428 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 429 |
*/ |
|---|
| 430 |
int write_double(int sock, double d) { |
|---|
| 431 |
ssize_t err; |
|---|
| 432 |
|
|---|
| 433 |
err = write(sock,&d,sizeof(d)); |
|---|
| 434 |
|
|---|
| 435 |
if (err == 0) { |
|---|
| 436 |
easy_sock_err = 0; |
|---|
| 437 |
sprintf(easy_sock_err_msg,"No double written: why?\n"); |
|---|
| 438 |
} else if (err < 0) { |
|---|
| 439 |
easy_sock_err = errno; |
|---|
| 440 |
sprintf(easy_sock_err_msg,"Error writing double: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 441 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 442 |
} |
|---|
| 443 |
|
|---|
| 444 |
return (err); |
|---|
| 445 |
} |
|---|
| 446 |
|
|---|
| 447 |
/* |
|---|
| 448 |
* This is a function that write a string from a socket. |
|---|
| 449 |
* return the number of written chars, or a number < 0 if an error occours. |
|---|
| 450 |
* |
|---|
| 451 |
* Details: this function writes first the string length (int) then |
|---|
| 452 |
* the string itself, so read_string() can read it. |
|---|
| 453 |
*/ |
|---|
| 454 |
int write_string(int sock, char* string) { |
|---|
| 455 |
ssize_t err; |
|---|
| 456 |
size_t len = strlen(string); |
|---|
| 457 |
|
|---|
| 458 |
err = write(sock,&len,sizeof(len)); |
|---|
| 459 |
|
|---|
| 460 |
if (err == 0) { |
|---|
| 461 |
easy_sock_err = 0; |
|---|
| 462 |
sprintf(easy_sock_err_msg,"No string length written: why?\n"); |
|---|
| 463 |
return 0; |
|---|
| 464 |
} else if (err < 0) { |
|---|
| 465 |
easy_sock_err = errno; |
|---|
| 466 |
sprintf(easy_sock_err_msg,"Error writing string length: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 467 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 468 |
} |
|---|
| 469 |
|
|---|
| 470 |
err = write(sock,string,len); |
|---|
| 471 |
|
|---|
| 472 |
if (err == 0) { |
|---|
| 473 |
easy_sock_err = 0; |
|---|
| 474 |
sprintf(easy_sock_err_msg,"No string written: why?\n"); |
|---|
| 475 |
return 0; |
|---|
| 476 |
} else if (err < 0) { |
|---|
| 477 |
easy_sock_err = errno; |
|---|
| 478 |
sprintf(easy_sock_err_msg,"Error writing string: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 479 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 480 |
} |
|---|
| 481 |
|
|---|
| 482 |
return (err); |
|---|
| 483 |
} |
|---|
| 484 |
|
|---|
| 485 |
/* |
|---|
| 486 |
* This is a function that read a char from a socket. |
|---|
| 487 |
* For compatibility only: works exactly like read_char(). |
|---|
| 488 |
*/ |
|---|
| 489 |
char read_char_c(int sock) { |
|---|
| 490 |
return (read_char(sock)); |
|---|
| 491 |
} |
|---|
| 492 |
|
|---|
| 493 |
/* |
|---|
| 494 |
* This is a function that read a short (as chars) from a socket. |
|---|
| 495 |
*/ |
|---|
| 496 |
short read_short_c(int sock) { |
|---|
| 497 |
char s[ES_MAX_SHORT]; |
|---|
| 498 |
char c = 'S'; |
|---|
| 499 |
int i; |
|---|
| 500 |
ssize_t err; |
|---|
| 501 |
|
|---|
| 502 |
memset(s,0,ES_MAX_SHORT); |
|---|
| 503 |
|
|---|
| 504 |
for (i = 0; i < ES_MAX_SHORT && c != 0 && c != '\n'; i++) { |
|---|
| 505 |
err = read(sock,&c,ES_MAX_CHAR); |
|---|
| 506 |
|
|---|
| 507 |
if (err == 0) { |
|---|
| 508 |
sprintf(easy_sock_err_msg,"End of file reached reading short.\n"); |
|---|
| 509 |
err_handler(EOF); |
|---|
| 510 |
} else if (err < 0) { |
|---|
| 511 |
easy_sock_err = errno; |
|---|
| 512 |
sprintf(easy_sock_err_msg,"Error reading short: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 513 |
err_handler(easy_sock_err); |
|---|
| 514 |
} |
|---|
| 515 |
|
|---|
| 516 |
if (isdigit(c)) |
|---|
| 517 |
s[i] = c; |
|---|
| 518 |
else if (c != 0 && c != '\n') { |
|---|
| 519 |
sprintf(easy_sock_err_msg,"Error reading short: isdigit(%c) == FALSE\n",c); |
|---|
| 520 |
err_handler(1000+c); |
|---|
| 521 |
} |
|---|
| 522 |
|
|---|
| 523 |
} |
|---|
| 524 |
|
|---|
| 525 |
return (short)atoi(s); |
|---|
| 526 |
} |
|---|
| 527 |
|
|---|
| 528 |
/* |
|---|
| 529 |
* This is a function that read an integer (as chars) from a socket. |
|---|
| 530 |
*/ |
|---|
| 531 |
int read_int_c(int sock) { |
|---|
| 532 |
char j[ES_MAX_INT]; |
|---|
| 533 |
char c = 'I'; |
|---|
| 534 |
int i; |
|---|
| 535 |
ssize_t err; |
|---|
| 536 |
|
|---|
| 537 |
memset(j,0,ES_MAX_INT); |
|---|
| 538 |
|
|---|
| 539 |
for (i = 0; i < ES_MAX_INT && c != 0 && c != '\n'; i++) { |
|---|
| 540 |
err = read(sock,&c,ES_MAX_CHAR); |
|---|
| 541 |
|
|---|
| 542 |
if (err == 0) { |
|---|
| 543 |
sprintf(easy_sock_err_msg,"End of file reached reading short.\n"); |
|---|
| 544 |
err_handler(EOF); |
|---|
| 545 |
} else if (err < 0) { |
|---|
| 546 |
easy_sock_err = errno; |
|---|
| 547 |
sprintf(easy_sock_err_msg,"Error reading int: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 548 |
err_handler(easy_sock_err); |
|---|
| 549 |
} |
|---|
| 550 |
|
|---|
| 551 |
if (isdigit(c)) |
|---|
| 552 |
j[i] = c; |
|---|
| 553 |
else if (c != 0 && c != '\n') { |
|---|
| 554 |
sprintf(easy_sock_err_msg,"Error reading int: isdigit(%c) == FALSE\n",c); |
|---|
| 555 |
err_handler(1000+c); |
|---|
| 556 |
} |
|---|
| 557 |
|
|---|
| 558 |
} |
|---|
| 559 |
|
|---|
| 560 |
return atoi(j); |
|---|
| 561 |
} |
|---|
| 562 |
|
|---|
| 563 |
/* |
|---|
| 564 |
* This is a function that read a long (as chars) from a socket. |
|---|
| 565 |
*/ |
|---|
| 566 |
long read_long_c(int sock) { |
|---|
| 567 |
char l[ES_MAX_LONG]; |
|---|
| 568 |
char c = 'L'; |
|---|
| 569 |
int i; |
|---|
| 570 |
ssize_t err; |
|---|
| 571 |
|
|---|
| 572 |
memset(l,0,ES_MAX_LONG); |
|---|
| 573 |
|
|---|
| 574 |
for (i = 0; i < ES_MAX_LONG && c != 0 && c != '\n'; i++) { |
|---|
| 575 |
err = read(sock,&c,ES_MAX_CHAR); |
|---|
| 576 |
|
|---|
| 577 |
if (err == 0) { |
|---|
| 578 |
sprintf(easy_sock_err_msg,"End of file reached reading long.\n"); |
|---|
| 579 |
err_handler(EOF); |
|---|
| 580 |
} else if (err < 0) { |
|---|
| 581 |
easy_sock_err = errno; |
|---|
| 582 |
sprintf(easy_sock_err_msg,"Error reading long: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 583 |
err_handler(easy_sock_err); |
|---|
| 584 |
} |
|---|
| 585 |
|
|---|
| 586 |
if (isdigit(c)) |
|---|
| 587 |
l[i] = c; |
|---|
| 588 |
else if (c != 0 && c != '\n') { |
|---|
| 589 |
sprintf(easy_sock_err_msg,"Error reading long: isdigit(%c) == FALSE\n",c); |
|---|
| 590 |
err_handler(1000+c); |
|---|
| 591 |
} |
|---|
| 592 |
|
|---|
| 593 |
} |
|---|
| 594 |
|
|---|
| 595 |
return atol(l); |
|---|
| 596 |
} |
|---|
| 597 |
|
|---|
| 598 |
/* |
|---|
| 599 |
* This is a function that read a float (as chars) from a socket. |
|---|
| 600 |
*/ |
|---|
| 601 |
float read_float_c(int sock) { |
|---|
| 602 |
char f[ES_MAX_FLOAT]; |
|---|
| 603 |
char c = 'G'; |
|---|
| 604 |
int i; |
|---|
| 605 |
ssize_t err; |
|---|
| 606 |
|
|---|
| 607 |
memset(f,0,ES_MAX_FLOAT); |
|---|
| 608 |
|
|---|
| 609 |
for (i = 0; i < ES_MAX_FLOAT && c != 0 && c != '\n'; i++) { |
|---|
| 610 |
err = read(sock,&c,ES_MAX_CHAR); |
|---|
| 611 |
|
|---|
| 612 |
if (err == 0) { |
|---|
| 613 |
sprintf(easy_sock_err_msg,"End of file reached reading float.\n"); |
|---|
| 614 |
err_handler(EOF); |
|---|
| 615 |
} else if (err < 0) { |
|---|
| 616 |
easy_sock_err = errno; |
|---|
| 617 |
sprintf(easy_sock_err_msg,"Error reading float: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 618 |
err_handler(easy_sock_err); |
|---|
| 619 |
} |
|---|
| 620 |
|
|---|
| 621 |
f[i] = c; |
|---|
| 622 |
} |
|---|
| 623 |
|
|---|
| 624 |
return (float)atof(f); |
|---|
| 625 |
} |
|---|
| 626 |
|
|---|
| 627 |
/* |
|---|
| 628 |
* This is a function that read a double (as chars) from a socket. |
|---|
| 629 |
*/ |
|---|
| 630 |
double read_double_c(int sock) { |
|---|
| 631 |
char d[ES_MAX_DOUBLE]; |
|---|
| 632 |
char c = 'K'; |
|---|
| 633 |
int i; |
|---|
| 634 |
ssize_t err; |
|---|
| 635 |
|
|---|
| 636 |
memset(d,0,ES_MAX_DOUBLE); |
|---|
| 637 |
|
|---|
| 638 |
for (i = 0; i < ES_MAX_DOUBLE && c != 0 && c != '\n'; i++) { |
|---|
| 639 |
err = read(sock,&c,ES_MAX_CHAR); |
|---|
| 640 |
|
|---|
| 641 |
if (err == 0) { |
|---|
| 642 |
sprintf(easy_sock_err_msg,"End of file reached reading double.\n"); |
|---|
| 643 |
err_handler(EOF); |
|---|
| 644 |
} else if (err < 0) { |
|---|
| 645 |
easy_sock_err = errno; |
|---|
| 646 |
sprintf(easy_sock_err_msg,"Error reading double: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 647 |
err_handler(easy_sock_err); |
|---|
| 648 |
} |
|---|
| 649 |
|
|---|
| 650 |
d[i] = c; |
|---|
| 651 |
} |
|---|
| 652 |
|
|---|
| 653 |
return atof(d); |
|---|
| 654 |
} |
|---|
| 655 |
|
|---|
| 656 |
/* |
|---|
| 657 |
* This is a function that read a string (as chars) from a socket. |
|---|
| 658 |
* return a new 'mallocated' string or NULL if an error occours. |
|---|
| 659 |
* |
|---|
| 660 |
* Details: this function reads first the string length (as char) then |
|---|
| 661 |
* use malloc() to allocate new string so it can be read. |
|---|
| 662 |
* Remember! It's your own responsibility free() the string. |
|---|
| 663 |
*/ |
|---|
| 664 |
char* read_string_c(int sock) { |
|---|
| 665 |
size_t len; |
|---|
| 666 |
char slen[ES_MAX_LEN]; |
|---|
| 667 |
char c = 'T'; |
|---|
| 668 |
int i; |
|---|
| 669 |
ssize_t err; |
|---|
| 670 |
char* string; |
|---|
| 671 |
|
|---|
| 672 |
memset(slen,0,ES_MAX_LEN); |
|---|
| 673 |
|
|---|
| 674 |
for (i = 0; i < ES_MAX_LEN && c != 0 && c != '\n'; i++) { |
|---|
| 675 |
err = read(sock,&c,ES_MAX_CHAR); |
|---|
| 676 |
|
|---|
| 677 |
if (err == 0) { |
|---|
| 678 |
sprintf(easy_sock_err_msg,"End of file reached reading string length.\n"); |
|---|
| 679 |
err_handler(EOF); |
|---|
| 680 |
return NULL; |
|---|
| 681 |
} else if (err < 0) { |
|---|
| 682 |
easy_sock_err = errno; |
|---|
| 683 |
sprintf(easy_sock_err_msg,"Error reading string length: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 684 |
err_handler(easy_sock_err); |
|---|
| 685 |
return NULL; |
|---|
| 686 |
} |
|---|
| 687 |
|
|---|
| 688 |
if (isdigit(c)) |
|---|
| 689 |
slen[i] = c; |
|---|
| 690 |
else if (c != 0 && c != '\n') { |
|---|
| 691 |
sprintf(easy_sock_err_msg,"Error reading string length: isdigit(%c) == FALSE\n",c); |
|---|
| 692 |
err_handler(1000+c); |
|---|
| 693 |
} |
|---|
| 694 |
|
|---|
| 695 |
} |
|---|
| 696 |
|
|---|
| 697 |
len = atoi(slen); |
|---|
| 698 |
string = (char*)malloc(len+1); |
|---|
| 699 |
memset(string,0,len+1); |
|---|
| 700 |
|
|---|
| 701 |
err = read(sock,string,len); |
|---|
| 702 |
|
|---|
| 703 |
if (err == 0) { |
|---|
| 704 |
sprintf(easy_sock_err_msg,"End of file reached reading string.\n"); |
|---|
| 705 |
err_handler(EOF); |
|---|
| 706 |
return NULL; |
|---|
| 707 |
} else if (err < 0) { |
|---|
| 708 |
easy_sock_err = errno; |
|---|
| 709 |
sprintf(easy_sock_err_msg,"Error reading string: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 710 |
err_handler(easy_sock_err); |
|---|
| 711 |
return NULL; |
|---|
| 712 |
} |
|---|
| 713 |
string[len] = (char)0; |
|---|
| 714 |
|
|---|
| 715 |
return (string); |
|---|
| 716 |
} |
|---|
| 717 |
|
|---|
| 718 |
/* |
|---|
| 719 |
* This is a function that write a char (as chars) to a socket. |
|---|
| 720 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 721 |
* For compatibility only: works exactly like write_char(). |
|---|
| 722 |
*/ |
|---|
| 723 |
int write_char_c(int sock, char c) { |
|---|
| 724 |
return (write_char(sock,c)); |
|---|
| 725 |
} |
|---|
| 726 |
|
|---|
| 727 |
/* |
|---|
| 728 |
* This is a function that write a short (as chars) to a socket. |
|---|
| 729 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 730 |
*/ |
|---|
| 731 |
int write_short_c(int sock, short s) { |
|---|
| 732 |
ssize_t err; |
|---|
| 733 |
char ss[ES_MAX_SHORT]; |
|---|
| 734 |
int i; |
|---|
| 735 |
|
|---|
| 736 |
memset(ss,0,ES_MAX_SHORT); |
|---|
| 737 |
sprintf(ss,"%hi",s); |
|---|
| 738 |
|
|---|
| 739 |
for (i = 0; i < ES_MAX_SHORT && ss[i] != 0; i++) { |
|---|
| 740 |
err = write(sock,&ss[i],ES_MAX_CHAR); |
|---|
| 741 |
|
|---|
| 742 |
if (err == 0) { |
|---|
| 743 |
easy_sock_err = 0; |
|---|
| 744 |
sprintf(easy_sock_err_msg,"No short written: why?\n"); |
|---|
| 745 |
return 0; |
|---|
| 746 |
} else if (err < 0) { |
|---|
| 747 |
easy_sock_err = errno; |
|---|
| 748 |
sprintf(easy_sock_err_msg,"Error writing short: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 749 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 750 |
} |
|---|
| 751 |
} |
|---|
| 752 |
err = write(sock,"\n",ES_MAX_CHAR); |
|---|
| 753 |
|
|---|
| 754 |
if (err == 0) { |
|---|
| 755 |
easy_sock_err = 0; |
|---|
| 756 |
sprintf(easy_sock_err_msg,"No short-end written: why?\n"); |
|---|
| 757 |
} else if (err < 0) { |
|---|
| 758 |
easy_sock_err = errno; |
|---|
| 759 |
sprintf(easy_sock_err_msg,"Error writing short: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 760 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 761 |
} |
|---|
| 762 |
|
|---|
| 763 |
return (err); |
|---|
| 764 |
} |
|---|
| 765 |
|
|---|
| 766 |
/* |
|---|
| 767 |
* This is a function that write an integer (as chars) to a socket. |
|---|
| 768 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 769 |
*/ |
|---|
| 770 |
int write_int_c(int sock, int j) { |
|---|
| 771 |
ssize_t err; |
|---|
| 772 |
char si[ES_MAX_INT]; |
|---|
| 773 |
int i; |
|---|
| 774 |
|
|---|
| 775 |
memset(si,0,ES_MAX_INT); |
|---|
| 776 |
sprintf(si,"%i",j); |
|---|
| 777 |
|
|---|
| 778 |
for (i = 0; i < ES_MAX_INT && si[i] != 0; i++) { |
|---|
| 779 |
err = write(sock,&si[i],ES_MAX_CHAR); |
|---|
| 780 |
|
|---|
| 781 |
if (err == 0) { |
|---|
| 782 |
easy_sock_err = 0; |
|---|
| 783 |
sprintf(easy_sock_err_msg,"No int written: why?\n"); |
|---|
| 784 |
return 0; |
|---|
| 785 |
} else if (err < 0) { |
|---|
| 786 |
easy_sock_err = errno; |
|---|
| 787 |
sprintf(easy_sock_err_msg,"Error writing int: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 788 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 789 |
} |
|---|
| 790 |
} |
|---|
| 791 |
err = write(sock,"\n",ES_MAX_CHAR); |
|---|
| 792 |
|
|---|
| 793 |
if (err == 0) { |
|---|
| 794 |
easy_sock_err = 0; |
|---|
| 795 |
sprintf(easy_sock_err_msg,"No int-end written: why?\n"); |
|---|
| 796 |
} else if (err < 0) { |
|---|
| 797 |
easy_sock_err = errno; |
|---|
| 798 |
sprintf(easy_sock_err_msg,"Error writing int: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 799 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 800 |
} |
|---|
| 801 |
|
|---|
| 802 |
return (err); |
|---|
| 803 |
} |
|---|
| 804 |
|
|---|
| 805 |
/* |
|---|
| 806 |
* This is a function that write a long (as chars) to a socket. |
|---|
| 807 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 808 |
*/ |
|---|
| 809 |
int write_long_c(int sock, long l) { |
|---|
| 810 |
ssize_t err; |
|---|
| 811 |
char sl[ES_MAX_LONG]; |
|---|
| 812 |
int i; |
|---|
| 813 |
|
|---|
| 814 |
memset(sl,0,ES_MAX_LONG); |
|---|
| 815 |
sprintf(sl,"%li",l); |
|---|
| 816 |
|
|---|
| 817 |
for (i = 0; i < ES_MAX_LONG && sl[i] != 0; i++) { |
|---|
| 818 |
err = write(sock,&sl[i],ES_MAX_CHAR); |
|---|
| 819 |
|
|---|
| 820 |
if (err == 0) { |
|---|
| 821 |
easy_sock_err = 0; |
|---|
| 822 |
sprintf(easy_sock_err_msg,"No long written: why?\n"); |
|---|
| 823 |
return 0; |
|---|
| 824 |
} else if (err < 0) { |
|---|
| 825 |
easy_sock_err = errno; |
|---|
| 826 |
sprintf(easy_sock_err_msg,"Error writing long: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 827 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 828 |
} |
|---|
| 829 |
} |
|---|
| 830 |
err = write(sock,"\n",ES_MAX_CHAR); |
|---|
| 831 |
|
|---|
| 832 |
if (err == 0) { |
|---|
| 833 |
easy_sock_err = 0; |
|---|
| 834 |
sprintf(easy_sock_err_msg,"No long-end written: why?\n"); |
|---|
| 835 |
} else if (err < 0) { |
|---|
| 836 |
easy_sock_err = errno; |
|---|
| 837 |
sprintf(easy_sock_err_msg,"Error writing long: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 838 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 839 |
} |
|---|
| 840 |
|
|---|
| 841 |
return (err); |
|---|
| 842 |
} |
|---|
| 843 |
|
|---|
| 844 |
/* |
|---|
| 845 |
* This is a function that write a float (as chars) to a socket. |
|---|
| 846 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 847 |
*/ |
|---|
| 848 |
int write_float_c(int sock, float f) { |
|---|
| 849 |
ssize_t err; |
|---|
| 850 |
char sf[ES_MAX_FLOAT]; |
|---|
| 851 |
int i; |
|---|
| 852 |
|
|---|
| 853 |
memset(sf,0,ES_MAX_FLOAT); |
|---|
| 854 |
sprintf(sf,"%f",f); |
|---|
| 855 |
|
|---|
| 856 |
for (i = 0; i < ES_MAX_FLOAT && sf[i] != 0; i++) { |
|---|
| 857 |
err = write(sock,&sf[i],ES_MAX_CHAR); |
|---|
| 858 |
|
|---|
| 859 |
if (err == 0) { |
|---|
| 860 |
easy_sock_err = 0; |
|---|
| 861 |
sprintf(easy_sock_err_msg,"No float written: why?\n"); |
|---|
| 862 |
return 0; |
|---|
| 863 |
} else if (err < 0) { |
|---|
| 864 |
easy_sock_err = errno; |
|---|
| 865 |
sprintf(easy_sock_err_msg,"Error writing float: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 866 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 867 |
} |
|---|
| 868 |
} |
|---|
| 869 |
err = write(sock,"\n",ES_MAX_CHAR); |
|---|
| 870 |
|
|---|
| 871 |
if (err == 0) { |
|---|
| 872 |
easy_sock_err = 0; |
|---|
| 873 |
sprintf(easy_sock_err_msg,"No float-end written: why?\n"); |
|---|
| 874 |
} else if (err < 0) { |
|---|
| 875 |
easy_sock_err = errno; |
|---|
| 876 |
sprintf(easy_sock_err_msg,"Error writing float: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 877 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 878 |
} |
|---|
| 879 |
|
|---|
| 880 |
return (err); |
|---|
| 881 |
} |
|---|
| 882 |
|
|---|
| 883 |
/* |
|---|
| 884 |
* This is a function that write a double (as chars) to a socket. |
|---|
| 885 |
* return a number > 0 if ok, or a number <= 0 if an error occours. |
|---|
| 886 |
*/ |
|---|
| 887 |
int write_double_c(int sock, double d) { |
|---|
| 888 |
ssize_t err; |
|---|
| 889 |
char sd[ES_MAX_DOUBLE]; |
|---|
| 890 |
int i; |
|---|
| 891 |
|
|---|
| 892 |
memset(sd,0,ES_MAX_DOUBLE); |
|---|
| 893 |
sprintf(sd,"%f",d); |
|---|
| 894 |
|
|---|
| 895 |
for (i = 0; i < ES_MAX_DOUBLE && sd[i] != 0; i++) { |
|---|
| 896 |
err = write(sock,&sd[i],ES_MAX_CHAR); |
|---|
| 897 |
|
|---|
| 898 |
if (err == 0) { |
|---|
| 899 |
easy_sock_err = 0; |
|---|
| 900 |
sprintf(easy_sock_err_msg,"No double written: why?\n"); |
|---|
| 901 |
return 0; |
|---|
| 902 |
} else if (err < 0) { |
|---|
| 903 |
easy_sock_err = errno; |
|---|
| 904 |
sprintf(easy_sock_err_msg,"Error writing double: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 905 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 906 |
} |
|---|
| 907 |
} |
|---|
| 908 |
err = write(sock,"\n",ES_MAX_CHAR); |
|---|
| 909 |
|
|---|
| 910 |
if (err == 0) { |
|---|
| 911 |
easy_sock_err = 0; |
|---|
| 912 |
sprintf(easy_sock_err_msg,"No double-end written: why?\n"); |
|---|
| 913 |
} else if (err < 0) { |
|---|
| 914 |
easy_sock_err = errno; |
|---|
| 915 |
sprintf(easy_sock_err_msg,"Error writing double: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 916 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 917 |
} |
|---|
| 918 |
|
|---|
| 919 |
return (err); |
|---|
| 920 |
} |
|---|
| 921 |
|
|---|
| 922 |
/* |
|---|
| 923 |
* This is a function that write a string (as chars) to a socket. |
|---|
| 924 |
* return the number of written chars, or a number < 0 if an error occours. |
|---|
| 925 |
* |
|---|
| 926 |
* Details: this function writes first the string length (as chars) then |
|---|
| 927 |
* the string itself, so read_string_c() can read it. |
|---|
| 928 |
*/ |
|---|
| 929 |
int write_string_c(int sock, char* string) { |
|---|
| 930 |
ssize_t err; |
|---|
| 931 |
size_t len = strlen(string); |
|---|
| 932 |
char slen[ES_MAX_LEN]; |
|---|
| 933 |
int i; |
|---|
| 934 |
|
|---|
| 935 |
memset(slen,0,ES_MAX_LEN); |
|---|
| 936 |
sprintf(slen,"%i",len); |
|---|
| 937 |
|
|---|
| 938 |
for (i = 0; i < ES_MAX_LEN && slen[i] != 0; i++) { |
|---|
| 939 |
err = write(sock,&slen[i],ES_MAX_CHAR); |
|---|
| 940 |
|
|---|
| 941 |
if (err == 0) { |
|---|
| 942 |
easy_sock_err = 0; |
|---|
| 943 |
sprintf(easy_sock_err_msg,"No string length written: why?\n"); |
|---|
| 944 |
return 0; |
|---|
| 945 |
} else if (err < 0) { |
|---|
| 946 |
easy_sock_err = errno; |
|---|
| 947 |
sprintf(easy_sock_err_msg,"Error writing string length: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 948 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 949 |
} |
|---|
| 950 |
} |
|---|
| 951 |
err = write(sock,"\n",ES_MAX_CHAR); |
|---|
| 952 |
|
|---|
| 953 |
if (err == 0) { |
|---|
| 954 |
easy_sock_err = 0; |
|---|
| 955 |
sprintf(easy_sock_err_msg,"No string-length-end written: why?\n"); |
|---|
| 956 |
return 0; |
|---|
| 957 |
} else if (err < 0) { |
|---|
| 958 |
easy_sock_err = errno; |
|---|
| 959 |
sprintf(easy_sock_err_msg,"Error writing string length: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 960 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 961 |
} |
|---|
| 962 |
|
|---|
| 963 |
err = write(sock,string,len); |
|---|
| 964 |
|
|---|
| 965 |
if (err == 0) { |
|---|
| 966 |
easy_sock_err = 0; |
|---|
| 967 |
sprintf(easy_sock_err_msg,"No string written: why?\n"); |
|---|
| 968 |
} else if (err < 0) { |
|---|
| 969 |
easy_sock_err = errno; |
|---|
| 970 |
sprintf(easy_sock_err_msg,"Error writing string: %i - %s\n",easy_sock_err,strerror(errno)); |
|---|
| 971 |
return (easy_sock_err <= 0 ? (easy_sock_err) : -(easy_sock_err)); |
|---|
| 972 |
} |
|---|
| 973 |
|
|---|
| 974 |
return (err); |
|---|
| 975 |
} |
|---|
| 976 |
|
|---|
| 977 |
|
|---|
| 978 |
|
|---|