Advertisement
obernardovieira

Socket Cliente [BETA]

Feb 26th, 2013
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.85 KB | None | 0 0
  1. <?php
  2. $service_port=54678;//porta do socket server
  3. $address="192.168.10.3";//como eu estou testando no pc, vou usar o ip alternativo (pode ir na buscar, executar>cmd e digita ipconfig e usar endereço IPv4)
  4. $socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);//cria um socket
  5. $result=socket_connect($socket,$address,$service_port);//conecta ao socket server
  6. if($result===false) {
  7.     echo "socket_connect() falhou.\nMotivo: ($result) ".socket_strerror(socket_last_error($socket))."\n";//erro ao conectar o socket server
  8. } else {
  9.     echo "OK.\n";//conectou
  10. }
  11. $in="Olá server!";
  12. socket_write($socket,$in,strlen($in));//envia mensagem ao socket server
  13. echo "OK.\n";
  14. echo "Lendo resposta:\n\n";
  15. $out=socket_read($socket,2048);//le mensagem do socket server
  16. echo $out;
  17. socket_close($socket);//fecha o socket iniciado (para nao exceder o limite de conexoes)
  18. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement