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>
37 /* Application header files */
38 #include "valerie_socket.h"
40 /** Initialise the socket.
43 valerie_socket
valerie_socket_init( char *server
, int port
)
45 valerie_socket socket
= malloc( sizeof( valerie_socket_t
) );
48 memset( socket
, 0, sizeof( valerie_socket_t
) );
50 socket
->server
= strdup( server
);
56 /** Connect to the server.
59 int valerie_socket_connect( valerie_socket connection
)
63 struct sockaddr_in sock
;
65 if ( connection
->server
!= NULL
)
67 host
= gethostbyname( connection
->server
);
69 memset( &sock
, 0, sizeof( struct sockaddr_in
) );
70 memcpy( &sock
.sin_addr
, host
->h_addr
, host
->h_length
);
71 sock
.sin_family
= host
->h_addrtype
;
72 sock
.sin_port
= htons( connection
->port
);
74 if ( ( connection
->fd
= socket( AF_INET
, SOCK_STREAM
, 0 ) ) != -1 )
75 ret
= connect( connection
->fd
, (const struct sockaddr
*)&sock
, sizeof( struct sockaddr_in
) );
83 /** Convenience constructor for a connected file descriptor.
86 valerie_socket
valerie_socket_init_fd( int fd
)
88 valerie_socket socket
= malloc( sizeof( valerie_socket_t
) );
91 memset( socket
, 0, sizeof( valerie_socket_t
) );
98 /** Read an arbitrarily formatted block of data from the server.
101 int valerie_socket_read_data( valerie_socket socket
, char *data
, int length
)
103 struct timeval tv
= { 1, 0 };
110 FD_SET( socket
->fd
, &rfds
);
112 if ( select( socket
->fd
+ 1, &rfds
, NULL
, NULL
, &tv
) )
114 used
= read( socket
->fd
, data
, length
- 1 );
124 /** Write an arbitrarily formatted block of data to the server.
127 int valerie_socket_write_data( valerie_socket socket
, char *data
, int length
)
131 while ( used
>=0 && used
< length
)
133 struct timeval tv
= { 1, 0 };
139 FD_SET( socket
->fd
, &rfds
);
141 FD_SET( socket
->fd
, &wfds
);
143 FD_SET( socket
->fd
, &efds
);
147 if ( select( socket
->fd
+ 1, &rfds
, &wfds
, &efds
, &tv
) )
149 if ( errno
!= 0 || FD_ISSET( socket
->fd
, &efds
) || FD_ISSET( socket
->fd
, &rfds
) )
153 else if ( FD_ISSET( socket
->fd
, &wfds
) )
155 int inc
= write( socket
->fd
, data
+ used
, length
- used
);
167 /** Close the socket.
170 void valerie_socket_close( valerie_socket socket
)
172 if ( socket
->fd
> 0 && !socket
->no_close
)
174 free( socket
->server
);