Initial import to Gerrit.
[profile/ivi/festival.git] / src / arch / festival / client.cc
1 /*************************************************************************/
2 /*                                                                       */
3 /*                Centre for Speech Technology Research                  */
4 /*                     University of Edinburgh, UK                       */
5 /*                       Copyright (c) 1996,1997                         */
6 /*                        All Rights Reserved.                           */
7 /*                                                                       */
8 /*  Permission is hereby granted, free of charge, to use and distribute  */
9 /*  this software and its documentation without restriction, including   */
10 /*  without limitation the rights to use, copy, modify, merge, publish,  */
11 /*  distribute, sublicense, and/or sell copies of this work, and to      */
12 /*  permit persons to whom this work is furnished to do so, subject to   */
13 /*  the following conditions:                                            */
14 /*   1. The code must retain the above copyright notice, this list of    */
15 /*      conditions and the following disclaimer.                         */
16 /*   2. Any modifications must be clearly marked as such.                */
17 /*   3. Original authors' names are not deleted.                         */
18 /*   4. The authors' names are not used to endorse or promote products   */
19 /*      derived from this software without specific prior written        */
20 /*      permission.                                                      */
21 /*                                                                       */
22 /*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
23 /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
24 /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
25 /*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
26 /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
27 /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
28 /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
29 /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
30 /*  THIS SOFTWARE.                                                       */
31 /*                                                                       */
32 /*************************************************************************/
33 /*                Author :  Alan Black (with Richard Tobin)              */
34 /*                Date   :  December 1996                                */
35 /*-----------------------------------------------------------------------*/
36 /*                                                                       */
37 /* Low level client functions, separated from the other scoket functions */
38 /* so things that link with these don't need the whole system            */
39 /*                                                                       */
40 /*=======================================================================*/
41 #include <cstdlib>
42 #include <cstdio>
43 #include <cerrno>
44 #include <sys/types.h>
45 #include <cstring>
46 #include <ctime>
47 #include "EST_unix.h"
48 #include "EST_socket.h"
49 #include "festival.h"
50 #include "festivalP.h"
51
52 static EST_Regex ipnum("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+");
53
54 int festival_socket_client(const char *host,int port)
55 {
56     // Return an FD to a remote server
57     struct sockaddr_in serv_addr;
58     struct hostent *serverhost;
59     EST_String shost;
60     int fd;
61
62     if (!socket_initialise())
63       {
64         festival_error();
65       }
66     fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
67
68     if (NOT_A_SOCKET(fd))
69       {
70         int n = socket_error();
71         cerr << "socket: socket failed (" << n << ")\n";
72         festival_error();
73       }
74     memset(&serv_addr, 0, sizeof(serv_addr));
75     shost = host;
76     if (shost.matches(ipnum))
77         serv_addr.sin_addr.s_addr = inet_addr(host);
78     else
79     {
80         serverhost = gethostbyname(host);
81         if (serverhost == (struct hostent *)0)
82         {
83             cerr << "socket: gethostbyname failed" << endl;
84             festival_error();
85         }
86         memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
87     }
88     serv_addr.sin_family = AF_INET;
89     serv_addr.sin_port = htons(port);
90
91     if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0)
92     {
93         cerr << "socket: connect failed" << endl;
94         festival_error();
95     }
96
97     return fd;
98 }
99
100