changed indentation to use spaces only
[wmaker-crm.git] / WINGs / Examples / connect.c
blob14a2174f8b0cb8a5cdf7d4a9dd952de02090e5de
1 /*
2 * WINGs connect.c: example how to create a network client using WMConnection
4 * Copyright (c) 1999-2003 Dan Pascu
6 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
14 #include <WINGs/WINGs.h>
18 static int initialized = 0;
22 static void didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr);
24 static void connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr);
26 static void didInitialize(ConnectionDelegate *self, WMConnection *cPtr);
30 static ConnectionDelegate socketDelegate = {
31 NULL, /* data */
32 NULL, /* canResumeSending */
33 NULL, /* didCatchException */
34 connectionDidDie, /* didDie */
35 didInitialize, /* didInitialize */
36 didReceiveInput, /* didReceiveInput */
37 NULL /* didTimeout */
42 void
43 wAbort(Bool foo)
45 exit(1);
49 static char*
50 getMessage(WMConnection *cPtr)
52 char *buffer;
53 WMData *aData;
54 int length;
56 aData = WMGetConnectionAvailableData(cPtr);
57 if (!aData)
58 return NULL;
59 if ((length=WMGetDataLength(aData))==0) {
60 WMReleaseData(aData);
61 return NULL;
64 buffer = (char*)wmalloc(length+1);
65 WMGetDataBytes(aData, buffer);
66 buffer[length]= '\0';
67 WMReleaseData(aData);
69 return buffer;
73 static void
74 inputHandler(int fd, int mask, void *clientData)
76 WMConnection *cPtr = (WMConnection*)clientData;
77 WMData *aData;
78 char buf[4096];
79 int n;
81 if (!initialized)
82 return;
84 n = read(fd, buf, 4096);
85 if (n>0) {
86 aData = WMCreateDataWithBytes(buf, n);
87 WMSendConnectionData(cPtr, aData);
88 WMReleaseData(aData);
93 static void
94 didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr)
96 char *buffer;
98 buffer = getMessage(cPtr);
99 if (!buffer) {
100 fprintf(stderr, "Connection closed by peer.\n");
101 exit(0);
104 printf("%s", buffer);
106 wfree(buffer);
110 static void
111 connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr)
113 WMCloseConnection(cPtr);
115 fprintf(stderr, "Connection closed by peer.\n");
116 exit(0);
120 static void
121 didInitialize(ConnectionDelegate *self, WMConnection *cPtr)
123 int state = WMGetConnectionState(cPtr);
124 WMHost *host;
126 if (state == WCConnected) {
127 host = WMGetHostWithAddress(WMGetConnectionAddress(cPtr));
128 fprintf(stderr, "connected to '%s:%s'\n",
129 host?WMGetHostName(host):WMGetConnectionAddress(cPtr),
130 WMGetConnectionService(cPtr));
131 initialized = 1;
132 if (host)
133 WMReleaseHost(host);
134 return;
135 } else {
136 wsyserrorwithcode(WCErrorCode, "Unable to connect");
137 exit(1);
143 main(int argc, char **argv)
145 char *ProgName, *host, *port;
146 int i;
147 WMConnection *sPtr;
149 wsetabort(wAbort);
151 WMInitializeApplication("connect", &argc, argv);
153 ProgName = strrchr(argv[0],'/');
154 if (!ProgName)
155 ProgName = argv[0];
156 else
157 ProgName++;
159 host = NULL;
160 port = "34567";
162 if (argc>1) {
163 for (i=1; i<argc; i++) {
164 if (strcmp(argv[i], "--help")==0 || strcmp(argv[i], "-h")==0) {
165 printf("usage: %s [host [port]]\n\n", ProgName);
166 exit(0);
167 } else {
168 if (!host)
169 host = argv[i];
170 else
171 port = argv[i];
176 printf("Trying to make connection to '%s:%s'\n",
177 host?host:"localhost", port);
179 sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
180 if (!sPtr) {
181 wfatal("could not create connection. exiting");
182 exit(1);
185 WMSetConnectionDelegate(sPtr, &socketDelegate);
187 /* watch what user types and send it over the connection */
188 WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
190 while (1) {
191 WHandleEvents();
194 return 0;