2 * valerie_socket.c -- Client Socket
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* System header files */
31 #include <sys/types.h>
32 #include <sys/socket.h>
36 /* Application header files */
37 #include "valerie_socket.h"
39 /** Initialise the socket.
42 valerie_socket
valerie_socket_init( char *server
, int port
)
44 valerie_socket socket
= malloc( sizeof( valerie_socket_t
) );
47 memset( socket
, 0, sizeof( valerie_socket_t
) );
49 socket
->server
= strdup( server
);
55 /** Connect to the server.
58 int valerie_socket_connect( valerie_socket connection
)
62 struct sockaddr_in sock
;
64 if ( connection
->server
!= NULL
)
66 host
= gethostbyname( connection
->server
);
68 memset( &sock
, 0, sizeof( struct sockaddr_in
) );
69 memcpy( &sock
.sin_addr
, host
->h_addr
, host
->h_length
);
70 sock
.sin_family
= host
->h_addrtype
;
71 sock
.sin_port
= htons( connection
->port
);
73 if ( ( connection
->fd
= socket( AF_INET
, SOCK_STREAM
, 0 ) ) != -1 )
74 ret
= connect( connection
->fd
, (const struct sockaddr
*)&sock
, sizeof( struct sockaddr_in
) );
82 /** Convenience constructor for a connected file descriptor.
85 valerie_socket
valerie_socket_init_fd( int fd
)
87 valerie_socket socket
= malloc( sizeof( valerie_socket_t
) );
90 memset( socket
, 0, sizeof( valerie_socket_t
) );
97 /** Read an arbitrarily formatted block of data from the server.
100 int valerie_socket_read_data( valerie_socket socket
, char *data
, int length
)
102 struct timeval tv
= { 1, 0 };
109 FD_SET( socket
->fd
, &rfds
);
111 if ( select( socket
->fd
+ 1, &rfds
, NULL
, NULL
, &tv
) )
113 used
= read( socket
->fd
, data
, length
- 1 );
123 /** Write an arbitrarily formatted block of data to the server.
126 int valerie_socket_write_data( valerie_socket socket
, char *data
, int length
)
130 while ( used
>=0 && used
< length
)
132 struct timeval tv
= { 1, 0 };
138 FD_SET( socket
->fd
, &rfds
);
140 FD_SET( socket
->fd
, &wfds
);
142 FD_SET( socket
->fd
, &efds
);
146 if ( select( socket
->fd
+ 1, &rfds
, &wfds
, &efds
, &tv
) )
148 if ( errno
!= 0 || FD_ISSET( socket
->fd
, &efds
) || FD_ISSET( socket
->fd
, &rfds
) )
152 else if ( FD_ISSET( socket
->fd
, &wfds
) )
154 int inc
= write( socket
->fd
, data
+ used
, length
- used
);
166 /** Close the socket.
169 void valerie_socket_close( valerie_socket socket
)
171 if ( socket
->fd
> 0 && !socket
->no_close
)
173 free( socket
->server
);