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
| #Region "Imports"
Imports System.Net
Imports System.Text
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
#End Region
#Region "Form1"
Public Class Form1
#Region "Dim Socket"
'Création du nouveau Socket
Dim CaraSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'Numéro de Port
Dim CaraPort As Int32 = 6666
'Résolution de l'hôte
Dim CaraHost As IPHostEntry = Dns.GetHostEntry("www.bidule.com")
'Adresse IP de l'hôte
Dim CaraIP As IPAddress = CaraHost.AddressList(0)
'Point de terminaison
Dim CaraIPEnd As New IPEndPoint(CaraIP, CaraPort)
'Buffer
Dim CaraBuffer(655360) As Byte
#End Region
#Region "Form1 Load"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
#End Region
#Region "Bouton Connexion"
Private Sub btn_CaraConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_CaraConnect.Click
If btn_CaraConnect.Text = "Connexion" Then
Try
'Info barre de status
Tab_StatusInfos.Text = "Tentative de connection"
'Connexion du Client
CaraSocket.BeginConnect(CaraIPEnd, AddressOf CaraConnectCallback, CaraSocket) 'c'est ici que ca merdouille
'Timer1.Enabled = True
Dim MessRecept As Integer = CaraSocket.Receive(CaraBuffer, 0, CaraBuffer.Length, SocketFlags.None)
If MessRecept <> 0 Then
RTF_Info.Text = Encoding.ASCII.GetString(CaraBuffer)
End If
MessRecept = CaraSocket.Receive(CaraBuffer, 0, CaraBuffer.Length, SocketFlags.None)
If MessRecept <> 0 Then
RTF_Info.Text = Encoding.ASCII.GetString(CaraBuffer)
End If
Catch
Tab_StatusInfos.Text = "Echec de connection"
End Try
Else
btn_CaraConnect.Text = "Connexion"
End If
End Sub
#End Region
#Region "Connexion CallBack"
Private Sub CaraConnectCallback(ByVal asyncResult As IAsyncResult)
Try
CaraSocket.ConnectAsync(asyncResult)
CaraSocket.EndConnect(asyncResult)
Tab_StatusInfos.Text = "Connectée"
btn_CaraConnect.Text = "Déconnexion"
Catch
Tab_StatusInfos.Text = "Echec de connection"
End Try
End Sub
#End Region
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If CaraSocket.Connected Then
Try
CaraSocket.BeginReceive(CaraBuffer, 0, CaraBuffer.Length, SocketFlags.None, AddressOf ReceptionCallback, CaraSocket)
Catch
End Try
End If
End Sub
Private Sub ReceptionCallback(ByVal asyncResult As IAsyncResult)
'If CaraSocket.Connected Then
Try
Dim read As Integer = CaraSocket.EndReceive(asyncResult)
If read <> 0 Then
RTF_Info.Text = Encoding.ASCII.GetString(CaraBuffer)
Debug.Print(Encoding.ASCII.GetString(CaraBuffer))
End If
Catch
End Try
'End If
End Sub
Private Sub EnvoiCallback(ByVal asyncResult As IAsyncResult)
If CaraSocket.Connected Then
Try
Dim send As Integer = CaraSocket.EndSend(asyncResult)
Catch
End Try
End If
End Sub
#End Region
End Class
#End Region |
Partager