 
   
   
   
   
   
   
   
   
   
   
CS185c
Chris Pollett
Mar 14, 2012
- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    // Override point for customization after application launch
    [window makeKeyAndVisible];
	CGRect bounds = [[UIScreen mainScreen ] applicationFrame];
	UIWebView *webView = [[UIWebView alloc] initWithFrame:bounds];
	webView.scalesPageToFit = YES;
		
    [window addSubview:webView];
	
	NSURL *url = [NSURL URLWithString:@"http://www.pollett.org"];
	NSURLRequest *request = [NSURLRequest requestWithURL:url];
	[webView loadRequest:request];
}
webView.delegate = myDelegate;
NSData *myData = [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://web_service_url"]];
UIImage *image = [UIImage imageWithData: myData];
NSString *string = [NSString stringWithFormat:@"%.*s", [myData length], [myData bytes]];
import java.io.*;
import java.net.*;
public final class MyServer extends Thread
{
   private static final int PORT=8889;
   private static final int NUM_CONNECT=1;
   private MyServer() {}
   public static void main(String args[])
   {
      MyServer myServer = new MyServer();
      if(myServer !=null) {myServer.start();}
   }
   public void run()
   {
      try
      {
         ServerSocket server = new ServerSocket(PORT, NUM_CONNECT);
         Socket client = server.accept();
         BufferedReader reader = new BufferedReader(
            new InputStreamReader(client.getInputStream()));
         PrintWriter writer = new PrintWriter(new BufferedWriter(
            new OutputStreamWriter(client.getOutputStream())));
         String line = reader.readLine();
         System.out.println("Hello"+line);
         if(line.equals("HELLO"))
         {
            writer.print("Chicago:New York:San Francisco:");
            writer.print("Chicago:New York:San Francisco:");
            writer.println("Chicago:New York:San Francisco\n");
         }
         writer.flush();
      }
      catch(IOException ie)
      {
         ie.printStackTrace();
      }
   }
}
CFSocketContext context = { 0, msg, NULL, NULL, NULL };
	
	
CFSocketRef client = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP,
     kCFSocketConnectCallBack, (CFSocketCallBack)ConnectCallBack, &context);
struct CFSocketContext {
   CFIndex version; // must be 0
   void *info; // data to send
   CFAllocatorRetainCallBack retain; // callback called when do CFRetain, CFRelease,CFCopy
   CFAllocatorReleaseCallBack release;
   CFAllocatorCopyDescriptionCallBack copyDescription;
};
CFSocketRef CFSocketCreate ( CFAllocatorRef allocator, SInt32 protocolFamily, //PF_INET is IPv4 SInt32 socketType, SInt32 protocol, CFOptionFlags callBackTypes, //when we want to be called back CFSocketCallBack callout, //our callback const CFSocketContext *context );
struct sockaddr_in theName;
struct hostent *hp;
CFDataRef addressData;
	
theName.sin_port = htons(8889);
theName.sin_family = AF_INET; //what kind in internet addresses to use; want IP
	
hp = gethostbyname("pollett.org");
if( hp == NULL ) {
   return;
}
memcpy( &theName.sin_addr.s_addr, hp->h_addr_list[0], hp->h_length );
	
addressData = CFDataCreate( NULL, (UInt8 *)&theName, (CFIndex)sizeof( struct sockaddr_in ) );
CFSocketConnectToAddress(client, addressData, 1); //connect; 1 second timeout CFRunLoopSourceRef sourceRef = CFSocketCreateRunLoopSource(kCFAllocatorDefault, client, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), sourceRef, kCFRunLoopCommonModes); CFRelease(sourceRef); CFRunLoopRun();
CFSocketNativeHandle sock;
UInt8 buffer[1024];
void ConnectCallBack(
   CFSocketRef socket,
   CFSocketCallBackType type,
   CFDataRef address,
   const void *data,
   void *info)
{
    char *msg = info;
	
    NSLog(@"%s\n", msg);
    sock = CFSocketGetNative(socket);
	
    send(sock, msg, strlen(msg)+1, 0);
    recv(sock, buffer, sizeof(buffer), 0);
    NSLog(@"Got: %s \n", buffer);
	
    CFRunLoopStop(CFRunLoopGetCurrent()); //return execution to our program
    return;
}
CFReadStreamRef readStream = NULL; CFWriteStreamRef writeStream = NULL; CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, &readStream, &writeStream);
NSOutputStream *networkOutputStream = (NSOutputStream *) writeStream; NSInputStream *networkInputStream =(NSInputStream *) readStream;and then work in Objective-C land.
#pragma mark - #pragma mark Table View Controller Methods
NSString *data = [[NSString alloc] initWithCString: (char*)buffer encoding: NSASCIIStringEncoding]; NSArray *array = [data componentsSeparatedByString:@":"]; [array retain]; self.listData = array; [array release]; [data release];
-(UITableViewCell *)tableView: (UITableView *)tableView
	cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
	
	UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
	
	if(cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
	   reuseIdentifier: SimpleTableIdentifier] autorelease];
	}
	
	NSUInteger row = [indexPath row];
	
	cell.textLabel.text = [listData objectAtIndex:row];
	return cell;
}