vb与php进行socket通信遇到的问题

前端之家收集整理的这篇文章主要介绍了vb与php进行socket通信遇到的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

vb代码PHP代码进行socket通信时,发现PHP的socket_read函数总是取不到数据。查找了一些资料才发现,是VB socket 发送数据时必须要加上 chr(13)+chr(10),再就是不能缺少:DoEvents 才能在PHP端接收到数据。

VB代码

Private Sub Form_Load()
Tcpserver.LocalPort = 8000
'Tcpserver.Bind
Tcpserver.Listen
Label1.Caption = "目前没有客户端连入!"
End Sub


Private Sub Form_unload(Cancel As Integer)
Tcpserver.Close
End Sub


Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
If Tcpserver.State <> sckClosed Then Tcpserver.Close
Tcpserver.Accept requestID
Label1.Caption = "有客户端连入本机! IP:" & Tcpserver.RemoteHostIP
End Sub

Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Tcpserver.GetData strData

Text1.Text = Text1.Text & strData & vbCrLf
Tcpserver.SendData "1服务器说:" + Chr(13) + Chr(10)
Tcpserver.SendData "2服务器说:" + Chr(13) + Chr(10)
'Tcpserver.SendData Chr(13) & Chr(10)
DoEvents
Tcpserver.Close
Tcpserver.Listen
End Sub


Private Sub txtSendData_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Tcpserver.State = sckConnected Then
Tcpserver.SendData "服务器说:" & txtSendData.Text
txtOutput.Text = "服务器说" & txtSendData.Text & vbCrLf & txtOutput.Text
txtSendData.Text = ""
Else
MsgBox "目前没有连接的客户端!"
End If
End If
End Sub


PHP代码

<?PHP

error_reporting(E_ALL);
set_time_limit(0);
$port=8000;
$ip="192.168.1.110";
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($socket<0)
{echo "socket_create() Failed: reason: ".socket_strerror(socket_last_error($socket));}
else
{echo "OK.<br>";}
echo "try to connect '$ip' port is '$port' ...<br>";
$result=socket_connect($socket,$ip,$port);
if($result < 0)
{echo "socket_connect()Failed: reason:".socket_strerror(socket_last_error($socket))."\n";}
else
{echo "connect ok!<br>";}
$in="Ho\r\n";
$in.="first blood \r\n";
$out='';
if(!socket_write($socket,$in,strlen($in)))
{echo "socket_write() Failed: reason:".socket_strerror(socket_last_error($socket))."\r\n";}
else
{echo "send message to server success!<br>";
echo "the content of send message is:<font color='red'>$in</font><br>";
}


while ($buffer = @socket_read($socket,1024,PHP_NORMAL_READ))
{
if (preg_match("/not connect/",$buffer)) {
echo "don`t connect\n";
break;
} else {
//服务端传来的信息
echo "Buffer Data: " . $buffer . "<br>";
echo "Writing to Socket<br>";
}
}
echo "close socket....<br>";
socket_close($socket);
echo "close ok \n";

?>

原文链接:/vb/256449.html

猜你在找的VB相关文章