954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with establishing a TCP socket connection

A service running in the Server is listening to port 2978 on 1xx.xxx.xx.xx for incoming connections! My PHP code receives URL data through get method. A tcp connection has to be established with the 'service' on that port and the data received from the URL should be sent to the service. The service responds with another data to the same port. this data should be obtained by the code and be saved in a file.

I've written the code. But its showing error! help me pls!!

#!/usr/local/php5/bin/php-cgi
<?php 
$CONTENT = $_GET["DATA"]."
";
//fsockopen("tcp://1xx.xxx.xx.xx",2978 , $errno, $errstr, 30); 
echo urldecode($CONTENT);
$Handle = fopen("/xxx/xxx/OldData.txt", "a");
fwrite($Handle, $CONTENT);
fclose($Handle);
// set IP and Port number
$host = "1xx.xxx.xx.xx";
$port = 2978;
set_time_limit(0);                                                                           // No timeout
$socket = socket_create(AF_INET, SOCK_STREAM, tcp) or die("Could not create socket\n");  // creating a socket
$result = socket_connect($socket, $host, $post) or die ("Could not connect to socket\n");    // connecting socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");           // binding socket to port
$result = socket_listen($socket, 1) or die("Could not set up socket listener\n");            // starting to listening for connections
$anothersocket = socket_accept($socket) or die("Could not accept incoming connection\n");    // accept incoming connections and create another socket to handle communication
socket_write($anothersocket, $CONTENT, strlen($CONTENT)) or die ("Could not write output\n");// writing content to port (Input to CS)
$output = socket_read($anothersocket, 1024) or die ("Could not read output from CS\n");      // Getting the output from CS
$Handle1 =fopen("/xxx/xxx/NewData.txt", "a");
fwrite($Handle1, $output);
fclose($Handle1);
socket_close($anothersocket);                                                               // close sockets
socket_close($socket);
?>


Error:
$GPGSV,4,1,16,04,15,293,,11,31,139,,12,04,354,,13,10,188,*7D
Fatal error: Call to undefined function socket_create() in /opt/x_apache/s1051713/cgi_bin/project/Server.php on line 14

knan
Light Poster
29 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Are you sure you have the sockets module installed in your PHP installation?

Check by looking at the output of phpinfo()

edwinhermann
Junior Poster
141 posts since Sep 2009
Reputation Points: 67
Solved Threads: 29
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: