php7 获取数据流,stream_socket_accept()

stream_socket_accept()

(PHP 5, PHP 7)

接受由stream_socket_server()创建的套接字连接

说明stream_socket_accept(resource$server_socket[,float$timeout= ini_get("default_socket_timeout")[,string&$peername]]):resource

接受由stream_socket_server()创建的套接字连接。

参数$server_socket需要接受的服务器创建的套接字连接。$timeout覆盖默认的套接字接受的超时时限。输入的时间需以秒为单位。$peername如果包含该参数并且是可以从选中的传输数据中获取到,则将被设置给连接中的客户端主机的名称(地址)(怕出入很大,附带上原文:Will be set to the name (address) of the client which connected, if included and available from the selected transport.)Note:

也可以之后通过stream_socket_get_name()来确定。

返回值

返回接受套接之后的资源流或者在失败时返回FALSE。

注释Warning

该函数不能被用于 UDP 套接字。可以使用stream_socket_recvfrom()和stream_socket_sendto()来取而代之。

参见cURL 函数This code could be very helpfull...

The following code is for the "server". It listen for a message until CTRL-C

while (true)

{

// disconnected every 5 seconds...

receive_message('127.0.0.1','85',5);

}

function receive_message($ipServer,$portNumber,$nbSecondsIdle)

{

// creating the socket...

$socket = stream_socket_server('tcp://'.$ipServer.':'.$portNumber, $errno, $errstr);

if (!$socket)

{

echo "$errstr($errno)
\n";

}

else

{

// while there is connection, i'll receive it... if I didn't receive a message within $nbSecondsIdle seconds, the following function will stop.

while ($conn = @stream_socket_accept($socket,$nbSecondsIdle))

{

$message= fread($conn, 1024);

echo 'I have received that : '.$message;

fputs ($conn, "OK\n");

fclose ($conn);

}

fclose($socket);

}

}

?>

The following code is for the "client". It send a message, and read the respons...

send_message('127.0.0.1','85','Message to send...');

function send_message($ipServer,$portServer,$message)

{

$fp = stream_socket_client("tcp://$ipServer:$portServer", $errno, $errstr);

if (!$fp)

{

echo "ERREUR :$errno-$errstr
\n";

}

else

{

fwrite($fp,"$message\n");

$response = fread($fp, 4);

if ($response != "OK\n")

{echo 'The command couldn\'t be executed...\ncause :'.$response;}

else

{echo 'Execution successfull...';}

fclose($fp);

}

}

?>To check if there's a new connection waiting, without blocking, or (when using non-blocking mode) without notices), you can use stream_accept (as opposed to socket_select).

class GenericClass {

protected $resSocket=null;

function acceptConnections() {

# check that we still have a resource

if(is_resource($this->resSocket)) {

$arrRead=array($this->resSocket);

$arrWrite=array();

/** @warning Passing $arrRead,$arrWrite by reference */

if(stream_select($arrRead,$arrWrite,$arrWrite,0)) {

$resConnection=stream_socket_accept($this->resSocket,0);

# ... other stuff here

}

}

}

}

?>this function, compared to the function socket_accept, got an extra argument "timeout".

To make this function wait indefinitelly to incoming connections, just as in socket_accept, set timeout to -1. It works for me with PHP 5.0.4.Note that if you use 0 as timeout, the connection will timeout right away.To whom it may concern, and it may concern you greatly, stream_set_blocking has no effect on stream_socket_accept.

If you want it to return right away, connection or not, use 0 for the timeout parameter.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 C# 和 Java 实现 Socket 传输 Protobuf 字节流的过程可以分为以下几个步骤: 1. 定义 Protobuf 消息结构 首先,需要使用 Protocol Buffers 定义消息结构。假设我们要传输的消息结构如下: ``` message Person { string name = 1; int32 age = 2; repeated string phone_number = 3; } ``` 2. 生成代码 使用 protobuf 编译器生成 C# 和 Java 的代码,方法如下: ``` protoc --csharp_out=. person.proto protoc --java_out=. person.proto ``` 3. C# 实现 Socket 发送 首先,在 C# 中创建一个 `Person` 对象,并将其序列化为字节数组,然后将其发送到 Java 服务器: ```csharp using System; using System.Net.Sockets; using Google.Protobuf; class Program { static void Main(string[] args) { // 创建 Person 对象 var person = new Person { Name = "张三", Age = 18, PhoneNumber = { "123456789", "987654321" } }; // 将 Person 对象序列化为字节数组 byte[] data = person.ToByteArray(); // 创建 Socket 连接 var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect("127.0.0.1", 8888); // 发送数据 socket.Send(data); // 关闭连接 socket.Shutdown(SocketShutdown.Both); socket.Close(); } } ``` 4. Java 实现 Socket 接收 在 Java 中,我们需要创建一个 `ServerSocket`,并监听指定的端口。当有连接请求时,我们可以使用 `Socket` 接收数据,并将其反序列化为 `Person` 对象: ```java import com.example.PersonOuterClass.Person; import com.google.protobuf.InvalidProtocolBufferException; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Main { public static void main(String[] args) throws IOException { // 创建 ServerSocket,监听指定端口 ServerSocket serverSocket = new ServerSocket(8888); while (true) { // 等待连接 Socket socket = serverSocket.accept(); // 读取数据 byte[] buffer = new byte[socket.getInputStream().available()]; socket.getInputStream().read(buffer); try { // 将字节数组反序列化为 Person 对象 Person person = Person.parseFrom(buffer); System.out.println(person); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } // 关闭连接 socket.shutdownInput(); socket.close(); } } } ``` 这样,就完成了 C# 和 Java 之间通过 Socket 传输 Protobuf 字节流的实例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值