- Added example of using WMCOnnection for a server like program.
[wmaker-crm.git] / WINGs / Examples / connect.c
blob6a0063e12358558788d312ebe73544779833a35a
1 /*
2 * WINGs connect.c: example how to create a network client using WMConnection
3 *
4 * Copyright (c) 1999-2001 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, /* didCatchException */
32 connectionDidDie, /* didDie */
33 didInitialize, /* didInitialize */
34 didReceiveInput, /* didReceiveInput */
35 NULL /* didTimeout */
40 void
41 wAbort(Bool foo) /*FOLD00*/
43 exit(1);
47 static char*
48 getMessage(WMConnection *cPtr)
50 char *buffer;
51 WMData *aData;
52 int length;
54 aData = WMGetConnectionAvailableData(cPtr);
55 if (!aData)
56 return NULL;
57 if ((length=WMGetDataLength(aData))==0) {
58 WMReleaseData(aData);
59 return NULL;
62 buffer = (char*)wmalloc(length+1);
63 WMGetDataBytes(aData, buffer);
64 buffer[length]= '\0';
65 WMReleaseData(aData);
67 return buffer;
71 static void
72 inputHandler(int fd, int mask, void *clientData)
74 WMConnection *cPtr = (WMConnection*)clientData;
75 WMData *aData;
76 char buf[4096];
77 int n;
79 if (!initialized)
80 return;
82 n = read(fd, buf, 4096);
83 if (n>0) {
84 aData = WMCreateDataWithBytes(buf, n);
85 WMSendConnectionData(cPtr, aData);
86 WMReleaseData(aData);
91 static void
92 didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
94 char *buffer;
96 buffer = getMessage(cPtr);
97 if (!buffer) {
98 fprintf(stderr, "Connection closed by peer.\n");
99 exit(0);
102 printf("%s", buffer);
104 wfree(buffer);
108 static void
109 connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
111 WMCloseConnection(cPtr);
113 fprintf(stderr, "Connection closed by peer.\n");
114 exit(0);
118 static void
119 didInitialize(ConnectionDelegate *self, WMConnection *cPtr)
121 int state = WMGetConnectionState(cPtr);
122 WMHost *host;
124 if (state == WCConnected) {
125 host = WMGetHostWithAddress(WMGetConnectionAddress(cPtr));
126 fprintf(stderr, "connected to '%s:%s'\n",
127 host?WMGetHostName(host):WMGetConnectionAddress(cPtr),
128 WMGetConnectionService(cPtr));
129 initialized = 1;
130 if (host)
131 WMReleaseHost(host);
132 return;
133 } else {
134 wsyserrorwithcode(WCErrorCode, "Unable to connect");
135 exit(1);
141 main(int argc, char **argv) /*FOLD00*/
143 char *ProgName, *host, *port;
144 int i;
145 WMConnection *sPtr;
147 wsetabort(wAbort);
149 WMInitializeApplication("connect", &argc, argv);
151 ProgName = strrchr(argv[0],'/');
152 if (!ProgName)
153 ProgName = argv[0];
154 else
155 ProgName++;
157 host = NULL;
158 port = "34567";
160 if (argc>1) {
161 for (i=1; i<argc; i++) {
162 if (strcmp(argv[i], "--help")==0 || strcmp(argv[i], "-h")==0) {
163 printf("usage: %s [host [port]]\n\n", ProgName);
164 exit(0);
165 } else {
166 if (!host)
167 host = argv[i];
168 else
169 port = argv[i];
174 printf("Trying to make connection to '%s:%s'\n",
175 host?host:"localhost", port);
177 sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
178 if (!sPtr) {
179 wfatal("could not create connection. exiting");
180 exit(1);
183 WMSetConnectionDelegate(sPtr, &socketDelegate);
185 /* watch what user types and send it over the connection */
186 WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
188 while (1) {
189 WHandleEvents();
192 return 0;