Java客户端和PHP服务器与套接字 [英] Java client and PHP server with sockets

查看:169
本文介绍了Java客户端和PHP服务器与套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找几个小时,只需要一些指导我的情况。我想创建一个简单的客户端/服务器程序。我最初计划在java中做这两个,但我从godaddy几个星期前买了一个共享的主机帐户,他们已禁用java的新帐户。所以,我想我的下一个最好的选择,在一种我熟悉的语言,是PHP。我一直遵循来自java网站的教程的套接字,并有java客户端。我一直试图将示例的服务器部分转换为PHP。显然,godaddy会让你使用fsockopen()在共享服务器上的套接字。我想我需要知道的是,这是可能的,我如何运行php文件一旦它被做?示例说,我需要在运行客户端之前启动服务器程序。我不知道如何做到这一点。

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.

推荐答案

我不会使用原始套接字做到这一点。而是使用JSON over HTTP,因为PHP支持处理HTTP而没有任何特殊考虑。在GoDaddy或Amazon EC2上的托管apache实例上运行PHP页面很简单。当然你可以使用套接字,但非常少的人实际上这样做。越来越多的人使用PHP处理和响应HTTP。这意味着,如果你跟随这里的牧群,你会发现更多的人可以帮助回答你的问题。还有两个API库,这样做很容易。使用套接字带来很多东西,你必须自己做,或忍受所有的奇怪的bug,第一次使用原始套接字。

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.

还有JSON处理很容易被Java和PHP支持,因为它很容易将数据发送到客户端和服务器。

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.

你可以在后端使用PHP, Java在前端,如果你喜欢,但我建议取消你的Go-Daddy帐户,并获得一个Amazon EC2 / S3帐户,因为你得到一个完整的机器专用于任何你想要的。所以如果你想在后端做Java,你只需要在亚马逊实例上安装JDK,Tomcat等,你就可以去了。你也可以在那里托管PHP。甚至有大量的AMI实例预先安装用于Java或PHP堆栈。

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.

这篇关于Java客户端和PHP服务器与套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆