vb2008socket简单数据收发

前端之家收集整理的这篇文章主要介绍了vb2008socket简单数据收发前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Imports Microsoft.VisualBasic
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Public Class Form2
Public s = New Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)

Private Sub btnsend_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnconnect.Click
Try
Dim result As String = String.Empty '返回结果字符窜
Dim bytes(2048) As Byte '缓存字段
Dim localEndPoint As New IPEndPoint(IPAddress.Parse("119.6.132.49"),23) '设置IP终端
s.Connect(localEndPoint) '链接终端
delay(1000) '延迟1000秒,以便下面接收响应,时延的大小更具时间和返回值确定
s.Receive(bytes) '接收socket数据
result = Encoding.ASCII.GetString(bytes) '返回值使用ascii 编解码
txt_result.Text = result '显示返回值
save_result("dsp.txt",result) '保存返回结果
Catch ex As Exception
End Try

End Sub

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btncommand.Click
Dim bytes(2048) As Byte
Dim result As String = String.Empty
s.Send(Encoding.ASCII.GetBytes(txtBox_command.Text & Chr(13))) '发送文本框中的命令并加一个回车
delay(1000) '延迟1000秒,以便下面接收响应,时延的大小更具时间和返回值确定
s.Receive(bytes) '接收socket数据
result = Encoding.ASCII.GetString(bytes) '返回值使用ascii 编解码
txtBox_command.Text = String.Empty
txt_result.Text = result
save_result("dsp.txt",result) '保存返回结果

End Sub
Sub save_result(ByVal fname As String,ByVal infos As String)
System.IO.File.AppendAllText(fname,infos)
End Sub
Public Sub delay(ByVal msec As Long)
On Error Resume Next
Dim tstart As Single
tstart = Timer
While (Timer - tstart) < (msec / 1000)
Application.DoEvents()
End While
End Sub

Private Sub txtBox_command_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles txtBox_command.TextChanged

End Sub End Class

原文链接:https://www.f2er.com/vb/257107.html

猜你在找的VB相关文章