2 * WINGs connect.c: example how to create a network 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 getMessage(WMConnection
*cPtr
)
67 aData
= WMGetConnectionAvailableData(cPtr
);
70 if ((length
=WMGetDataLength(aData
))==0) {
75 buffer
= (char*)wmalloc(length
+1);
76 WMGetDataBytes(aData
, buffer
);
85 inputHandler(int fd
, int mask
, void *clientData
)
87 WMConnection
*cPtr
= (WMConnection
*)clientData
;
95 n
= read(fd
, buf
, 4096);
97 aData
= WMCreateDataWithBytes(buf
, n
);
98 WMSendConnectionData(cPtr
, aData
);
105 didReceiveInput(ConnectionDelegate
*self
, WMConnection
*cPtr
) /*FOLD00*/
109 buffer
= getMessage(cPtr
);
111 fprintf(stderr
, "Connection closed by peer.\n");
115 printf("%s", buffer
);
122 connectionDidDie(ConnectionDelegate
*self
, WMConnection
*cPtr
) /*FOLD00*/
124 WMCloseConnection(cPtr
);
126 fprintf(stderr
, "Connection closed by peer.\n");
132 didInitialize(ConnectionDelegate
*self
, WMConnection
*cPtr
)
134 int state
= WMGetConnectionState(cPtr
);
137 if (state
== WCConnected
) {
138 host
= WMGetHostWithAddress(WMGetConnectionAddress(cPtr
));
139 fprintf(stderr
, "connected to '%s:%s'\n",
140 host
?WMGetHostName(host
):WMGetConnectionAddress(cPtr
),
141 WMGetConnectionService(cPtr
));
147 wsyserrorwithcode(WCErrorCode
, "Unable to connect");
154 main(int argc
, char **argv
) /*FOLD00*/
156 char *ProgName
, *host
, *port
;
162 WMInitializeApplication("connect", &argc
, argv
);
164 ProgName
= strrchr(argv
[0],'/');
174 for (i
=1; i
<argc
; i
++) {
175 if (strcmp(argv
[i
], "--help")==0 || strcmp(argv
[i
], "-h")==0) {
176 printf("usage: %s [host [port]]\n\n", ProgName
);
187 printf("Trying to make connection to '%s:%s'\n",
188 host
?host
:"localhost", port
);
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
);