doujian1954 2013-07-26 14:58
浏览 114

PHP socket_read()和socket_recv()问题

Edit: I've solved my own problem and fixed the code below to reflect those changes in case anyone runs into the same difficulties. This code will work for a PHP client and java server connection.

I know the forums are peppered with these questions...however, I have tried nearly everything else for over eight hours and am at my wit's end. I am trying to send a string from a java Socket to a PHP Socket. I'm a PHP socket newbie so hopefully someone can diagnose my problem rather easily... Here is the PHP code:

<html>

<head>
</head>

<body>
<?php
//connection to NetworkService in app to manage incoming request
$addr = 'your server address';
$port = 1740;

$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($socket, $addr, $port);

//"
" is very important as it signifies to the server
// that this is the end of the transmission 
$request = "message to send 
";

//no flags needed circa php 5.2
$sent = socket_sendto($socket, $request, strlen($request), 0 , $addr, $port);

if($sent !== FALSE){
    $message = '';
    $next = ''; 

    //loop through to continue appending until everything has been read
    while($next = socket_read($socket, 4096)){
        $message .= $next;
    }
    echo $message;
} else{
    echo "Failed.";
}

socket_close($socket);
?>
</body>

</html>

Java Code:

public class TestServer{
    public static void main(String argv[0]) throws Exception{
        String in; String out = "Hello PHP
";
        ServerSocket inSock = new ServerSocket(1740);

        while(true){
            System.out.println("Now accepting connections");
            Socket outSock = inSock.accept();
            BufferedReader bReader = new BufferedReader(
                 new InputStreamReader(outSock.getInputStream()));
            PrintWriter sender = new PrintWriter(outSock.getOutputStream());
            in = bReader.readLine();
            System.out.println("Received: " + in);
            dWriter.print(out);
            System.out.println("Sent: " + out);
        }
    }
}

The problem is not with the first send (from PHP to JAVA) but with the second one (the response) which just hangs. The Java code runs completely so I know the problem has to do with the socket_read() or socket_recv() functions. I have tried to sleep() and tried a method using socket_setnoblock() (though perhaps incorrectly). As you can see by my "OPTION:" comments, I have tried a few different methods here as well but to no avail. What could I be missing? Thank you so much in advance!

  • 写回答

2条回答 默认 最新

  • douyao7390 2013-07-26 15:11
    关注

    According to the manual, socket_recv() will return an integer, which is the number of bytes received.

    The problem is the false !==, which you shouldn't use here (since socket_recv() returns an integer). This condition should be enough:

    while(0 != socket_recv($socket, $out, 1024)){
          if($out != null)
            $fullResult .= $out;
        };
    

    You may be interrested by socket_last_error().

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度