Live data from Hacker News

PHP Socket Programming, done the Right Way

christophh.net

1–10 of 15 posts

Re: PHP Socket Programming, done the Right Way

#6
Why not use a class? http://pear.php.net/package/Net_Socket

Or at least use namespaces. There is no reason to stick with the native PHP wrappers. It's common knowledge that PHP function names are illogical because of backward compatibility. So why not use a wrapper?

I think it's sad that even after PHP got all these features so that it resembles a proper programming languages, people aren't even using them! Makes me wonder if it was a good decision to force people to use classes in Java...

Re: PHP Socket Programming, done the Right Way

#7
I have a question regarding socket programming in general.

Let's say I want to build some sort of notification service. This could be instant messages, presence indication, or notification of file changes ala dropbox.

Now let's say that most of my clients are behind firewalls, forcing me to tie up one of my ports for each client as I do long polling or something else to keep the connection through their NAT open.

How do you keep from running out of ports? I only have 65K ports, right? How do some of these companies that have millions of open connections do it?

Re: PHP Socket Programming, done the Right Way

#8
post #7

I have a question regarding socket programming in general. Let's say I want to build some sort of notification service. This could be instant messages, presence indication, or notification of file changes ala dropbox. Now let's say that most of my clients are behind firewalls, forcing me to tie up one of my ports for each client as I do long polling or something else to keep the connection through their NAT open. How…

A connection is uniquely identified by (IP #1, Port #1) and (IP #2, Port #2).

You can have 65535 connections (in theory) from a single client to your port 80. And another 65535 from a different client to port 80 on the same server.

If you're serving a bunch of different clients it's not a problem.

Re: PHP Socket Programming, done the Right Way

#9

Why not use a class? http://pear.php.net/package/Net_Socket Or at least use namespaces. There is no reason to stick with the native PHP wrappers. It's common knowledge that PHP function names are illogical because of backward compatibility. So why not use a wrapper? I think it's sad that even after PHP got all these features so that it resembles a proper programming languages, people aren't even using them! Makes me…

You seem to be under the assumption that classes are always categorically better than a procedural API. I think you're mistaken. Classes aren't always a good abstraction.

Classes being a good abstraction:

    $user = new User('John', 'Doe');
    $user->authenticate($token);
Classes being a bad abstraction:

    $two = Math::add(1, 1);
    // or worse:
    $math = new Math;
    $three = $math->sqrt(9);
In that latter case, you'd be much better served using a functional or procedural API, even if it's namespaced:

    (+ 1 1) ; two
    (math/sqrt 9) ; three
If you're going to argue that `stream_socket_` is doing it wrong, I'd like to hear why it'd be better as a OO API.
Post reply on HN