1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| Imports System
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Imports System.IO
Imports System.Text
Public Class TestAsync_2
Public myConnect As Socket
Dim bteAccept(65536) As Byte
Dim bteSend(65536) As Byte
Dim MessageInfo As String = "rien"
Dim MessageSender As String = "message to send"
Dim MessageReceive As String = ""
'Dim MyHostName As String = "localhost"
Dim iPort As Integer = 8594
Private Sub TestAsync_2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnConnectSendReceive_Click(sender As Object, e As EventArgs) Handles btnConnectSendReceive.Click
' connexion
Connect()
logs(MessageInfo)
SendCommand("0 protocol peptalk" & vbLf) ' protocol peptalk noevents
'Receive()
'logs(MessageReceive)
SendCommand("1 get /storage/shows/NEWSA_MasterNewsNew/playlists/carousel/elements" & vbLf)
Receive()
logs("Retour message = " & MessageReceive)
CloseConnexion()
End Sub
Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
CloseConnexion()
End Sub
Private Sub logs(ByVal message As String)
RichTextBox1.AppendText(vbCrLf & message)
End Sub
Public Sub Connect()
Dim myThread As New Thread(New ThreadStart(AddressOf ConnectStart))
myThread.Start()
'ConnectStart()
End Sub
Private Sub ConnectStart()
Dim bidEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Parse("127.0.0.1"), iPort)
myConnect = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Try
Dim myAsyncCallBack As New AsyncCallback(AddressOf ConnectEnd)
myConnect.BeginConnect(bidEndPoint, myAsyncCallBack, myConnect)
MessageInfo = "Test SocketDial = connecté"
Catch ex As SocketException
MessageInfo = "Error:TestSocketDial: Connection error = " & ex.Message
End Try
End Sub
Private Sub ConnectEnd(ByVal pIAsyncResult As IAsyncResult)
myConnect.EndConnect(pIAsyncResult)
End Sub
Public Sub CloseConnexion()
myConnect.Close()
End Sub
Public Sub SendCommand(ByVal cmd As String)
MessageSender = ""
MessageSender = cmd
Dim myThread As New Thread(New ThreadStart(AddressOf SendStart))
myThread.Start()
End Sub
Private Sub SendStart()
Dim myAsyncCallBack As New AsyncCallback(AddressOf SendData)
bteSend = Encoding.ASCII.GetBytes(MessageSender)
myConnect.BeginSend(bteSend, 0, bteSend.Length, SocketFlags.DontRoute, myAsyncCallBack, myConnect)
End Sub
Private Sub SendData(ByVal pIAsyncResult As IAsyncResult)
Dim intSend As Integer
intSend = myConnect.EndSend(pIAsyncResult)
'txtMessage.Text += vbCrLf + "Message sent" + intSend.ToString + " bytes "
End Sub
Public Sub Receive()
Dim myThread As New Thread(New ThreadStart(AddressOf ReceiveStart))
myThread.Start()
End Sub
Private Sub ReceiveStart()
Dim myAsyncCallBack As New AsyncCallback(AddressOf ReceiveData)
myConnect.BeginReceive(bteAccept, 0, 65536, 0, myAsyncCallBack, myConnect)
'txtMessage.Text += vbCrLf + "Receive"
End Sub
Private Sub ReceiveData(ByVal pIAsyncResult As IAsyncResult)
Dim intByte As Integer
intByte = myConnect.EndReceive(pIAsyncResult)
If intByte > 0 Then
MessageReceive &= Encoding.ASCII.GetString(bteAccept)
End If
End Sub
End Class |
Partager