Visual Basic‎ > ‎

TCP通信

tcpclientでtelnetを実装した例。

Form1.vb

-----sauce-----
Public Class frmMain
Private client As System.Net.Sockets.TcpClient
Private Const BYTES_TO_READ As Integer = 4096
Private readBuffer(BYTES_TO_READ) As Byte

'presponse is a private variable used to store the response.

Private presponse As String

Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
cmdSend.Enabled = True
txtCommand.Enabled = True
txtResponse.Enabled = True
client = New System.Net.Sockets.TcpClient(txtIP.Text, txtPort.Text)
client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing)

End Sub

Private Sub doRead(ByVal ar As System.IAsyncResult)
Dim totalRead As Integer
Try
totalRead = client.GetStream.EndRead(ar) 'Ends the reading and returns the number of bytes read.
Catch ex As Exception

'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections
'to this client and remove it from the list of connected clients.

End Try

Dim receivedString As String = System.Text.Encoding.Default.GetString(readBuffer, 0, totalRead)

If totalRead > 0 Then

'the readBuffer array will contain everything read from the client.

'This is a new bit. We used to store receivedString directly in txtResponse.text
'but that generated an error. So instead we store it in the private
'variable.

presponse = presponse & receivedString

End If

Try
client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing) 'Begin the reading again.
Catch ex As Exception

'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections
'to this client and remove it from the list of connected clients.

End Try

End Sub

Private Sub messageReceived(ByVal message As String)
MessageBox.Show(message)
End Sub
Private Sub SendMessage(ByVal msg As String)
Dim sw As IO.StreamWriter
Try
sw = New IO.StreamWriter(client.GetStream)
sw.Write(msg + vbCrLf)
sw.Flush()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub

Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
SendMessage(txtCommand.Text)
txtCommand.Text = ""
client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing)
End Sub

'Right, so the response is now being stored in presponse. We now need to use the timer to check for any
'data in presponse, and display it if so. Yeah I know its a bodge, but it works!

Private Sub timResponse_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timResponse.Tick

If presponse <> "" Then
txtResponse.Text = txtResponse.Text & presponse
presponse = ""
End If

End Sub

End Class

-----form-----
Form:
(Name): frmMain
Text: MyTel

Text boxes:
1. (Name): txtIP
Text: 192.168.1.1

2. (Name): txtPort
Text: 23

3. (Name): txtCommand
Text:
Enabled: False
Font: Courier New, 8pt

4 (Name): txtResponse
Text:
Enabled: False
Font: Courier New, 8pt
Scrollbars: Both


Buttons:
1. (Name): cmdConnect

2. (Name): cmdSend
Enable: False

Timer:
1 (Name): timResponse
Enable: True