Files
wmaker/WINGs/Examples/connect.c
T

173 lines
3.4 KiB
C
Raw Normal View History

/*
1999-12-14 05:31:49 +00:00
* WINGs connect.c: example how to create a network client using WMConnection
2004-10-12 21:28:27 +00:00
*
2003-01-16 23:30:45 +00:00
* Copyright (c) 1999-2003 Dan Pascu
2004-10-12 21:28:27 +00:00
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <WINGs/WINGs.h>
static int initialized = 0;
2009-08-20 00:59:40 +02:00
static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr);
2009-08-20 00:59:40 +02:00
static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr);
2009-08-20 00:59:40 +02:00
static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr);
static ConnectionDelegate socketDelegate = {
2009-08-20 00:59:40 +02:00
NULL, /* data */
NULL, /* canResumeSending */
NULL, /* didCatchException */
connectionDidDie, /* didDie */
didInitialize, /* didInitialize */
didReceiveInput, /* didReceiveInput */
NULL /* didTimeout */
};
2009-08-20 00:59:40 +02:00
void wAbort(Bool foo)
{
2009-08-20 00:59:40 +02:00
exit(1);
}
2009-08-20 00:59:40 +02:00
static char *getMessage(WMConnection * cPtr)
{
2009-08-20 00:59:40 +02:00
char *buffer;
WMData *aData;
int length;
aData = WMGetConnectionAvailableData(cPtr);
if (!aData)
return NULL;
if ((length = WMGetDataLength(aData)) == 0) {
WMReleaseData(aData);
return NULL;
}
buffer = (char *)wmalloc(length + 1);
WMGetDataBytes(aData, buffer);
buffer[length] = '\0';
WMReleaseData(aData);
return buffer;
}
2009-08-20 00:59:40 +02:00
static void inputHandler(int fd, int mask, void *clientData)
{
2009-08-20 00:59:40 +02:00
WMConnection *cPtr = (WMConnection *) clientData;
WMData *aData;
char buf[4096];
int n;
if (!initialized)
return;
n = read(fd, buf, 4096);
if (n > 0) {
aData = WMCreateDataWithBytes(buf, n);
WMSendConnectionData(cPtr, aData);
WMReleaseData(aData);
}
}
2009-08-20 00:59:40 +02:00
static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr)
{
2009-08-20 00:59:40 +02:00
char *buffer;
2009-08-20 00:59:40 +02:00
buffer = getMessage(cPtr);
if (!buffer) {
fprintf(stderr, "Connection closed by peer.\n");
exit(0);
}
2009-08-20 00:59:40 +02:00
printf("%s", buffer);
2009-08-20 00:59:40 +02:00
wfree(buffer);
}
2009-08-20 00:59:40 +02:00
static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr)
{
2009-08-20 00:59:40 +02:00
WMCloseConnection(cPtr);
2009-08-20 00:59:40 +02:00
fprintf(stderr, "Connection closed by peer.\n");
exit(0);
}
2009-08-20 00:59:40 +02:00
static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr)
{
2009-08-20 00:59:40 +02:00
int state = WMGetConnectionState(cPtr);
WMHost *host;
if (state == WCConnected) {
host = WMGetHostWithAddress(WMGetConnectionAddress(cPtr));
fprintf(stderr, "connected to '%s:%s'\n",
host ? WMGetHostName(host) : WMGetConnectionAddress(cPtr), WMGetConnectionService(cPtr));
initialized = 1;
if (host)
WMReleaseHost(host);
return;
} else {
wsyserrorwithcode(WCErrorCode, "Unable to connect");
exit(1);
}
}
2009-08-20 00:59:40 +02:00
int main(int argc, char **argv)
{
2009-08-20 00:59:40 +02:00
char *ProgName, *host, *port;
int i;
WMConnection *sPtr;
2009-08-20 00:59:40 +02:00
wsetabort(wAbort);
2009-08-20 00:59:40 +02:00
WMInitializeApplication("connect", &argc, argv);
2009-08-20 00:59:40 +02:00
ProgName = strrchr(argv[0], '/');
if (!ProgName)
ProgName = argv[0];
else
ProgName++;
2009-08-20 00:59:40 +02:00
host = NULL;
port = "34567";
2009-08-20 00:59:40 +02:00
if (argc > 1) {
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
printf("usage: %s [host [port]]\n\n", ProgName);
exit(0);
} else {
if (!host)
host = argv[i];
else
port = argv[i];
}
}
}
2009-08-20 00:59:40 +02:00
printf("Trying to make connection to '%s:%s'\n", host ? host : "localhost", port);
1999-12-14 04:41:56 +00:00
2009-08-20 00:59:40 +02:00
sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
if (!sPtr) {
wfatal("could not create connection. exiting");
exit(1);
}
2009-08-20 00:59:40 +02:00
WMSetConnectionDelegate(sPtr, &socketDelegate);
2009-08-20 00:59:40 +02:00
/* watch what user types and send it over the connection */
WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
2009-08-20 00:59:40 +02:00
while (1) {
WHandleEvents();
}
2009-08-20 00:59:40 +02:00
return 0;
}