• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Making an NSConnection from a BSD socket
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Making an NSConnection from a BSD socket


  • Subject: Re: Making an NSConnection from a BSD socket
  • From: John Scalo <email@hidden>
  • Date: Thu, 25 Mar 2004 18:42:48 -0800

Thanks Douglas. That sounds promising. When I set up the NSSocketPort that way, everything happens error-free on the server side, but the client (different computer, local network) hangs when trying to get the proxy for the connection the first time (actually it times out eventually).

Here's how I'm setting up the connection (abbreviated with no error checking):

~~~
int newSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverAddress;
int namelen = sizeof(serverAddress);
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = MYPORT;
int one = 1;
int setOptionsResult = setsockopt(newSocket, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
int bindResult = bind(newSocket, (struct sockaddr *)&serverAddress, namelen);

NSSocketPort *newPort = [[[NSSocketPort alloc] initWithProtocolFamily:PF_INET socketType:SOCK_STREAM protocol:0 socket:newSocket] autorelease];

[self setRemoteConnection:[[[NSConnection alloc] initWithReceivePort:newPort sendPort:newPort] autorelease]];
~~~

Am I missing something? The client works fine when I set up the port via [[NSSocketPort alloc] initWithTCPPort: MYPORT] however then I can't reuse the port if/when the server dies for 10 minutes or so.

Thanks again
~ John

On Mar 25, 2004, at 6:24 PM, Douglas Davidson wrote:


On Mar 25, 2004, at 5:39 PM, John Scalo wrote:

I need to set some options on a BSD socket via setsockopt before binding it (otherwise setsockopt has no effect). I then want to use this socket to create an NSConnection. Is there any way to create an NSConnection from a BSD socket rather than an NSSocketPort? Or some way to turn a raw BSD socket into an NSSocketPort after it's created via socket() but before bind() ? The problem with using NSSocketPorts is that when you initialize them, they also get bound so there's no chance to set any socket options.

BTW, the option I'm setting is SO_REUSEPORT so that if the process dies with an active connection, it won't take 10 minutes before a new process can create a new connection with the same port #. It sure would be nice if NSSocketPort had an initWithTCPPort: options: method!

NSSocketPort has an initWithProtocolFamily:socketType:protocol:socket: method. That should qualify as a "way to turn a raw BSD socket into an NSSocketPort".

Douglas Davidson
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: Making an NSConnection from a BSD socket
      • From: Douglas Davidson <email@hidden>
    • Re: Making an NSConnection from a BSD socket
      • From: j o a r <email@hidden>
References: 
 >Making an NSConnection from a BSD socket (From: John Scalo <email@hidden>)
 >Re: Making an NSConnection from a BSD socket (From: Douglas Davidson <email@hidden>)

  • Prev by Date: Re: NSTableView
  • Next by Date: Truncating strings to fit a text field
  • Previous by thread: Re: Making an NSConnection from a BSD socket
  • Next by thread: Re: Making an NSConnection from a BSD socket
  • Index(es):
    • Date
    • Thread