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];
从服务器的角度来看,你可以听从使用<代码>的港口。 SKTCPSocketServer 班级:
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];
当然,使用TCP袖珍片意味着撰写自己的网络议定书。 一个简单的例子就是分四长的外地,然后是NSDictionary或这种性质的一部分。 这将使你能够完成类似于基本分配物体系统的工作。