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