Connecting from an iPhone client to a PHP server through sockets - php

In one of my projects a PHP server is successfully serving a php client via sockets but when I try an iPhone client using the NSStreams to this PHP server the client gets connected but if I send a string from the client to server, the xcode comes up with error message that connection is broken.
If I use the same iPhone client to connect to a server running on Python, it establishes the connection and strings are being read/written without any problem.
Can someone understand this please? If some sample or tutorial on PHP / iOS applications on sockets then I will be obliged.

hi you can use WebSocketServer located on http://code.google.com/p/phpwebsocket/ and on Iphone clien http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-servert

i cant repost my code here, because SO considers it spamming to repeat your answer.
check out this sample code and tutorial link. works like a charm and is really simple to implement, less than 3 minutes and you are up and going (IF you have a socket server ready).
as a general rule, php might not be the best solution for a socket server. i like working with c++ or python better. just holler if you need some sample code for a socket server using c++.
also, it is VERY VERY VERY important to check if your server has open ports. unless you are using your own server, chances are that your hosting package does NOT open ports. a virtual private server is necessary for that. at this time, i strongly recommend amazon's AWS EC2 servers.
in case you have difficulties with it, i can give you a couple of nudges in the right direction.

Related

How can I use my php webapp to interact with an API on localhost?

I've written a webapp in php, which works as a standalone app, but needs to get/post new data from time to time to an API (written by a different company, for desktop software) on localhost. When connecting, it would be from a webpage/browser open on that machine.
So far I've attempted to send curl requests, basically using the generated postman curl code, and it's returning false. I'm thinking it's because it can't connect to the localhost server, possibly because it's blocked from the internet.
I've been researching for a few days, but want a solution that doesn't involve me creating security vulnerabilities in the computers I am trying to connect to, by opening them directly to the internet, and if there is a way to send requests to localhost without installing more software on them I would be partial to that kind of setup.
Does this make sense? I hope someone has some experience or wisdom in this area that could at least nudge me in the right direction.
The localhost is always the computer where you refer to that host. You can't connect to the localhost from your webapp, because the localhost hostname for that app is the computer which runs it.
You have to open a tunnel from you computer to the internet where you webapp can communicate with it.
You can use ngrok to do that, for example

Websockets can't connect to server

I am trying to obtain some education on websockets but I can't really find a decent tutorial. All tutorials out there instruct you to download a server and teach you how to use it. I'm more interested in how the server actually works and how I can control it on a remote host, like a hosting provider.
Anyway if I wanna learn there really aren't many options other than to download a built server and start inspecting. So I downloaded the server given in this tutorial http://dev.tutsplus.com/tutorials/start-using-html5-websockets-today--net-13270
The problem for now is that I can't even seem to connect to it. I'm running XAMPP on Win7, so it's completely relevant to the tutorial and appears quite easy, however I fail. I've managed to start the server, but when I open client.php I get
Firefox can't establish a connection to the server at
ws://localhost:8000/pol/newWS/server/startDaemon.php. var socket =
new WebSocket(host);
Fortunately chrome appears to provide a more detailed error message
WebSocket connection to 'ws://localhost:8000/pol/newWS/server/startDaemon.php' failed: Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing
Javascript's host variable is set to 'ws://localhost:8000/pol/newWS/server/startDaemon.php', that's where the server is located on my localhost. I haven't made any changes to the server. Can anyone help me get this thing going? Thanks in advance!
Websockets in PHP are generally a pain in the ass. Even if you use a library like rachet you need to start it separately, not through a webserver.
As brad said, consider using node.js or the excellent autobahn.ws for python.
I just found some WS server I have downloaded in the past which works pretty well. I can't confirm for any security issues but if anyone knows or finds anything please don't spare us the sharing. I searched for some info in the comments but I couldn't find anything about the creator so I can't really give any credits.
files here

TCP Holepunch implementation

I'm working on an idea of mine which is comparable to a home automation system.
The layout of network devices would be like this:
What I want is for my Mobile Phone App to be able to communicate with the home server at all times, but also for the server to be able to reach my phone (Push-notifications).
For this I thought it would be a good idea to implement TCP-Holepunching using my server in the datacenter as the center for traffic.
The problem is though, Since my Java skills aren't good enough yet, I'm forced to write my server in the PHP scripting language, but PHP isn't capable of keeping sockets active.
Is there TCP-holepunching server software (executable in Debian) available which would be able to keep the sockets alive and push any commands from my server towards the correct destination (ie: phone) and vice versa?
There are ways to get sockets working in PHP. One library that does this can be found here:
http://socketo.me/
Also an easy way of setting a server like this up can be done with NodeJS. You code your server in JavaScript (run on Google Chrome's V8 Engine) and then you can handle persistent connections based on events.
It should be possible (with NodeJS) to code a relay server (relaying from your phone to your home PC and vice versa) with only a few lines of code.

Is there a way to connect sockets from perl script to a PHP server?

I am new to the concept of web socket connections and this would be of real help to me.
I have XAMPP running on a local machine.
How can i connect via a socket to the APACHE server on this machine ?
I am not getting any leads. Is there a tutorial to set that up ?
Also how do I control what happens when a socket connection is made towards the server side ?
It sounds like you want to connect to a web server on localhost and fetch a document from it. The easiest way to do that is to not worry about interacting with sockets directly and instead use a prepackaged web client library, such as LWP or LWP::Simple.

PHP Websocket on Webserver

A few days ago I setup this WebSocket server from http://code.google.com/p/phpwebsocket/
It works excellent on my localhost by using Xampp. Then, I uploaded it to my webspace on Strato, but now I am not able to connect to the server.
I changed the the sockets connection to my domain but it didn`t work
Client.html:
var host = "ws://xxxxxxxx.com:12345/Websocket/server.php";
Server.php:
$master = WebSocket("xxxxxx",12345);
I already tried it with different port(80,443,12345,8080,8000....), using the IP address in server.php instead of the domain.
I also used absolute and relative paths in the client.html.
From the projects page http://code.google.com/p/phpwebsocket/ someone suggested to use 0 or 0.0.0.0 in the server.php, but that didn`t work either.
In the client.html it just shows:
WebSocket - status 0
Disconnected - status 3
My guess that the problem is that Strato is blocks me from using WebSockets.
Is it possible to run WebSockets on my webspace?
My bet is that your host is blocking incoming requests to the socket. Your not supposed to run scripts like phpwebsocket within a web service (it is a server itself). This confuses a lot of people since PHP is typically used for scripting webpages, not for coding daemons.
I say this because you mentioned running the script within Xampp, and referred to your hosting service as 'webspace'.
If this is the case you will have to upgrade to a virtual server package so that you can run your own services.
Okay, to bring this to an end.
I found a solution to use my websockets app and I´ve got an explanation why it did not work on my shared hosting solution.
I contacted Strato who told me that they don`t allow Websocket on shared hosting. The only way to get a similiar result would be to use AJAX Long Pooling.
Now I purchased to a Virtual Server and my websocket app works great now.
Thanks for your support.

Categories