Jump to content

socket programing


Recommended Posts

Start "aa.php"... leave it there. It is supposed not to give you any response, and just hang in there running.While it is running, from a browser, start "bb.php".

Link to post
Share on other sites

That's exactly what's supposed to happen.Like I said, there is no dialogue between clients, because there is no way with PHP (on Windows) you can have two clients connected to the same server at the same time.If you take another look at the client, there is a sort of dialogue between the one client and the one server though:

<?phpnamespace PEAR2\Net\Transmitter;require_once 'PEAR2/Net/Transmitter/Autoload.php';$c = new TcpClient('127.0.0.1', 6666);$c->send('######');echo $c->receive(2), "\n";$c->send('OPPS');echo $c->receive(2), "\n";$c->send('######');echo $c->receive(2), "\n";$c->send('DONE');

Every "send" line is what the client says, and every "echo" line reflects what the server has answered. THAT is the dialogue. Change the argument of some send() calls to a different 4 letter word (like "HERE" for example), and observe the new answers.Or look at the server, and adjust the reactions in the switch as you want them.

Link to post
Share on other sites

I can see why you're not in any way impressed though... there's no state. It doesn't feel like the server has any purpose in the whole scheme...OK, here's a slightly more complicated server: Once the server receives a "HERE" from a client, it will continue to answer "ok" to "######", until it receives "HERE" again.

<?phpnamespace PEAR2\Net\Transmitter;require_once 'PEAR2/Net/Transmitter/Autoload.php';$server = stream_socket_server('tcp://127.0.0.1:6666');$hasIt = false;while (true) {    try {        $conn = new TcpServerConnection($server, -1);        while ('DONE' !== $message = $conn->receive(4)) {            switch ($message) {                case '######':                    $conn->send($hasIt ? 'ok' : 'no');                    break;                case 'OPPS':                    $conn->send('oh');                    break;                case 'HERE':                    $hasIt = !$hasIt;                    $conn->send('ty');                    break;                case 'KILL':                    break 3;                default:                    $conn->send('??');            }        }    } catch (Exception $e) {        echo $e;    }}

And with just a slight altering of the client...

<?phpnamespace PEAR2\Net\Transmitter;require_once 'PEAR2/Net/Transmitter/Autoload.php';$c = new TcpClient('127.0.0.1', 6666);$c->send('######');echo $c->receive(2), "\n";$c->send('OPPS');echo $c->receive(2), "\n";$c->send('HERE');echo $c->receive(2), "\n";$c->send('DONE');

...now the client will first output "no oh ty", then, at the next request, "ok oh ty", then back to "no oh ty" and so forth. NOW you can clearly see that the server is "alive" in that it remembers what it was once told in a previous connection, and it acts accordingly.

Link to post
Share on other sites

excuse me boen thanks for your help but I need a program that using it I can run a live dialogue between server and clientsome thing like chat room but just with two person |------------------| | server: | |-------- | | ----------------- | |----------------- | |------------------| |------------------| | client: | |-------- | | ----------------- | |----------------- | |------------------|

Edited by Mahdi
Link to post
Share on other sites

That is what the above is doing. The server program can be on a different machine than the client.Both of them can be made to send and receive stuff entered by a user rather than "hard coded" stuff. They could also be made to receive stuff of variable length rather than a constant one. I'm keeping it a constant hard coded stuff for simplicity's sake.But if you want to observe a more real app... here's one that users interact with from the command line, although it still demands constant length messages:The server:

<?phpnamespace PEAR2\Net\Transmitter;require_once 'PEAR2/Net/Transmitter/Autoload.php';$server = stream_socket_server('tcp://127.0.0.1:6666');echo "Server started...\n";while (true) {    try {        $conn = new TcpServerConnection($server, -1);        echo "[[New peer]]\n";        while ('DONE' !== $message = $conn->receive(4)) {            echo $message, "\n";            $conn->send(trim(fgets(STDIN)));        }        echo "[[Peer is gone]]\n";    } catch (Exception $e) {        echo $e;    }}

The client:

<?phpnamespace PEAR2\Net\Transmitter;require_once 'PEAR2/Net/Transmitter/Autoload.php';$c = new TcpClient('127.0.0.1', 6666);echo "[[Connected...]]\n";while (true) {    $message = trim(fgets(STDIN));    $c->send($message);    if ('DONE' === $message) {        break;    }    echo $c->receive(2), "\n";}

Run BOTH the server and the client from the command line. First the server, then the client.Then, type 4 letters from the client and hit enter. You'll see them appear in the server. From the server, type 2 letters and hit enter, and you'll see them appear on the client. You can go back and forth as you please, just as long as you keep 4 letters from clients, and 2 letters back. You can type "DONE" from the client to disconnect the client from the server.To implement variable length messages, you need to invent your own "protocol" (as in "algorithm") that would somehow specify how the length of each message is determined.In the HTTP protocol for example, the client/server first receives everything, byte by byte, up to "\n\n" (or some hardcoded limit), and expects to find "Content-Length: XX" within what it has received. From there, it reads the number of bytes Content-Length says.In some simpler protocols, the first byte is the length that the client/server should then receive.In some more complex protocols, the client and server have different means of calculating the length from the other peer (e.g. constant length messages from client to server, "first byte" length messages from server to client).

Link to post
Share on other sites

There should be only one server and one client running, and the server must be started first.If you try to run two servers, the second one should fail to receive any messages.If you try to run two clients, the second one will wait a few seconds, and fail unless the server is finished with the previous client in that time.What is the rest of the message anyway?Anyway... the reason you see those is that some socket errors provide error messages and numbers from the OS itself. When there are no such things in the particular exception, you just see empty strings.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...