2 * WINGs connect.c: example how to create a netwrok client using WMConnection
4 * Copyright (c) 1999 Dan Pascu
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 static int initialized
= 0;
34 static void didReceiveInput(ConnectionDelegate
*self
, WMConnection
*cPtr
);
36 static void connectionDidDie(ConnectionDelegate
*self
, WMConnection
*cPtr
);
38 static void didInitialize(ConnectionDelegate
*self
, WMConnection
*cPtr
);
42 static ConnectionDelegate socketDelegate
= {
44 NULL
, /* didCatchException */
45 connectionDidDie
, /* didDie */
46 didInitialize
, /* didInitialize */
47 didReceiveInput
, /* didReceiveInput */
54 wAbort(Bool foo
) /*FOLD00*/
61 printHelp(char *progname
) /*FOLD00*/
63 printf("usage: %s [host [port]]\n\n", progname
);
64 printf(" --help print this message\n");
69 getMessage(WMConnection
*cPtr
)
75 aData
= WMGetConnectionAvailableData(cPtr
);
78 if ((length
=WMGetDataLength(aData
))==0) {
83 buffer
= (char*)wmalloc(length
+1);
84 WMGetDataBytes(aData
, buffer
);
93 inputHandler(int fd
, int mask
, void *clientData
)
95 WMConnection
*cPtr
= (WMConnection
*)clientData
;
103 n
= read(fd
, buf
, 4096);
105 aData
= WMCreateDataWithBytes(buf
, n
);
106 WMSendConnectionData(cPtr
, aData
);
107 WMReleaseData(aData
);
113 didReceiveInput(ConnectionDelegate
*self
, WMConnection
*cPtr
) /*FOLD00*/
117 buffer
= getMessage(cPtr
);
119 fprintf(stderr
, "Connection closed by peer.\n");
123 printf("%s", buffer
);
130 connectionDidDie(ConnectionDelegate
*self
, WMConnection
*cPtr
) /*FOLD00*/
132 WMCloseConnection(cPtr
);
134 fprintf(stderr
, "Connection closed by peer.\n");
140 didInitialize(ConnectionDelegate
*self
, WMConnection
*cPtr
)
142 int state
= WMGetConnectionState(cPtr
);
144 if (state
== WCConnected
) {
145 fprintf(stderr
, "connected to '%s:%s'\n", WMGetConnectionAddress(cPtr
),
146 WMGetConnectionService(cPtr
));
150 wsyserrorwithcode(WCErrorCode
, "Unable to connect");
157 main(int argc
, char **argv
) /*FOLD00*/
159 char *ProgName
, *host
, *port
;
165 WMInitializeApplication("connect", &argc
, argv
);
167 ProgName
= strrchr(argv
[0],'/');
177 for (i
=1; i
<argc
; i
++) {
178 if (strcmp(argv
[i
], "--help")==0) {
190 sPtr
= WMCreateConnectionToAddressAndNotify(host
, port
, NULL
);
192 wfatal("could not create connection. exiting");
196 WMSetConnectionDelegate(sPtr
, &socketDelegate
);
198 /* watch what user types and send it over the connection */
199 WMAddInputHandler(0, WIReadMask
, inputHandler
, sPtr
);