PHP套接字服务器不能与客户端一起使用,socket_bind()

时间:2017-03-22 03:42:15

标签: java php websocket

我有共享主机“ad-box.deslab.vn”。以下代码位于Web服务器中:

<?php

error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();

$address = '181.224.157.142';
$port = 80;

// Create Socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($sock === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) { 
    echo socket_strerror(socket_last_error($sock)); 
 exit; 
}

// Bind socket to port
if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

// Start listening for connection
if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

// Handling connection from client
do {
    $msgsock = socket_accept($sock); // msgsock is a client connect to webserver
    if ($msgsock === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }

    $request ="PHP said : you are sent " . socket_read($msgsock, 2048, PHP_BINARY_READ);
    socket_write($msgsock, $request, strlen($request));
    socket_close($msgsock);  // Close connect of client
} while (true);
socket_close($sock);  // Close socket of server

&GT;

在客户端,程序是用Java编写的。以下代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class WebClient {
public static void main(String[] args) throws IOException {

    String host ="ad-box.deslab.vn";

    Socket s = null;
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
        s = new Socket(host, 80);
        br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: hostname\n");
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: hostname\n");
    }
    // Send message to server
    String message = "FETEL HCMUS";
    // String message = "GET / HTTP/1.1\r\n\r\n";
    // System.out.println(message);
    bw.write(message);
    bw.flush();
    // Get response from server
    String response = "";
    while ((response = br.readLine()) != null) {
        System.out.println(response);
    }
    bw.close();
    br.close();
    s.close();
}

}

当我通过网络浏览器运行index.php文件时。浏览器显示此错误:

警告:socket_bind():无法绑定地址[13]:第26行/home/vietchip/public_html/deslab.vn/ad-box/index.php中的权限被拒绝 socket_bind()失败:原因:权限被拒绝socket_bind()失败:原因:权限被拒绝

任何人都可以帮我解决吗? 非常感谢。

0 个答案:

没有答案