php socket accept,PHP:socket_accept()在收到第一个字符串消息后停止工作(PHP: socket_accept() stops working after it re...

PHP:socket_accept()在收到第一个字符串消息后停止工作(PHP: socket_accept() stops working after it received the first string message)

我使用socket_accept(socket)创建了一个包含10个可能客户端的套接字服务器。 现在最多可以连接10次。

在第一个客户端向服务器提交消息后,问题就出现了

$data = @socket_read($clients[$i]['socket'], 1024, PHP_NORMAL_READ);

然后脚本尝试通过socket_accept和socket_read再次侦听客户端和消息,但所有socket_accept-requests都失败并显示错误字符串“Invalid argument”。

但是,var_dump表示该参数是类型(Socket)的资源(14)。

已连接的客户端可以继续使用“服务器”脚本,因为没有任何事情发生并保持连接。 只有新客户端无法连接,端口似乎关闭(没有telnet和netcat请求可能 - 连接被拒绝)

任何想法都会有所帮助。 谢谢!

I created a socket server with 10 possible clients by using socket_accept(socket). It is possible to connect up to 10 times now.

The problem appears after the first client submitted a message to the server which is caught by

$data = @socket_read($clients[$i]['socket'], 1024, PHP_NORMAL_READ);

Then the script tries to listen for clients and messages again by socket_accept and socket_read, but all socket_accept-requests fail with the error string "Invalid argument".

var_dump indicates that the parameter is a resource(14) of type (Socket), though.

Already connected clients can continue using the "server" script as nothing happened and remain connected. Only new clients are unable to connect and the port seems to close (no telnet and netcat requests are possible - connection refused)

Any ideas would be helpful. Thanks!

原文:https://stackoverflow.com/questions/19139878

更新时间:2020-01-02 23:09

最满意答案

发现我假设在调用套接字处理程序类的析构函数时断开所有套接字 - 一旦子进程关闭就终止所有连接。

从析构函数中删除socket_shutdown()和socket_close()解决了这个问题。

Discovered that I assumed to disconnect all sockets when the destructor of the socket handler class was called - which terminated all connections once the child process closed.

Removing the socket_shutdown() and socket_close() from the destructor solved the problem.

2013-10-08

相关问答

这是你应该明确改变的方法以及如何改变它。 private void ProcessClientThread(object clientObj)

{

Console.WriteLine("Client connected");

ClientObject client = (ClientObject)clientObj;

Socket clientSocket = client.ClientSocket;

byte[] b

...

这根本不是static字段。 你的错误是在这个循环中: while (!received.equals("QUIT")) {

if (head.equals("rep:"))

changeText(tail);

else if (head.equals("app:"))

appendText(tail);

output.println(text)

...

看起来像死锁情况正在发生,两个源代码都挂在socket.receive 幸运的是,Android和J2SE UDP服务器客户端代码的代码是相同的,因此请在您的机器中尝试此代码并进行调试以查看发生了什么。 您可以更好地了解打印报表。 此外,UDP是不可靠的。 我没有看到代码中实现的数据包接收确认和重发机制。 这是必需的。 您不能只假设您发送的数据包将被接收到另一端。 Seems like a Deadlock scenario is taking place with both Source cod

...

您当前的源代码,特别是编写的AsyncTask不是很好的指定。 为什么不从doInBackground()方法返回响应? 您应该遵循官方编写的指南“如何使用AsyncTask” 。 所以我的意见是改变你的doInBackground()方法签名 - 它应该将你的响应作为String返回,然后在onPostExecute()方法中使用这个方法的参数来更新UI。 例: public class MyClientTask extends AsyncTask {

...

我是新来的Stackoverflow,所以我没有意识到可以回答自己的问题。 无论如何,我在原帖的末尾回答了上周的编辑问题。 现在我将它包含在这里,以便任何遇到此类问题的人都可以参考这一点,并意识到这是我能够解决问题的方式。 我尝试了由同伴贡献者在这里给出的第一个解决方案,但不幸的是这对我没有任何作用。 第二种解决方案一旦我已经能够以另一种方式解决,我就没有尝试过。 也许我会在未来的某个时候开始。 但是如果有人尝试过,请在这里留下您的意见,以便从智慧中获益。 这是我的解决方案: 经过一个星期,我的头

...

发现我假设在调用套接字处理程序类的析构函数时断开所有套接字 - 一旦子进程关闭就终止所有连接。 从析构函数中删除socket_shutdown()和socket_close()解决了这个问题。 Discovered that I assumed to disconnect all sockets when the destructor of the socket handler class was called - which terminated all connections once the

...

Java代码在base 64中返回。 解码它以获得原始消息。 Java code is returning in base 64 . Decode it to get original message.

我认为问题不在于您的应用程序是如何实现的。 您是否考虑过Azure负载平衡超时? 这是一篇很好的博客文章,详细介绍了它: Windows Azure负载均衡器超时详细信息 Azure将在大约一分钟后停止您的应用程序,而与您的实现无关。 看一下,看看你面临的问题是否存在。 问候 I think the problem is not on how your application is implemented. Have you consider the Azure load balance time

...

第一步:将缓冲区大小更改为1,而不是0。 第二:如果第一条日志消息正常,您的配置文件就可以了。 它可能在你的其余代码中,向我们展示一点。 如果它很复杂,用一个或两个非常简单的函数测试然后让我们知道会发生什么。 编辑添加 好的,我看到了你的代码,现在我看到HexPatternConverter被用来格式化一列:所以有些东西可能出错,例如抛出异常。 我建议您尝试在不使用该类的情况下运行它(只需将Thread列写为普通字符串),看看是否有任何改变。 另外,万一你不知道:如果log4net遇到SQL问题,

...

根据文档 ,方法的名称是observeValue(forKeyPath:of:change:context:) 。 注意of:不是ofObject:就像你一样。 您的方法不是所需方法的实现。 我相信你需要用override来标记正确命名的方法(并且编译器允许你离开的事实没有那个是你的名字错误的标志)。 According to the docs, the name of the method is observeValue(forKeyPath:of:change:context:). Note

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值