Jump to content

Noob Help Socket Creating


superblunt

Recommended Posts

Hi all i found a tutorial for socket creating on the web and it it seems to work fine but to get the socket to stay open seems to be a problem for me. This is the code i got to create the socket

 

File Name: port.php

 

$port="1234";

$host='127.0.0.1';

set_time_limit(0);

// create low level socket

if(!$socket=socket_create(AF_INET,SOCK_STREAM,0)){

    trigger_error('Error creating new socket',E_USER_ERROR);

}

// tie up socket to TCP port

socket_bind($socket,$host,$port);

 

// begin listening connections

socket_listen($socket);

// create communication socket

$comSocket=socket_accept($socket);

// read socket input

$socketInput=socket_read($comSocket,1024);

// convert to uppercase socket input

$socketOutput=strtoupper(trim($socketInput))."n";

// write data back to socket server

socket_write($comSocket,$socketOutput,strlen($socketOutput));

// close sockets

socket_close($comSocket);

socket_close($socket);

 

when i execute the code the first time it seems to hang the page not sure why

After i executed the port.php I run send.php witch connects to the port and sends some text info to it.Then the open.php changes it to caps and sends it back.

 

here is the send.php:

 

<?php

// check if form was submitted

if($_POST['send']){

    // open client connection to TCP server

    if(!$fp=fsockopen('127.0.0.1',1234,$errstr,$errno,30)){

        trigger_error('Error opening socket',E_USER_ERROR);

    }

    $message=$_POST['message'];

    // write message to socket server

    fputs($fp,$message);

    // get server response

    $ret=fgets($fp,1024);

    // close socket connection

    fclose($fp);

    echo '<h1>You entered the following message in

lowercase :'.$ret.'</h1>';

    exit();

}

?>

 

<html>

<head>

<title>TESTING TCP SOCKET SERVER</title>

</head>

<body>

<h1>Enter your message here</h1>

<form action="newtest.php" method="post">

 

 

<input type="send" name="message" size="30" /><br />

<input type="submit" name="send" value="Send Value" />

</form>

</body>

</html> 

 

In the tutorial they sed to use the php cgi and add the line : php g- port.php

But that did not work the g- has no meaning so i tried -f that i found somewhere.

It looks like it addes it but when i try to execute the send.php Form it just does not do anything

 

Thx for any help in advance and sorry for beaning such a noob

Link to comment
https://forums.phpfreaks.com/topic/146515-noob-help-socket-creating/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.