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>
35 #include <netinet/in.h>
38 /* Application header files */
39 #include "valerie_socket.h"
41 /** Initialise the socket.
44 valerie_socket valerie_socket_init( char *server, int port )
46 valerie_socket socket = malloc( sizeof( valerie_socket_t ) );
49 memset( socket, 0, sizeof( valerie_socket_t ) );
51 socket->server = strdup( server );
57 /** Connect to the server.
60 int valerie_socket_connect( valerie_socket connection )
64 struct sockaddr_in sock;
66 if ( connection->server != NULL )
68 host = gethostbyname( connection->server );
70 memset( &sock, 0, sizeof( struct sockaddr_in ) );
71 memcpy( &sock.sin_addr, host->h_addr, host->h_length );
72 sock.sin_family = host->h_addrtype;
73 sock.sin_port = htons( connection->port );
75 if ( ( connection->fd = socket( AF_INET, SOCK_STREAM, 0 ) ) != -1 )
76 ret = connect( connection->fd, (const struct sockaddr *)&sock, sizeof( struct sockaddr_in ) );
84 /** Convenience constructor for a connected file descriptor.
87 valerie_socket valerie_socket_init_fd( int fd )
89 valerie_socket socket = malloc( sizeof( valerie_socket_t ) );
92 memset( socket, 0, sizeof( valerie_socket_t ) );
99 /** Read an arbitrarily formatted block of data from the server.
102 int valerie_socket_read_data( valerie_socket socket, char *data, int length )
104 struct timeval tv = { 1, 0 };
111 FD_SET( socket->fd, &rfds );
113 if ( select( socket->fd + 1, &rfds, NULL, NULL, &tv ) )
115 used = read( socket->fd, data, length - 1 );
125 /** Write an arbitrarily formatted block of data to the server.
128 int valerie_socket_write_data( valerie_socket socket, const char *data, int length )
132 while ( used >=0 && used < length )
134 struct timeval tv = { 1, 0 };
140 FD_SET( socket->fd, &rfds );
142 FD_SET( socket->fd, &wfds );
144 FD_SET( socket->fd, &efds );
148 if ( select( socket->fd + 1, &rfds, &wfds, &efds, &tv ) )
150 if ( errno != 0 || FD_ISSET( socket->fd, &efds ) || FD_ISSET( socket->fd, &rfds ) )
154 else if ( FD_ISSET( socket->fd, &wfds ) )
156 int inc = write( socket->fd, data + used, length - used );
168 /** Close the socket.
171 void valerie_socket_close( valerie_socket socket )
173 if ( socket->fd > 0 && !socket->no_close )
175 free( socket->server );