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
| Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call serveur()
End Sub
Public Sub serveur()
NotifyIcon1.Visible = True
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipText = "Cette le serveur est ici marche bien"
NotifyIcon1.ShowBalloonTip(20)
ip = IPAddress.Parse("127.0.0.1")
ecoute = New TcpListener(ip, port)
ecoute.Start()
Me.Text = "Ecoute .."
clt = ecoute.AcceptTcpClient()
NotifyIcon1.BalloonTipText = "Un client vient de se connecter"
NotifyIcon1.ShowBalloonTip(20)
Dim NWStream As NetworkStream = clt.GetStream
Const BUFFER_SIZE As Integer = 10
Dim bytesToRead(BUFFER_SIZE) As Byte
'---read incoming stream
Dim textReceived As String = ""
Do
Dim numBytesRead As Integer = _
NWStream.Read(bytesToRead, 0, BUFFER_SIZE)
textReceived += Encoding.ASCII.GetString(bytesToRead, _
0, numBytesRead)
Loop Until Not NWStream.DataAvailable
NWStream.Flush()
RB_recu.Text += "Received :" + textReceived
End Sub
Public Sub client()
clt.Connect("127.0.0.1", port)
NotifyIcon1.Visible = True
NotifyIcon1.BalloonTipText = "Le client a démarré"
NotifyIcon1.ShowBalloonTip(50)
End Sub
Private Sub cmd_envoie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_envoie.Click
If Rb_envoie.Text <> "" Then
Dim NWStream As NetworkStream = clt.GetStream
Dim bytesToSend As Byte() = Encoding.ASCII.GetBytes(Rb_envoie.Text)
MessageBox.Show("Le message : " & Rb_envoie.Text & " a été envoyé", "", MessageBoxButtons.OK)
NWStream.Write(bytesToSend, 0, bytesToSend.Length)
NotifyIcon1.BalloonTipText = "Le client a envoyé le message"
NotifyIcon1.ShowBalloonTip(50)
RB_recu.Text += "Le client dit : " + vbCrLf + vbCrLf + Rb_envoie.Text
Rb_envoie.Text = ""
NWStream.Flush()
End If
End Sub |