mardi 28 novembre 2017

php client server on web host cant bind

I want to Run a simple Client-Server socket PHP script on my web host. But i don't know how to exactly specify IP address and port number to work.

I test then on my wammp with success, but wen i upload them on my host control panel and run, they are not works.

Here is my codes:

Client

<?php
$host    = "105.24.123.188";
$port    = 8080;
$message = "hello";
echo "Message To server: ".$message. "<br>";
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
// send string to server
echo "After connect: ".$result. "<br>";
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server: ".$result. "<br>";
// close socket
socket_close($socket);
?>

Server

<?php

$host = "105.24.123.188";
$port = 8080;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");

if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
    echo socket_strerror(socket_last_error($socket));
    exit;
} 
//echo "reached \n";
printf("this1\n");

// bind socket to port
$result = socket_bind($socket, 0, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
///echo "reached \n";
printf("this2\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message: ".$input. "<br>";
// reverse client input and send back
$output = strtoupper($input) . "\n";

socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>

[Error]

Client Message To server: hello After connect: 1 Reply From Server: HTTP/1.1 400 Bad Request

Server

can't bind

I cant find out how to run my simple example on web server. IP address and port checked they are valid and open.




Aucun commentaire:

Enregistrer un commentaire