- new function in WINGs: WMSetConnectionShutdownOnClose()
[wmaker-crm.git] / WINGs / Examples / connect.c
blobbe753fb80e102875a2000a001b8a5cc36ede5d20
1 /*
2 * WINGs connect.c: example how to create a network client using WMConnection
3 *
4 * Copyright (c) 1999-2002 Dan Pascu
5 *
6 */
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <string.h>
13 #include <WINGs/WINGs.h>
17 static int initialized = 0;
21 static void didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr);
23 static void connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr);
25 static void didInitialize(ConnectionDelegate *self, WMConnection *cPtr);
29 static ConnectionDelegate socketDelegate = {
30 NULL, /* data */
31 NULL, /* canResumeSending */
32 NULL, /* didCatchException */
33 connectionDidDie, /* didDie */
34 didInitialize, /* didInitialize */
35 didReceiveInput, /* didReceiveInput */
36 NULL /* didTimeout */
41 void
42 wAbort(Bool foo)
44 exit(1);
48 static char*
49 getMessage(WMConnection *cPtr)
51 char *buffer;
52 WMData *aData;
53 int length;
55 aData = WMGetConnectionAvailableData(cPtr);
56 if (!aData)
57 return NULL;
58 if ((length=WMGetDataLength(aData))==0) {
59 WMReleaseData(aData);
60 return NULL;
63 buffer = (char*)wmalloc(length+1);
64 WMGetDataBytes(aData, buffer);
65 buffer[length]= '\0';
66 WMReleaseData(aData);
68 return buffer;
72 static void
73 inputHandler(int fd, int mask, void *clientData)
75 WMConnection *cPtr = (WMConnection*)clientData;
76 WMData *aData;
77 char buf[4096];
78 int n;
80 if (!initialized)
81 return;
83 n = read(fd, buf, 4096);
84 if (n>0) {
85 aData = WMCreateDataWithBytes(buf, n);
86 WMSendConnectionData(cPtr, aData);
87 WMReleaseData(aData);
92 static void
93 didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr)
95 char *buffer;
97 buffer = getMessage(cPtr);
98 if (!buffer) {
99 fprintf(stderr, "Connection closed by peer.\n");
100 exit(0);
103 printf("%s", buffer);
105 wfree(buffer);
109 static void
110 connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr)
112 WMCloseConnection(cPtr);
114 fprintf(stderr, "Connection closed by peer.\n");
115 exit(0);
119 static void
120 didInitialize(ConnectionDelegate *self, WMConnection *cPtr)
122 int state = WMGetConnectionState(cPtr);
123 WMHost *host;
125 if (state == WCConnected) {
126 host = WMGetHostWithAddress(WMGetConnectionAddress(cPtr));
127 fprintf(stderr, "connected to '%s:%s'\n",
128 host?WMGetHostName(host):WMGetConnectionAddress(cPtr),
129 WMGetConnectionService(cPtr));
130 initialized = 1;
131 if (host)
132 WMReleaseHost(host);
133 return;
134 } else {
135 wsyserrorwithcode(WCErrorCode, "Unable to connect");
136 exit(1);
142 main(int argc, char **argv)
144 char *ProgName, *host, *port;
145 int i;
146 WMConnection *sPtr;
148 wsetabort(wAbort);
150 WMInitializeApplication("connect", &argc, argv);
152 ProgName = strrchr(argv[0],'/');
153 if (!ProgName)
154 ProgName = argv[0];
155 else
156 ProgName++;
158 host = NULL;
159 port = "34567";
161 if (argc>1) {
162 for (i=1; i<argc; i++) {
163 if (strcmp(argv[i], "--help")==0 || strcmp(argv[i], "-h")==0) {
164 printf("usage: %s [host [port]]\n\n", ProgName);
165 exit(0);
166 } else {
167 if (!host)
168 host = argv[i];
169 else
170 port = argv[i];
175 printf("Trying to make connection to '%s:%s'\n",
176 host?host:"localhost", port);
178 sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
179 if (!sPtr) {
180 wfatal("could not create connection. exiting");
181 exit(1);
184 WMSetConnectionDelegate(sPtr, &socketDelegate);
186 /* watch what user types and send it over the connection */
187 WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
189 while (1) {
190 WHandleEvents();
193 return 0;