English 中文(简体)
• 如何在CFSocket上安装导航服务器安装,以便有多个链接
原标题:How to fix naive server implementation with CFSocket to allow multiple connections

我一直在研究从2010年冬天起从斯坦福第193p课程(iOS节目)第17号讲座中得出的骨 the/NSStream样本代码。 The example Code is available

样本法以营养脂重计,设定了海面表,对0港进行约束,以便给它一个自由港。 然后使用NSNetservice(bonjour)向该港口发布服务。 也于备份时开始。 现有服务载于<编码>可调取的。 在选定一个单位时,相应的服务即得到解决,建立了,并可发送数据。

This is a naive implementation because connections are rejected if a connection already exists. My question is, what is the proper way to handle multiple connections? Once multiple clients are connected to the server, how does the server distinguish between them? i.e. How can data be sent specifically to one client and not the others?

- (void) _acceptConnection:(int)fd
{
    int     junk;

// If we already have a connection, reject this new one.  This is one of the 
// big simplifying assumptions in this code.  A real server should handle 
// multiple simultaneous connections.

    if ( self.isReceiving ) {
        junk = close(fd);
        assert(junk == 0);
    } else {
        [self _startReceive:fd];
    }
}


// Called by CFSocket when someone connects to our listening socket.  
// This implementation just bounces the request up to Objective-C.
static void AcceptCallback(CFSocketRef s, 
                           CFSocketCallBackType type, 
                           CFDataRef address, 
                           const void *data, 
                           void *info)

{
    ReceiveServer *  obj;

    assert(type == kCFSocketAcceptCallBack);

    assert(data != NULL);

    obj = (ReceiveServer *) info;
    assert(obj != nil);

    assert(s == obj->_listeningSocket);


    [obj _acceptConnection:*(int *)data];
}
最佳回答

I m not specifically familiar with that course or sample code, but: separate out the code for handling a connection into a different class from the code which accepts new connections. So, in the posted code, you d move the -startReceive: method and everything which it calls to another class.

然后,每次接受联系时,都会发现另一类关系。 这一案件将负责处理这方面的所有来文。 将在初始阶段向其提供有关联系(主要是fd)的信息。 服务器的主要控制器物体可以在一个阵列中控制这些情形。

从这里来的东西将取决于您的服务器实际做些什么。 至少,您的联系对象在主控制器关闭时需要告知主控制器,以便主控制人能够将其从阵列中删除。 你可以为此使用通知或代表模式。

问题回答

暂无回答




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签