关于socket监听端口接收数据的问题

miao326200937

现在有一个需求,有一堆手持设备会往指定的IP和端口(服务器IP)发送数据,我要做的工作就是接收这个端口发送的数据,想知道如何才能做到对面的手持设备不主动断开我就能一直开启监听接收数据并且返回给他,而且如果此时有别的设备在连接也可以连接,我写的代码可以持续接收一个设备连接上来的数据,但是其他的就不能连接了

    $host = "192.168.0.30";
    $port = 3333;
    set_time_limit(0);
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("创建socket失败!\n");
    $result = socket_bind($socket, $host, $port) or die("端口绑定失败!\n");
    $result = socket_listen($socket, 3) or die("端口监听失败!\n");

    $spawn = socket_accept($socket) or die("接收数据失败!\n");

    do {
//读取客户端发送的数据
    $input = socket_read($spawn, 1024) or die("读取数据失败!\n");

    if(isset($input2)) {
        if($input2 == '# HEA_PACK BK123456') {
            mysql_query("INSERT INTO taskFault (functionEncoding) VALUES ('$input2')");
            continue;
        }
    }

    $input = trim($input);

                                    //额外添加的关闭命令
                                    if($input == 'exit') {
                                        socket_close($socket);
                                        break;
                                    }
    //收到数据处理流程
    if(isset($input)) {

        if($input == '# HEA_PACK BK123456') {
            mysql_query("INSERT INTO taskFault (functionEncoding) VALUES ('$input')");
            continue;
        }

        $array = explode(',', $input);
        //将收到的数据写入数据库表格中
        $functionEncoding = $array;
        $deviceId = $array;
        $persionId = $array;
        $stationId = $array;
        $operationType = $array;
        $taskNum = $array;
        $taskSer = $array;
        $taskName = $array;
        $i = 1;
        mysql_query("INSERT INTO taskFault (functionEncoding,deviceId,persionId,stationId,operationType,taskNum,taskSer,taskName) VALUES (
        '$functionEncoding','$deviceId','$persionId','$stationId','$operationType','$taskNum','$taskSer','$taskName'
        )");
        if(mysql_affected_rows() != 1) continue;
        //
        if($array == 'TASK_RUN') {
            if($array == 'EQU_APPLY') {
                $sql = "SELECT * FROM maintenancetask limit 0,5";
                $query = mysql_query($sql);
                $count = mysql_affected_rows();
//              $str = null;
                while(!!$task = mysql_fetch_array($query)) {
                    $taskSer = "N".$i;
$str = "#,TASK_RUN,".$array.",".$array.",".$array.",PC_LIST,".$count.",".$taskSer.",".$task.",\\r\\n";
                    socket_write($spawn, $str, strlen ($str)) or die("Could not write output\n");
                    $functionEncoding = "TASK_RUN";
                    $deviceId = $array;
                    $persionId = $array;
                    $stationId = $array;
                    $operationType = "PC_LIST";
                    $taskNum = $count;
                    $taskName = $task;
                    mysql_query("INSERT INTO taskFault (functionEncoding,deviceId,persionId,stationId,operationType,taskNum,taskSer,taskName) VALUES (
                                '$functionEncoding','$deviceId','$persionId','$stationId','$operationType','$taskNum','$taskSer','$taskName'
                                )");
                    $i ++;
                }

            }
        }
    }
    } while(true);

socket方面是个小白,希望大神帮忙看看

6950 2 0
2个回答

walkor 打赏

用workerman吧

  • miao326200937 2015-12-01

    workerman能实现吗,我下载几个例子试了一下,没找到大概相似的,我下了聊天的和多人在线游戏那个,有跟我需求差不太多例子吗,我试试

walkor 打赏
  • 暂无评论
年代过于久远,无法发表回答
×
🔝