开发者

Java client and PHP server with sockets

开发者 https://www.devze.com 2023-03-24 05:35 出处:网络
I have been searching for hours now and just need some guidance about my situation. I want to create a simple client/server program. I was originally planning on doing them both in java, but I bought

I have been searching for hours now and just need some guidance about my situation. I want to create a simple client/server program. I was originally planning on doing them both in java, but I bought a shared hosting account from godaddy a couple of weeks ago and they have disabled java for new accounts. So, I guess my next best choice, and in a language that I'm somewhat familiar with, is PHP. I have been following the tutorial on sockets from the java site, and have the java client made. I've been trying to convert the server part of the example to PHP. Apparently, godaddy will let you use fsockopen() for sockets on a shared server. I guess what I need to know is, is this possible, and how do I run the php file once it is made? The example says I need to start the server program before I run the client. I'm not sure of how to do that though.开发者_JAVA百科


While I can't be 100% sure on this, I am going to go ahead and say that it probably won't work. Sockets can be on the more expensive side for a hosting company, so chances are they will be among the first things to be cut. When you need something acting as a socket server, you generally need a long-running process which has access to certain ports on the machine running the connection. Because most hosting companies create a chrooted environment for each of their clients, the clients are denied the security access which they would need to be able to run genuine socket servers.

My experiences with GoDaddy as a hosting company, is that they seem to operate along that line of thought. From what I can tell, GoDaddy will allow PHP to read sockets and act as a socket client. That is consistent with their allowance of fsockopen. I sincerely doubt that is what you need.

Unless you have an actual need to have the server push information to the client (instead of having the client request data from the server), then you really are much better off using the standard HTTP request and having either XML or JSON go over the wire (XML and JSON are both supported rather masterfully in PHP). If you really have to have a socketserver, then I would go to webmasters.stackexchange.com and see if you can find recommendations similar to this one.

If you do decide that you would like to create a PHP Socket Server after all that, there is a tutorial from one of the people at Zend. They're pretty sharp so hopefully it will be enough to help you convert the script (if that is the way you decide to go)


I would NOT use raw sockets to do this. Instead use JSON over HTTP because PHP supports processing HTTP without any special consideration. It's simple to run your PHP pages on the hosted apache instance on GoDaddy or Amazon EC2. Sure you can use sockets, but very very few people actually do that. Massively more people process and respond to HTTP with PHP. That means you'll find vastly more people who can help answer your questions if you follow the herd here. Also there are API libraries on both sides for doing this easily. Using sockets comes with plenty of things you'll have to do yourself or suffer through all the strange bugs that comes with working with raw sockets for the first time.

Also JSON processing is easily supported by both Java and PHP to it's very easy to send the data to the client and server using that.

Well you can certainly use PHP on the backend and Java on the frontend if you like, but I'd suggest canceling your Go-Daddy account and get an Amazon EC2/S3 account because you get a full machine dedicated to whatever you want to put on it. So if you want to do Java on the backend you can just by installing the JDK, Tomcat, etc yourself on the amazon instance and you're good to go. You can also host PHP on there too. There is even plenty of AMI instances pre-installed for Java or PHP stacks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号