6

I want to create http socket connection for server in php for multiple client . how can I do that ? I need some resource .

First I was trying to create server in java .I create a server in java . And trying to reach from android application .But server can't find any client.But when I create client in java .It was working. How can I solve that problem ???

Diptopol Dam
  • 823
  • 1
  • 10
  • 39
  • You can look into Nanoserv http://nanoserv.si.kz/ or PEARs Net_Server http://pear.php.net/package/Net_Server/ – mario May 09 '11 at 13:28
  • 1
    Be sure to watch out for execution timeouts. Also, as far as I know it lacks threads that you see in most web servers. Another language would probably be a better choice if possible. – jocull May 09 '11 at 13:23
  • What webserver you are using? or you want to create your own web server? wouldn't it be reinventing the wheel? – Muhammad Ummar May 18 '11 at 09:07

5 Answers5

4

Take a look at this article:

Writing Socket Servers in PHP by Zend

Also give a try with Google:

http://www.google.com/search?aq=0&oq=php+socket+server+mul&sourceid=chrome&ie=UTF-8&q=php+socket+server+multiple+clients

Adnan
  • 25,882
  • 18
  • 81
  • 110
2

Personally I think this would be a pretty bad idea, as already mentioned it lacks Threading and it's Socket support (imo) isn't really that adaptable.

The only plus side is that you can use fork to fork off another PHP process to handle a client, but you're getting very complex.

Another language would be much more suited for this type of development.

Note that even if you did do this in PHP, you'd probably have to rely on external services anyway, and possibly even end up writing at least some code in another language anyway.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
1

though i love php and java, i wrote my socket servers in c++ running under lamp in an amazon ec2 cloud server. it is very, very simple to code and debug and safe and you can practically just copy/paste examples.

in the long run, i will probably develop a java solution because of portability and scalability, but the initial effort to get a c++ solution working is just so much less than implementing a java solution...

the first thing you must ascertain (find out) is whether your server allows you to open custom ports. amazon ec2 does and at this point in time (feb13), can be used for free for 12 months.

so, this is for you if you are in a hurry:

this here set of examples has all that you need to be up and running in no time.

tony gil
  • 9,424
  • 6
  • 76
  • 100
1

You're trying to use PHP to do what? Mind you, I like PHP and work with it almost every day, but please do remember PHP in and on itself is based on request and response, and not very suitable for long running processes. In a manner of exercise, it might be interesting, but if you're trying to write a webserver from scratch using PHP, you might want to reconsider your choice of language.

That said, you can create a socket acting as a server, and listen to incoming packets. I do still think you're reinventing the wheel though.

Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
  • There is http://nanoweb.si.kz/ and it did outperform Apache 2.0 on PHP4. While it's not a complete replacement for production settings, can be suitable for some app settings. – mario May 18 '11 at 10:09
  • I can name at *least* five different webservers that will outperform Apache 2.0. That doesn't mean it's a good idea to build it all over again. – Berry Langerak May 19 '11 at 08:40
  • @BerryLangerak u r absolutely right: php is NOT for this - even if u can get php code to open a socket 4 u and listen to it for a while – tony gil Feb 06 '13 at 15:23
0

Judging from the question title (the rest only makes it more confusing) you could use an existing package like http://pear.php.net/package/HTTP_Server to implement a webserver in PHP. It already contains all the socket code to accept client connections and stuff.

So what i have to do to find the server from different client

"Finding" is too broad a topic. Depends on your actual setting. On a LAN there are some protocols for discoverability. Otherwise you should just rely on a fixed machine name and port number for your instantiated server. You can connect to it as e.g. http://localhost:8007/ or whatever you've predefined.

mario
  • 144,265
  • 20
  • 237
  • 291