Sending an NSImage on a Network

mindwalkernine
Unregistered
 
Post: #1
Hopefully there are some Cocoa game network programmers, because my question is about sending an NSImage from one computer to another.

Additionally, could someone help me out with the protocols. I don't know how to write them for this kind of thing.

Here's all of my server and client code:

Code:
---------- CLIENT CODE ----------
/* MyView */

#import <Cocoa/Cocoa.h>

@interface MyView : NSView
{    
    NSImage *myImage;
}

- (void)setImage:(NSImage *)image;

@end

#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(NSRect)frameRect
{
    if ((self = [super initWithFrame:frameRect]) != nil)
    {
        
    }
    
    return self;
}

- (void)setImage:(NSImage *)image
{
    [image retain];
    
    [myImage release];
    
    myImage = image;
    
    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect
{
    NSRect bounds = [self bounds];
    
    if(myImage)
    {
        NSRect imageRect;
        NSRect drawingRect;
        
        imageRect.origin = NSZeroPoint;
        imageRect.size = [myImage size];
        drawingRect = imageRect;
        [myImage
            drawInRect:drawingRect
            fromRect:imageRect
            operation:NSCompositeSourceOver
            fraction:1.0
        ];
    }
    else
        NSLog(@"Did not draw myImage.");
}

@end


/* Controller */

#import <Cocoa/Cocoa.h>

#import "MyView.h"

@interface Controller : NSObject
{
    NSSocketPort *port;
    NSConnection *connection;

    IBOutlet NSView *myView;
}

- (void)setImage:(NSImage *)image;

- (IBAction)vendService:(id)sender;

@end

#import "Controller.h"

@implementation Controller

- (void)setImage:(NSImage *)image
{
    [myView setImage:image];
}

- (IBAction)vendService:(id)sender
{
    port = [[NSSocketPort alloc] initWithTCPPort:12345];
    
    connection = [[NSConnection connectionWithReceivePort:port sendPort:nil] retain];
    
    [connection setRootObject:self];
    
    if(![[NSSocketPortNameServer sharedInstance] registerPort:port name:@"server"])
    {
        NSLog(@"Could not register name of server.");
    }
}

@end

---------- CLIENT CODE ----------
/* Controller */

#import <Cocoa/Cocoa.h>

@interface Controller : NSObject
{
    IBOutlet NSTextField *messageField;
    
    NSSocketPort *port;
    NSConnection *connection;
    NSDistantObject *proxy;
    
    NSImage *anImage;
}

- (IBAction)createProxy:(id)sender;
- (IBAction)sendImage:(id)sender;

@end


#import "Controller.h"

@implementation Controller

- (id)init
{
    self = [super init];
  
   if ( self )
    {
        anImage = [[NSImage alloc] initWithContentsOfFile:@"/users/jordanevans/desktop/snapshot.tiff"];
        [anImage retain];
    }
    
   return self;
}

- (IBAction)createProxy:(id)sender
{
    port = [[NSSocketPortNameServer sharedInstance] portForName:@"server" host:@"*"];
        
    connection = [NSConnection connectionWithReceivePort:nil sendPort:port];
        
    proxy = [[connection rootProxy] retain];
    
}

- (IBAction)sendImage:(id)sender
{
    if( ![proxy setImage:anImage] )
    {
        NSLog(@"Did not set send image.");
    }
}

@end
Quote this message in a reply
Member
Posts: 37
Joined: 2006.08
Post: #2
My general approach would be to use archivers and unarchivers. This makes sending objects over a network pretty trivial.

http://www.cocoadev.com/index.pl?UsingAr...narchivers
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  NSImage leaking? Cirdan 5 3,837 Nov 16, 2009 12:02 PM
Last Post: SethWillits
  Sending Data to a Website kodex 5 3,823 Apr 16, 2008 10:32 AM
Last Post: AnotherJake
  Animate An NSImage mindwalkernine 1 4,227 Jun 18, 2006 12:30 AM
Last Post: StealthyCoin
  NSImage and NSView problems Joseph Duchesne 1 3,913 Aug 19, 2005 07:36 AM
Last Post: unknown
  Faking a Z Coordinate With NSImage Nick 2 2,872 Jun 11, 2005 05:38 PM
Last Post: Nick