| Deisuke |
| | Ho un client e un server, il mio problema in breve è che se invio un messaggio dal client al server, e poi voglio mettere in ascolto il client per ricevere la risposta del server non so come fare.. non so se mi sono spiegato.. riporto il codice dei due form. CODICE Imports System.Net Imports System.Net.Sockets
Public Class frmClient
'------------------------------------------CODICE CLIENT ------------------------------------------------------------ Dim client As Socket Dim bytedata(1023) As Byte Dim messaggio As String
Private Sub cmdConnetti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnetti.Click client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim IpAddress As IPAddress = IpAddress.Parse("127.0.0.1") Dim IpEndPoint As IPEndPoint = New IPEndPoint(IpAddress, 1200) client.BeginConnect(IpEndPoint, New AsyncCallback(AddressOf onconnect), Nothing) End Sub
Private Sub onconnect(ByVal ar As IAsyncResult) client.EndConnect(ar) client.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf onreceive), client) End Sub
Private Sub onreceive(ByVal ar As IAsyncResult) Dim client As Socket = ar.AsyncState client.EndReceive(ar) Dim bytericevuti As Byte() = bytedata Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytericevuti) Read(message, RichTxtConnessione) client.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf onreceive), client) End Sub
Delegate Sub _read(ByVal msg As String, ByVal richtxt As Object) Private Sub Read(ByVal msg As String, ByVal richtxt As Object) If InvokeRequired Then Invoke(New _read(AddressOf Read), msg, richtxt) Exit Sub End If richtxt.text = msg End Sub
Private Sub send(ByVal msg As String, ByVal client As Socket) Dim sendbyte As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(msg) client.BeginSend(sendbyte, 0, sendbyte.Length, SocketFlags.None, New AsyncCallback(AddressOf onsend), client) End Sub Private Sub onsend(ByVal ar As IAsyncResult) Dim client As Socket = ar.AsyncState Dim server As Socket = ar.AsyncState
client.EndSend(ar)
End Sub
Private Sub frmClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load frmServer.Show() End Sub
'------------------------------------------- fine codice client ---------------------------------------------------
Private Sub cmdInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInfo.Click send("info", client) End Sub End Class CODICE Public Class frmServer
'----------------------------------------------------- Codice Server---------------------------------------- Dim client As Socket Dim server As Socket Dim bytedata(1023) As Byte Private Sub frmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load server = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim IpEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 1200)
server.Bind(IpEndPoint) server.Listen(3) server.BeginAccept(New AsyncCallback(AddressOf onaccept), Nothing) End Sub
Private Sub onaccept(ByVal ar As IAsyncResult) client = server.EndAccept(ar) server.BeginAccept(New AsyncCallback(AddressOf onaccept), Nothing) addclient(client) client.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf onreceive), client) End Sub Delegate Sub _addclient(ByVal client As Socket)
Private Sub addclient(ByVal client As Socket) If InvokeRequired Then Invoke(New _addclient(AddressOf addclient), client) Exit Sub End If
Dim lvi As New ListViewItem(client.LocalEndPoint.ToString) lvi.Tag = client ListView1.Items.Add(lvi) End Sub
Private Sub send(ByVal msg As String, ByVal client As Socket) Dim sendbyte As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(msg) client.BeginSend(sendbyte, 0, sendbyte.Length, SocketFlags.None, New AsyncCallback(AddressOf onsend), client) End Sub Private Sub onsend(ByVal ar As IAsyncResult) Dim client As Socket = ar.AsyncState client.EndSend(ar) End Sub
Private Sub onreceive(ByVal ar As IAsyncResult) Dim client As Socket = ar.AsyncState client.EndReceive(ar) Dim bytericevuti As Byte() = bytedata Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytericevuti) Read(message) client.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf onreceive), client) End Sub
Delegate Sub _read(ByVal msg As String) Private Sub Read(ByVal msg As String) If InvokeRequired Then Invoke(New _read(AddressOf Read), msg) Exit Sub End If ListView1.Clear() ListView1.Items.Add(msg) If msg = "info" Then send(My.Computer.Name.ToString, client) End If End Sub
'---------------------------------------- FINE CODICE PER CONNESSIONE -----------------------------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click send("Message from server", client) End Sub
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
End Sub End Class Se volete altre spiegazioni cercherò di essere il più chiarò possibile. Grazie.
| | |
| |
|