2005-07-01 21:00:04 +00:00
|
|
|
/* Test/example program for the base library
|
|
|
|
|
|
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
|
|
|
|
2005-07-15 22:51:23 +00:00
|
|
|
Copying and distribution of this file, with or without modification,
|
|
|
|
are permitted in any medium without royalty provided the copyright
|
|
|
|
notice and this notice are preserved.
|
|
|
|
|
2005-07-01 21:00:04 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
*/
|
1996-03-01 16:26:49 +00:00
|
|
|
#include <stdio.h>
|
2003-07-31 23:49:32 +00:00
|
|
|
#include <GNUstepBase/TcpPort.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <Foundation/NSRunLoop.h>
|
2003-07-31 23:49:32 +00:00
|
|
|
#include <GNUstepBase/Invocation.h>
|
1996-03-12 19:20:07 +00:00
|
|
|
#include <Foundation/NSDate.h>
|
|
|
|
|
|
|
|
id handle_incoming_packet (id packet)
|
|
|
|
{
|
|
|
|
fprintf (stdout, "received >");
|
|
|
|
fwrite ([packet streamBuffer] + [packet streamBufferPrefix],
|
|
|
|
[packet streamEofPosition], 1, stdout);
|
|
|
|
fprintf (stdout, "<\n");
|
|
|
|
[packet release];
|
|
|
|
return nil;
|
|
|
|
}
|
1996-03-01 16:26:49 +00:00
|
|
|
|
1996-03-06 14:44:21 +00:00
|
|
|
int main (int argc, char *argv[])
|
1996-03-01 16:26:49 +00:00
|
|
|
{
|
1996-03-06 14:44:21 +00:00
|
|
|
id out_port;
|
|
|
|
id in_port;
|
1996-03-01 16:26:49 +00:00
|
|
|
id packet;
|
|
|
|
int i;
|
|
|
|
|
1996-03-06 14:44:21 +00:00
|
|
|
if (argc > 1)
|
2005-02-22 11:22:44 +00:00
|
|
|
out_port = [TcpOutPort newForSendingToRegisteredName:
|
2006-10-29 09:11:52 +00:00
|
|
|
[NSString stringWithUTF8String: argv[1]]
|
1996-03-06 14:44:21 +00:00
|
|
|
onHost: @"localhost"];
|
|
|
|
else
|
|
|
|
out_port = [TcpOutPort newForSendingToRegisteredName: @"tcpport-test"
|
1996-07-15 18:41:44 +00:00
|
|
|
onHost: nil];
|
1996-03-06 14:44:21 +00:00
|
|
|
|
|
|
|
in_port = [TcpInPort newForReceiving];
|
1996-03-12 19:20:07 +00:00
|
|
|
|
1996-03-13 02:42:35 +00:00
|
|
|
[in_port setReceivedPacketInvocation:
|
1996-03-12 19:20:07 +00:00
|
|
|
[[[ObjectFunctionInvocation alloc]
|
|
|
|
initWithObjectFunction: handle_incoming_packet]
|
|
|
|
autorelease]];
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
[[NSRunLoop currentRunLoop] addPort: in_port
|
|
|
|
forMode: NSDefaultRunLoopMode];
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-03-06 14:44:21 +00:00
|
|
|
for (i = 0; i < 10; i++)
|
1996-03-01 16:26:49 +00:00
|
|
|
{
|
1996-03-13 02:42:35 +00:00
|
|
|
packet = [[TcpOutPacket alloc] initForSendingWithCapacity: 100
|
|
|
|
replyInPort: in_port];
|
1996-03-06 14:44:21 +00:00
|
|
|
[packet writeFormat: @"Here is message number %d", i];
|
1997-09-01 21:59:51 +00:00
|
|
|
[out_port sendPacket: packet timeout: 10.0];
|
1996-03-01 16:26:49 +00:00
|
|
|
[packet release];
|
1996-03-06 14:44:21 +00:00
|
|
|
|
2002-02-26 10:38:50 +00:00
|
|
|
[[NSRunLoop currentRunLoop] runUntilDate:
|
|
|
|
[NSDate dateWithTimeIntervalSinceNow: 1.0]];
|
1996-03-01 16:26:49 +00:00
|
|
|
}
|
|
|
|
|
1996-03-06 14:44:21 +00:00
|
|
|
[out_port close];
|
1996-03-01 16:26:49 +00:00
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|