home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Papers / Garrison / Code / SchmoozingExamples / Listing1.m < prev    next >
Encoding:
Text File  |  2001-06-23  |  5.3 KB  |  135 lines

  1. //
  2. //  Listing1.m
  3. //  SchmoozingExamples
  4. //
  5. //  Created by garrison on Fri Apr 20 2001.
  6. //  Copyright (c) 2001 Standard Orbit Software, LLC. All rights reserved.
  7. //
  8. //  Permission is granted to use this code for any purpose, at your own risk.
  9. //  No warranties are expressed or implied.
  10.  
  11. #import <Foundation/Foundation.h>
  12. #import <OmniNetworking/OmniNetworking.h>
  13.  
  14. int main (int argc, const char * argv[])
  15. {
  16.     // TCP Client Using OmniNetworking
  17.     
  18.     NSAutoreleasePool *mainPool;
  19.     ONHost *serverHost;
  20.     ONTCPSocket *connectionSocket;
  21.     NSString *serverHostname = @"www.machack.com";
  22.     unsigned short serverPort = 80;
  23.     
  24.     mainPool = [[NSAutoreleasePool alloc] init];
  25.   
  26.     NS_DURING {
  27.         serverHost = [ONHost hostForHostname:serverHostname];
  28.         // Step 1. Get the Internet address of the server.
  29.     }
  30.     NS_HANDLER {
  31.         // hostForHostname: can throw any of four exceptions
  32.         //
  33.         // ONHostNotFoundExceptionName
  34.         // - covers three error conditions from the resolver library: 
  35.         //     a) the name doesn't exist in the host name resolution database, 
  36.         //     b) a temporary error from which recovery might possible,
  37.         //     c) a unrecoverable error was returned by the resolver library.
  38.         //
  39.         // ONHostNameLookupErrorExceptionName
  40.         // - The resolver library returned some unspecified error.
  41.         //
  42.         // ONHostHasNoAddressExceptionName
  43.         // - The host name was found but the database has no Internet address 
  44.         // associated with it.
  45.         //
  46.         // ONGetHostByNameNotFoundExceptionName
  47.         // - The ONGetHostByName tool on which OmniNetworking relies for thread-safe
  48.         // name lookups cannot be found, indicating bad build or installation of the
  49.         // framework.
  50.         
  51.  //       NSLog(@"hostForHostname: raised %@ (%@)", [localException name], localException );
  52.         exit(1);
  53.     }
  54.     NS_ENDHANDLER
  55.     
  56.  
  57.   
  58.     NS_DURING {
  59.         connectionSocket = [ONTCPSocket tcpSocket];
  60.         // Step 2. Allocate a socket.
  61.         
  62.         [connectionSocket setLocalPortNumber];
  63.         // Step 3. Configure the socket to use an arbitrary local port number.
  64.         
  65.         [connectionSocket connectToHost:serverHost port:serverPort];
  66.         // Step 4. Connect to the server.
  67.     }
  68.     NS_HANDLER {
  69.         // tcpSocket: throws ONInternetSocketConnectFailedExceptionName
  70.         // if the underlying called to socket() returns a -1.
  71.         
  72.         // setLocalPortNumber: throws ONInternetSocketBindFailedExceptionName
  73.         // if the socket cannot be bound to a local port number
  74.         
  75.         // connectToHost: throws 
  76.         // - ONInternetSocketConnectFailedExceptionName if the host argument is bad
  77.         // - ONInternetSocketConnectTemporarilyFailedExceptionName if no connection
  78.         // could be made using any of host's known Internet addresses.
  79.         
  80.      //  NSLog(@"tcpSocket: raised %@ (%@)", [localException name], localException );
  81.         exit(1);
  82.     }
  83.     NS_ENDHANDLER
  84.     
  85.     
  86.     // Step 5. Communicate with server using some application protocol.  In this example,
  87.     // I'll use HTTP to request the root page from the MacHack web site.  I'll use
  88.     // ONTCPSocket's NSString-based I/O methods
  89.     
  90.     NS_DURING {
  91.         NSString *httpRequest = @"GET / HTTP 1.0\r\n\r\n";
  92.         NSMutableString *httpReply = [NSMutableString string];
  93.         NSString *chunk;
  94.         
  95.         [connectionSocket writeString: httpRequest];
  96.         // Send the HTTP request to the server.
  97.                 
  98.         while ( chunk = [connectionSocket readString] )
  99.                 [httpReply appendString: chunk];
  100.         // Read the response.  readString: will read up to 2048 bytes from the socket 
  101.         // at a time, unless the read buffer size has been changed.  TCP is a byte stream
  102.         // protocol, so you'll need to read until there are no more bytes on the socket
  103.         // to ensure that you have a complete message.  readString: returns nil when there
  104.         // are no more bytes on the socket.  The first call to readString: will block 
  105.         // the program until there are bytes available to read.  Use ONTCPSocket's
  106.         // waitForInputWithTimeout: method to set up a timer on the first read to keep
  107.         // your application from locking up on a slow or unresponsive connection.
  108.         // "www.hp.com" is a good site to use for slow response testing.
  109.         
  110.         NSLog(@"%@", httpReply);
  111.     }
  112.     NS_HANDLER {
  113.         // Either readString: and writeString: can throw...
  114.         // ONInternetSocketUserAbortExceptionName, if the connection is
  115.         // aborted (via abortSocket:), or
  116.         // ONInternetSocketWouldBlockExceptionName, if the socket is in non-blocking
  117.         // mode and a read or write would have caused the socket to block.
  118.         //
  119.         // readString: (and the other read methods) throws
  120.         // ONInternetSocketReadFailedExceptionName if the read fails.
  121.         //
  122.         // writeString: (and the other write methods) throws
  123.         // ONInternetSocketWriteFailedExceptionName if the write fails.
  124.         
  125.        // NSLog(@"Socket I/O raised %@ (%@)", [localException name], localException);
  126.         exit(1);        
  127.     }
  128.     NS_ENDHANDLER
  129.     
  130.     [mainPool release];
  131.     // Step 6. The connection socket is closed when the pool is released.
  132.     
  133.     return(0);
  134.     exit(0);
  135. }