Professional Web Applications Themes

I'm having problems with sockets - Mac Programming

I've been playing with sockets, and I have a problem. When I try to create a socket port like this: incomingSocket = [[NSSocketPort alloc] initWithTCPPort:PORT_NUMBER]; I find that 'incomingSocket' only gets created if PORT_NUMBER is zero (i.e incomingSocket==nil). For a zero value, according to Foundation.pdf, a port number is selected by the system. My problem is that I want to use a *specific* port number - I'm working on a Usenet client, and IIRC NNTP usually uses port 119. (Yes, I realise the above is for local working - I'm trying to keep things simple at this stage.) So how ...

  1. #1

    Default I'm having problems with sockets

    I've been playing with sockets, and I have a problem.

    When I try to create a socket port like this:

    incomingSocket = [[NSSocketPort alloc] initWithTCPPort:PORT_NUMBER];

    I find that 'incomingSocket' only gets created if PORT_NUMBER is zero
    (i.e incomingSocket==nil).

    For a zero value, according to Foundation.pdf, a port number is selected
    by the system. My problem is that I want to use a *specific* port
    number - I'm working on a Usenet client, and IIRC NNTP usually uses
    port 119. (Yes, I realise the above is for local working - I'm trying
    to keep things simple at this stage.) So how do I successfully create
    a socket configured for the port I want?

    I'm fairly new to the use of sockets and ports, so I expect I'm
    making a simple error. I would be extremely grateful if someone
    here could kindly tell me what that error is!

    Stuart, Obj-C newbie, using Xcode under Panther.

    Stuart Guest

  2. #2

    Default Re: I'm having problems with sockets

    On Sun, 1 Aug 2004, Stuart Rogers wrote:
     

    On unix systems, only root is allowed to open ports for listening below
    1024. That is why your initialization is failing.

    However, for an NNTP *client*, you don't need to listen on any ports at
    all, you need to connect to the NNTP server. For that, you'd use a method
    like -initRemoteWithTCPPort:host:.

    However Part 2, NSSocketPort is intended for use with NSConnection and
    does not make a good way to do raw TCP communications. If you don't need
    compatibility below OS X 10.3, check out NSStream. If you do, you can
    either use raw sockets with the C-based API, or you can use one of the
    many third-party ObjC sockets libraries, of which several are linked to
    here: <http://www.cocoadev.com/index.pl?SocketClasses>.
    Michael Guest

  3. #3

    Default Re: I'm having problems with sockets

    In article <BD33229F.2FC2%com>,
    Stuart Rogers <com> wrote:
     

    On most Unix systems, only the superuser is allowed to allocate ports in
    this range.
    David Guest

  4. #4

    Default Re: I'm having problems with sockets

    "Michael Ash" <com> wrote: 
    >
    > On unix systems, only root is allowed to open ports for listening below
    > 1024. That is why your initialization is failing.[/ref]

    D'oh! And in one move we see the limits of my knowledge of sockets!
    Thanks!
     

    I know, but as I don't have a permanently on internet connection
    I wanted to make a local test rig that my main code could talk to
    before letting it loose on innocent news servers.
     

    NSStream is something that's missed my stream of consciousness,
    but I'll certainly check it out. I haven't tried using NSConnection
    as all the example source code I've found using it dealt specifically
    with local inter-app communications, rather than talking to a strange
    and foreign server. Not only that, but none of these examples
    was clear on how messages are sent and returned from an NSConnection.
     

    I am aware of such things, but (a) this is for me an exercise in
    Obj-C, and (b) I want to keep my reliance on third party material
    to a minimum, in the hope of maximising my learning. Otherwise,
    that's a perfectly good suggestion!

    Thanks for your help - I can stop tearing my hair out now.

    Stuart

    Stuart Guest

  5. #5

    Default Re: I'm having problems with sockets

    On Mon, 2 Aug 2004, Stuart Rogers wrote:
     
    >
    > I know, but as I don't have a permanently on internet connection
    > I wanted to make a local test rig that my main code could talk to
    > before letting it loose on innocent news servers.[/ref]

    Gotcha. That certainly seems reasonable.
     
    >
    > NSStream is something that's missed my stream of consciousness,
    > but I'll certainly check it out. I haven't tried using NSConnection
    > as all the example source code I've found using it dealt specifically
    > with local inter-app communications, rather than talking to a strange
    > and foreign server. Not only that, but none of these examples
    > was clear on how messages are sent and returned from an NSConnection.[/ref]

    NSConnection does normally work with Mach ports for local inter-app
    communication, but it can be used with NSSocketPort to communicate with
    apps on a remote machine. The catch, however, is that both sides must be
    using Cocoa and NSConnection on the link; you can't use NSConnection to
    talk to an NNTP server or any other "normal" TCP server program.

    NSConnection sends and receives messages by using proxy objects that
    forward messages. Once a conneciton is set up, you can do something like:

    id obj = [connection rootProxy];
    [obj someMethodWithObj:x andThing:y]; // gets sent to the other program

    Of course, this doesn't help your particular case at all.
     
    >
    > I am aware of such things, but (a) this is for me an exercise in
    > Obj-C, and (b) I want to keep my reliance on third party material
    > to a minimum, in the hope of maximising my learning. Otherwise,
    > that's a perfectly good suggestion!
    >
    > Thanks for your help - I can stop tearing my hair out now.[/ref]

    I'm happy to be of service.
    Michael Guest

  6. #6

    Default Re: I'm having problems with sockets

    "Michael Ash" <com> wrote: 
    >>
    >> NSStream is something that's missed my stream of consciousness,
    >> but I'll certainly check it out. I haven't tried using NSConnection
    >> as all the example source code I've found using it dealt specifically
    >> with local inter-app communications, rather than talking to a strange
    >> and foreign server. Not only that, but none of these examples
    >> was clear on how messages are sent and returned from an NSConnection.[/ref]
    >
    > NSConnection does normally work with Mach ports for local inter-app
    > communication, but it can be used with NSSocketPort to communicate with
    > apps on a remote machine. The catch, however, is that both sides must be
    > using Cocoa and NSConnection on the link; you can't use NSConnection to
    > talk to an NNTP server or any other "normal" TCP server program.[/ref]

    That was the conclusion I had drawn - thanks for confirming it.
     
    >
    > I'm happy to be of service.[/ref]

    Look out for more stupid questions! (Assuming work doesn't
    get in the way of my programming attempts.)

    Stuart

    Stuart Guest

  7. #7

    Default Re: I'm having problems with sockets

    Stuart Rogers wrote: 
    >
    > I know, but as I don't have a permanently on internet connection
    > I wanted to make a local test rig that my main code could talk to
    > before letting it loose on innocent news servers.[/ref]

    there are two options. first, if you're writing your client for general
    consumption, it should allow itself to be configured to use server ports
    other than 119. sometimes a company will set up an nntp server for
    internal use that uses a non-standard port number for security reasons.
    at the very least, you could set up compile-time defines that allow you
    to build a version of your client that's hard-coded to some port other
    than 119. then you can configure your client and server to agree on
    some port number that's greater than 1000, so you've gotten around the
    unix security problem.

    the other solution is to run your server simulator using 'sudo', which
    will give it access to low-numbered ports.
    Jhnny Guest

  8. #8

    Default Re: I'm having problems with sockets

    "Jhnny Fvrt (it means "halo, then resonate")" <com>
    wrote: 
    >>
    >> I know, but as I don't have a permanently on internet connection
    >> I wanted to make a local test rig that my main code could talk to
    >> before letting it loose on innocent news servers.[/ref]
    >
    > there are two options. first, if you're writing your client for general
    > consumption, it should allow itself to be configured to use server ports
    > other than 119. sometimes a company will set up an nntp server for
    > internal use that uses a non-standard port number for security reasons.
    > at the very least, you could set up compile-time defines that allow you
    > to build a version of your client that's hard-coded to some port other
    > than 119. then you can configure your client and server to agree on
    > some port number that's greater than 1000, so you've gotten around the
    > unix security problem.[/ref]

    I was planning to have a user-configurable port, but at the
    moment I'm in a hard-coded quick'n'dirty experimental phase.
     

    Hmm... As much as I trust my own programming skills, I think
    I'll *not* use sudo for experimenting!

    I've just been taking a look at the doentation for NSStream
    that Michael pointed out (AFAIK it's only doented on Apple's
    online docs - it's not filtered down into any off-line PDFs).
    They look quite simple to use, and geared towards my needs.

    Stuart

    Stuart Guest

  9. #9

    Default Re: I'm having problems with sockets

    On Tue, 3 Aug 2004, Stuart Rogers wrote:
     

    I don't know about the PDFs, I don't use them, but I have the full
    NSStream doentation available in Xcode's doentation window.
    Michael Guest

  10. #10

    Default Re: I'm having problems with sockets

    "Michael Ash" <com> wrote: 
    >
    > I don't know about the PDFs, I don't use them, but I have the full
    > NSStream doentation available in Xcode's doentation window.[/ref]

    I'm pretty sure the pdf I downloaded today didn't mention them,
    and the version I've been working from up to know certainly
    doesn't. I find the PDFs easier to navigate than the webby-
    style docs. But I'm weird like that.

    Stuart.

    Stuart Guest

Similar Threads

  1. WSE 2.0TP using raw sockets
    By Eugene Bilenko in forum ASP.NET Web Services
    Replies: 0
    Last Post: October 29th, 08:56 PM
  2. udp-sockets
    By Christian in forum UNIX Programming
    Replies: 2
    Last Post: October 11th, 06:39 AM
  3. IRC & SOCKETS
    By Paulo in forum PHP Development
    Replies: 2
    Last Post: October 7th, 10:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139