View Full Version : PHP Script
MrPinkPanther
04-01-2010, 01:16 PM
I've followed a tutorial to create a socket server in PHP however I am getting the following error:
Parse error: syntax error, unexpected T_NS_SEPARATOR, expecting T_STRING in /Applications/XAMPP/xamppfiles/htdocs/script.php on line 9
Heres my code:
<?
$host = "127.0.0.1";
$port = 6932;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
$input = trim($input);
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
socket_close($socket);
?>
Any help would be greatly appreciated!
Mickword
04-01-2010, 02:00 PM
Are you sure the ip is correct or online?
MrPinkPanther
04-01-2010, 06:58 PM
Are you sure the ip is correct or online?
Yep, it's the IP of your own computer and if I visit it I get the Xampp logo like I should.
Source
05-01-2010, 05:00 PM
Worked great for me? Made a small C# app (based on online examples) to send data to it as well.
PHP:
<?php
// Binding Info
$host = "127.0.0.1";
$port = 7684;
// Unlimited Script Running
set_time_limit(0);
// Open Sockets
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
$result = socket_bind($socket, $host, $port);
$result = socket_listen($socket, 3);
$spawn = socket_accept($socket);
// Infinate Loop
while( 1 )
{
$input = @socket_read($spawn, 1024) or exit;
$input = trim($input);
$output = "Recieved \n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
echo $input . '<br>';
ob_flush();
}
?>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpClient svConn = new TcpClient();
Console.WriteLine("Establishing connection to PHP socket server...");
svConn.Connect("127.0.0.1", 7684);
Console.WriteLine("Connected");
Console.WriteLine("Enter string/command to be sent:");
while (true)
{
String message = Console.ReadLine();
Stream svStream = svConn.GetStream();
ASCIIEncoding asciiMessage = new ASCIIEncoding();
byte[] asciiMessageBytes = asciiMessage.GetBytes(message);
svStream.Write(asciiMessageBytes, 0, asciiMessageBytes.Length);
byte[] response = new byte[100];
int responseBytes = svStream.Read(response, 0, 100);
for (int i = 0; i < responseBytes; i++)
{
Console.Write(Convert.ToChar(response[i]));
}
}
}
}
}
and screencast showing it working :)
http://screencast.com/t/OWE5MTk5Y2
Are you using php 5.3 by any chance?
Source
05-01-2010, 05:47 PM
In my example, although I am pretty sure it was directed at FlyDuo, I was using 5.3.0 with the sockets extension enabled (obviously).
MrPinkPanther
05-01-2010, 06:46 PM
I'm using the latest XAMPP on Snow Leopard with just the defaults. On yours it doesn't load until you start the client, on mine it loads instantly and says the below so I don't have time to even start the client.
Parse error: syntax error, unexpected T_NS_SEPARATOR, expecting T_STRING in /Applications/XAMPP/xamppfiles/htdocs/test.php on line 10
Is there maybe something extra I need to enable?
Its PHP version 5.2.12
Source
05-01-2010, 08:20 PM
I'm sure it would output a different error, but is the sockets extension enabled?
MrPinkPanther
05-01-2010, 10:44 PM
I'm sure it would output a different error, but is the sockets extension enabled?
It is unfortunately. It's uncommented in php.ini :(
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.