在Cocoa中构建服务器/客户端应用程序 [英] Building a Server/Client application in Cocoa

查看:75
本文介绍了在Cocoa中构建服务器/客户端应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个相当简单的程序,它监视一个文件夹,操作文件,因为它们被添加,并提供了一个简单的进度视图。通过NSOperation的子类来监视文件夹,该子类通过NSNotificationCenter将NSDictionary中的信息传递到我的进度视图。



现在我需要打破并运行我的服务器上的监视文件夹/处理部分,并构建一个客户端来监视来自多个工作站的进度。我的问题是我不知道如何完成这一点,我的搜索并不真正帮助我。



似乎我得到了很多过时的解决方案(WebObjects,Portable Distributed Objects)或不完整的信息。看起来我想使用NSStream来回传递数据,但是我在NSStream上找到的一切看起来像是为客户端设置,因为它正在寻找一个IP地址。

解决方案

我会建立一个服务器,建议您使用 TCP 来执行此操作。因为(我假设)你正在为BSD编写这个软件(Mac OS X和iPhone都是BSD),你可以使用BSD C套接字,或者一个Objective-C包装器。客户端的一个好的库是 CocoaAsyncSocket 。我个人为TCP网络编写了一个名为 SocketKit 的轻量级Objective-C套接字类。该库的使用如下:

  //打开连接
SKTCPSocket * socket = [[SKTCPSocket alloc ] initWithRemoteHost:@SERVER_IPport:SERVER_PORT];
//写数据
[socket writeData:someData];
//读取数据
NSData * someData = [socket readData:4];
//关闭套接字
[socket close];
[socket release];从服务器的角度来看,您可以使用 SKTCPSocketServer <端口>侦听端口

/ code> class:

  SKTCPSocket * aSocket = nil; 
SKTCPSocketServer * server = [[SKTCPSocketServer alloc] initListeningOnPort:1337];
@try {
[server listen];
while((aSocket =(SKTCPSocket *)[server acceptConnection])!= nil){
// a something with aSocket
[aSocket close];
}
} @catch(NSException * e){
NSLog(@Exception:%@,e);
}
[server stopServer];
[server release];

当然使用TCP套接字意味着编写自己的网络协议。一个简单的例子是发送一个四字节长度的字段,后跟一个NSDictionary的数据或某种性质的数据。这可以让你完成类似一个非常基本的分布式对象系统。


I built a fairly simple program that watches a folder, manipulates files as they are added, and gives a simple progress view of whats going on. The folder is watched via a subclass of NSOperation that passes information in an NSDictionary to my progress view, via the NSNotificationCenter.

Now I need to break things up and run the watched folder/processing part on my server, and build a client to monitor the progress from multiple workstations. My problem is I don't know how to accomplish this and my searches aren't really helping me.

It seems I'm getting a lot of out dated solutions (WebObjects, Portable Distributed Objects) or incomplete information. It seems like I'd want to use NSStream to pass data back and forth, but everything I find on NSStream looks like it's set up for client side because it's looking for an IP address. What would be the best way to go about setting up both a server, and a client to connect to it?

解决方案

I would suggest using TCP for something like this. Since (I assume) you are writing this software for BSD (Mac OS X and iPhone are both BSD) you can use BSD C sockets, or an Objective-C wrapper for this. One good library for a client is CocoaAsyncSocket. I personally have written a lightweight Objective-C socket class for TCP networking called SocketKit. Usage of this library is something as follows:

// open a connection
SKTCPSocket * socket = [[SKTCPSocket alloc] initWithRemoteHost:@"SERVER_IP" port:SERVER_PORT];
// write data
[socket writeData:someData];
// read data
NSData * someData = [socket readData:4];
// close the socket
[socket close];
[socket release];

From a server standpoint, you can listen on a port using the SKTCPSocketServer class:

SKTCPSocket * aSocket = nil;
SKTCPSocketServer * server = [[SKTCPSocketServer alloc] initListeningOnPort:1337];
@try {
    [server listen];
    while ((aSocket = (SKTCPSocket *)[server acceptConnection]) != nil) {
        // do something with aSocket
        [aSocket close];
    }
} @catch (NSException * e) {
    NSLog(@"Exception : %@", e);
}
[server stopServer];
[server release];

Of course using TCP sockets means writing your own network protocol. A simple example would be sending a four byte length field, followed by the data of an NSDictionary or something of that nature. This could allow you to accomplish something similar to a very basic Distributed Objects system.

这篇关于在Cocoa中构建服务器/客户端应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆