Bonjour,
Je développe actuellement une appli Client/Serveur, c'est vraiment pour commencer...
J'utilise les Socket, j'arrive a connecter les deux cotés, maintenant je voudrais envoyer un message à l'autre...
Voila mon code pour le Client :
Et coté serveur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Option Strict On Imports System Imports System.io Imports System.Net Imports System.Net.Sockets Public Class Form2 Inherits System.Windows.Forms.Form Private Client As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.IP) 'trafic ip standard Friend WithEvents Button1 As System.Windows.Forms.Button Dim addr As System.Net.IPAddress Private Sub connexion_acceptee(ByVal ar As IAsyncResult) Client.EndConnect(ar) MsgBox("client : " & Client.Connected) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click addr = addr.Parse("192.168.2.2") Client.BeginConnect(New IPEndPoint(addr, 150), New System.AsyncCallback(AddressOf connexion_acceptee), Client) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 'Dim Message As [Byte]() = System.Text.Encoding.ASCII.GetBytes("Test envoi de données") 'Dim OctetsEnvoyes As System.Int64 = Client.Send(Message, 0, Message.Length, SocketFlags.None) Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes("Test envoi de données") Dim i As Integer = Client.Send(msg, 0, msg.Length, SocketFlags.None) 'Console.WriteLine("Sent {0} bytes.", i) MsgBox(i) End Sub End Class
Comment faire pour récupérer le message du client ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Imports System Imports System.Net Imports System.Net.sockets Public Class Form1 Inherits System.Windows.Forms.Form Private Serveur As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.IP) Private Sub connexion_requested(ByVal ar As System.IAsyncResult) Serveur.EndAccept(ar) MessageBox.Show("Connexion Acceptée") End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Form2 As New Form2 Form2.Show() Dim Addr As IPAddress Dim hostIP As System.Net.IPAddress = Addr.Parse("192.168.2.2") Dim ep As New System.Net.IPEndPoint(hostIP, 150) Serveur.Bind(ep) Serveur.Listen(1) Serveur.BeginAccept(New System.AsyncCallback(AddressOf connexion_requested), Serveur) End Sub Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Timer1.Enabled = True End Sub End Class
Ce code c'est de la récup d'un peu partout sur le net, j'essaye de comprendre en meme temps, j'attaque direct un truc compliqué je pense mais bon, si quelqu'un peut me filer un coup de pouce ca serait cool...
Merci d'avance
Partager