Up

Using netclasses synchronously

Authors

Andrew Ruder (aeruder@ksu.edu)

Version: Revision 1

Date: July 7, 2005

How to use netclasses synchronously.

Copyright: (C) Andrew Ruder


Contents -

  1. Introduction
  2. Ensure that Asynchronous Mode is Off
  3. Writing synchronously
  4. Reading synchronously

Introduction

While netclasses is primarily for asynchronous connections, it can somewhat easily be used for synchronous connections as well.

This is primarily done by directly calling the -[id<NetTransport> writeData:] and -[id<NetTransport> readData:] methods on the appropriate transport. This must be done while the object is not being handled by NetApplication .

Ensure that Asynchronous Mode is Off

The first thing to worry about is that the object is not being handled asynchronously by NetApplication . To ensure that this is the case, -[NetApplication disconnectObject:] should be called with the object we are interested in as the argument. This will remove it from the netclasses runloop. At this point, the object is ready to be written to and read from synchronously.

Writing synchronously

After disabling asynchronous mode, you can easily write any data you want to the transport using the -[id<NetTransport> writeData:] method with a NSData as the argument. However, none of the data is actually written out. To force the write out of the data, pass a 'nil' argument to -[id<NetTransport> writeData:] . To see if there is more data, use the -[id<NetTransport> isDoneWriting] method on the transport.

Reading synchronously

After disabling asynchronous mode, you can easily read data from the transport using the -[id<NetTransport> readData:] method. This method takes a single argument of the maximum number of bytes to read. Passing 0 will cause as much data as is available to be read.


Up