加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb2008socket简单数据收发

发布时间:2020-12-17 07:41:01 所属栏目:百科 来源:网络整理
导读: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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读