-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.php
executable file
·51 lines (39 loc) · 1.29 KB
/
client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
//error_reporting(~E_WARNING);
// $host = '51.254.140.189';
$host = '127.0.0.1';
$port = '5000';
echo 'Welcome to ShellChat !' . "\r\n";
echo 'Enter your message or type :q to quit' . "\r\n";
//Creation de la socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die('Création de socket refusée');
//Connexion au serveur
socket_connect($sock,$host,$port) or die('Connexion impossible');
//Ecriture du paquet vers le serveur
socket_write($sock,$paquet,$write_len);
//Fermeture de la connexion
// socket_close($sock);
while(true) {
// read incoming msg
$input = socket_read($sock, 1024);
if(null != $input && $input != '') {
echo $input . "\r\n";
}
$userMsg = fgets(STDIN);
//Send the message to the server
if( ! socket_sendto($sock, $userMsg , strlen($userMsg) , 0 , $host , $port))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
//Now receive reply from server and print it
if(socket_recv ( $sock , $reply , 2045 , MSG_WAITALL ) === FALSE)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
echo $reply;
}
?>