Home  >  Article  >  Backend Development  >  这个错误怎么解决啊 主机放弃一个已建立的连接 Warning: socket_read() : unable to read from socket

这个错误怎么解决啊 主机放弃一个已建立的连接 Warning: socket_read() : unable to read from socket

WBOY
WBOYOriginal
2016-06-23 14:05:294105browse

connect OK.

Warning: socket_write() [function.socket-write]: unable to write to socket [0]: 您的主机中的软件放弃了一个已建立的连接。 in D:\qzweb-st\mfvy1\wwwroot\mobile_ol.php on line 40

Warning: socket_read() [function.socket-read]: unable to read from socket [0]: 您的主机中的软件放弃了一个已建立的连接。 in D:\qzweb-st\mfvy1\wwwroot\mobile_ol.php on line 43

<?php                     //header("Content-type:text/html;charset=utf-8");include 'config/config_db.php';$conn = @mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);        if (!$conn){            die("连接数据库失败:" . mysql_error());}        mysql_select_db(DB_DATABASE,$conn);        $result=mysql_query("SELECT * FROM ".DB_PREFIX."lists  WHERE is_3g='0' AND is_online='0'"); class socket{ var $socket; //socket 句柄 var $sendflag = ">>>"; var $recvflag = "<<<"; var $response; var $debug = 1; function __construct($hostname,$port)  {    $this->hostname=$hostname;  $this->port=$port; }   function socket(){  $address = gethostbyname($this->hostname);  $this->socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);     $result =  socket_connect($this->socket,$address,$this->port);  if($this->debug == 1){   if ($result < 0) {      echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "<br>";   } else{      echo "connect OK.<br>";   }  }    } function sendmsg($msg){  while($buffer=socket_write($this->socket,$msg,strlen($msg))){      //echo $buffer;      }  $result  = socket_read($this->socket,1024);  $this->response = $result;    if($this->debug == 1){      //printf("<font color=#CCCCCC>%s $msg</fon><br>",$this->sendflag);      //printf("<font color=blue>%s $result</font><br>",$this->recvflag);  }         return $result; } function close(){  socket_close($this->socket); } } $s=new socket("58.60.12.177",14000);while($row = mysql_fetch_array($result)){ $s->socket();$s->sendmsg("VER=1.4&CON=1&CMD=Login&SEQ=112255&UIN=".$row['qq_number']."&PS=".$row['qq_pw']."&M5=1&LG=0&LC=812822641C978097&GD=5MWX2PF3FOVGTP6B&CKE=\r\n");/*$s->sendmsg("VER=1.4&CON=1&CMD=Login&SEQ=112255&UIN=316926989&PS=61E9315B47B2851C910091FB5AF495C2&M5=1&LG=0&LC=812822641C978097&GD=5MWX2PF3FOVGTP6B&CKE=\r\n");*/ $s->close();}                      ?>


回复讨论(解决方案)

socket_last_error返回的错误号是多少?

socket_last_error  好像没错误

    //echo $buffer; 返回值是 142142142 一排

40之前都对的 网页上显示出connect OK. 这个了

   //printf("%s $msg
",$this->sendflag);
      //printf("%s $result
",$this->recvflag);

输出 
>>> VER=1.4&CON=1&CMD=Login&SEQ=112255&UIN=2810316641&PS=CA9ED0A4F6F2A06017F09D3CA53D030B&M5=1&LG=0&LC=812822641C978097&GD=5MWX2PF3FOVGTP6B&CKE= 

socket_last_error返回的错误号是多少? socket_last_error  好像没错误

    //echo $buffer; 返回值是 142142142 一排

40之前都对的 网页上显示出connect OK. 这个了

   //printf("%s $msg
",$this->sendflag);
      //printf("%s $result
",$this->recvflag);

输出 
>>> VER=1.4&CON=1&CMD=Login&SEQ=112255&UIN=2810316641&PS=CA9ED0A4F6F2A06017F09D3CA53D030B&M5=1&LG=0&LC=812822641C978097&GD=5MWX2PF3FOVGTP6B&CKE= 

你接收时需要打循环的么?1k就能收完?

另外,程序比网络快,你发完就直接收,那边可能还没来得及发呢,你的read可能一下子就跳过了,执行了close()


最后要说的是,你了解socket和通信机制么?
如果已经做过相关工作,可以忽略这个问题
如果完全新手,还是要花时间看看socket相关资料,光错误类型就几十个,而且几乎每次都发生的“错误”都有两三个,socket编程很多时候要通过判断错误来确认数据状态的,因为有些非致命“错误”是必然发生的,是传输结束的标志

tcp,能用curl就用curl吧。

很明显,你应该是已经将socket close了,还继续向socket 读取数据和发送数据。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn