vb和php 基于socket通信
php代码(页面代码非cmd命令脚本)
<?php $server = '127.0.0.1'; $port = 8888; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if($socket < 0) { echo socket_strerror($socket); } else { echo '成功<br />'; } $result = @socket_connect($socket, $server, $port); $buf = $_GET['message']; $len = strlen($buf); socket_send($socket, $buf, $len, 0); socket_close($socket); ?>
vb代码
Option Explicit Private Sub Form_Load() tcpServer.LocalPort = 8888 tcpServer.Listen lblstate.Caption = "目前没有客户端连入!" End Sub Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long) If tcpServer.State <> sckClosed Then tcpServer.Close tcpServer.Accept requestID lblstate.Caption = "有客户端连入本机! IP:" & tcpServer.RemoteHostIP End Sub Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long) Dim strData As String tcpServer.GetData strData txtOutput.Text = strData & vbCrLf & txtOutput.Text 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
效果
http://bbs.csdn.net/topics/390614319