Differenze tra le versioni di "Trasmissione di dati fra due o più programmi Gambas mediante la Classe Socket del Componente gb.net"
Riga 87: | Riga 87: | ||
Ecco il codice del primo "Client Socket": | Ecco il codice del primo "Client Socket": | ||
− | Private | + | Private SockClient1 As Socket |
− | Private | + | Private Button1 As Button |
+ | Private Button2 As Button | ||
+ | Private TextArea1 As TextArea | ||
+ | |||
+ | |||
+ | '''Public''' Sub form_Open() | ||
+ | |||
+ | With Me | ||
+ | .Center | ||
+ | .W = 400 | ||
+ | .H = 400 | ||
+ | End With | ||
+ | With Button1 = New Button(Me) As "Button1" | ||
+ | .Text = " Attiva connessione " | ||
+ | .W = .Font.TextWidth(.Text) | ||
+ | .H = .Font.TextHeight(.Text) * 3 | ||
+ | .X = (Me.W / 2) - (.W / 2) | ||
+ | .Y = Me.H * 0.05 | ||
+ | End With | ||
+ | With TextArea1 = New TextArea(Me) As "TextArea1" | ||
+ | .W = Me.W * 0.8 | ||
+ | .H = Me.H * 0.6 | ||
+ | .X = (Me.W / 2) - (.W / 2) | ||
+ | .Y = Button1.Y + Button1.H + 10 | ||
+ | End With | ||
+ | With Button2 = New Button(Me) As "Button2" | ||
+ | .Text = " INVIA TESTO " | ||
+ | .W = .Font.TextWidth(.Text) | ||
+ | .H = .Font.TextHeight(.Text) * 3 | ||
+ | .X = (Me.W / 2) - (.W / 2) | ||
+ | .Y = TextArea1.Y + TextArea1.H + 10 | ||
+ | End With | ||
+ | |||
+ | '''End''' | ||
Riga 96: | Riga 129: | ||
ClientSock_1 = New Socket As "Server" | ClientSock_1 = New Socket As "Server" | ||
− | ClientSock_1.Connect(" | + | <FONT Color=gray>' ''Si connette al proprgramma "Server" impostando l'indirizzo IP privato e il numero della porta del "Server" medesimo:''</font> |
− | + | ClientSock_1.Connect("<FONT Color=gray>'''Indirizzo_IP_privato'''", 12345) | |
+ | |||
Do While (MySock.Status <> 7) And (MySock.Status > 0) | Do While (MySock.Status <> 7) And (MySock.Status > 0) | ||
Wait 0.1 | Wait 0.1 | ||
Loop | Loop | ||
− | + | ||
If ClientSock_1.Status <> 7 Then | If ClientSock_1.Status <> 7 Then | ||
Print "Error" | Print "Error" | ||
Riga 111: | Riga 145: | ||
'''Public''' Sub Button2_Click() | '''Public''' Sub Button2_Click() | ||
− | <FONT Color=gray>' ''Invia testo al Server:''</font> | + | <FONT Color=gray>' ''Invia testo al Server, affinché lo indirizzi all'altro programma "Client":''</font> |
− | Write # | + | Write #SockClient1, TextArea1.Text |
'''End''' | '''End''' | ||
− | '''Public''' Sub Server_Read() | + | '''Public''' Sub Server_Read() <FONT Color=gray>' ''Legge i dati inviati dal Server''</font> |
− | + | ||
Dim buf As String | Dim buf As String | ||
− | Read # | + | Read #SockClient1, buf, Lof(SockClient1) |
With TextArea1 | With TextArea1 | ||
Riga 132: | Riga 166: | ||
'''Public''' Sub Form_Close() | '''Public''' Sub Form_Close() | ||
− | If | + | If SockClient1.Status > Net.Inactive Then SockClient1.Close |
'''End''' | '''End''' |
Versione delle 15:28, 5 mag 2021
I Socket sono definiti come endpoint di comunicazione e si riferiscono a un mezzo di comunicazione del computer che utilizza indirizzi IP e porte.
I Socket vengono generalmente utilizzati per inviare dati tra due computer su una rete, ma possono anche essere utilizzati per comunicare tra due programmi sullo stesso computer.
La struttura base di funzionamento delle Socket è di tipo Client/Server.
Supponiamo di avere due processi p1 e p2. Il processo p2 ha bisogno del processo p1 per eseguire un determinato compito. Il processo p1 offrirà al processo p2 tale servizio, esso perciò sarà il "Servente" ovvero il Server. Il processo p2 che richiede il servizio sarà dunque il "Cliente", ovvero il Client.
Questa pagina mostrerà come creare un programma Server Socket e due o più programmi Client Socket mediante la Classe "Socket" del Componente Gambas gb.net . [Nota 1]
Dopo aver lanciato il Server e i due Client, si potrà inviare del testo dal primo Client al secondo e viceversa.
Creiamo il "Server Socket":
Public Clients As New Object[] Private MySock As Socket Public Sub Form_Open() ' Si assegna un numero qualsiasi della porta: ServerSocket1.Port = 12345 ServerSocket1.Type = Net.Internet Button1.Text = "Attiva il Server..." End Public Sub Form_Close() ServerSocket1.Close End Public Sub Button1_Click() ServerSocket1.Listen(2) If ServerSocket1.Status = Net.Active Then Button1.Enabled = False Button1.Background = Color.Green Button1.Text = "Server attivo !" Me.Caption &= "In attesa sulla Porta: " & ServerSocket1.Port & gb.CrLf End If End Public Sub ServerSocket1_Connection(HostIP As String) Me.Text = "Connessione da... " & HostIP MySock = ServerSocket1.Accept() Clients.Add(MySock) Object.Attach(MySock, Me, "Forza") Write #Clients[Clients.Max], "Connessione al Server accettata !\n" End Public Sub Forza_Read() Dim strMsg As String If Last.Status <> Net.Connected Then Return Read #Last, strMsg, Lof(Last) TextArea1.Text &= strMsg If Clients.Count = 1 Then Write #Clients[0], strMsg Else If Last.RemotePort == Clients[0].RemotePort Then Write #Clients[1], strMsg Else Write #Clients[0], strMsg Endif Endif End Public Sub TextArea1_MouseUp() TextArea1.Clear End
Ecco il codice del primo "Client Socket":
Private SockClient1 As Socket Private Button1 As Button Private Button2 As Button Private TextArea1 As TextArea Public Sub form_Open() With Me .Center .W = 400 .H = 400 End With With Button1 = New Button(Me) As "Button1" .Text = " Attiva connessione " .W = .Font.TextWidth(.Text) .H = .Font.TextHeight(.Text) * 3 .X = (Me.W / 2) - (.W / 2) .Y = Me.H * 0.05 End With With TextArea1 = New TextArea(Me) As "TextArea1" .W = Me.W * 0.8 .H = Me.H * 0.6 .X = (Me.W / 2) - (.W / 2) .Y = Button1.Y + Button1.H + 10 End With With Button2 = New Button(Me) As "Button2" .Text = " INVIA TESTO " .W = .Font.TextWidth(.Text) .H = .Font.TextHeight(.Text) * 3 .X = (Me.W / 2) - (.W / 2) .Y = TextArea1.Y + TextArea1.H + 10 End With End Public Sub Button1_Click() Dim sBuf As String ClientSock_1 = New Socket As "Server" ' Si connette al proprgramma "Server" impostando l'indirizzo IP privato e il numero della porta del "Server" medesimo: ClientSock_1.Connect("Indirizzo_IP_privato", 12345) Do While (MySock.Status <> 7) And (MySock.Status > 0) Wait 0.1 Loop If ClientSock_1.Status <> 7 Then Print "Error" Quit Endif End Public Sub Button2_Click()
' Invia testo al Server, affinché lo indirizzi all'altro programma "Client": Write #SockClient1, TextArea1.Text End Public Sub Server_Read() ' Legge i dati inviati dal Server Dim buf As String Read #SockClient1, buf, Lof(SockClient1) With TextArea1 .Wrap = True .Text = buf & gb.NewLine .SetFocus End With End Public Sub Form_Close() If SockClient1.Status > Net.Inactive Then SockClient1.Close End
Ecco il codice del secondo "Client Socket":
Note
[1] Da vedere le seguenti pagine web:
- http://gambaswiki.org/wiki/doc/network
- http://gambaswiki.org/wiki/comp/gb.net/socket
- http://captainbodgit.blogspot.com/2016/08/using-sockets-with-gambas.html
- https://jsbsan.blogspot.com/2012/07/arduino-y-linux-ejemplo-de-uso-de.html
- https://docs.oracle.com/cd/E19620-01/805-4041/index.html
- http://linuxdidattica.org/docs/altre_scuole/planck/socket/progr-socket.pdf
- https://www.wikihow.it/Controllare-l%27Indirizzo-IP-su-Linux
- https://noviello.it/come-trovare-il-proprio-indirizzo-ip-su-linux/
- https://linuxiano.altervista.org/2019/11/indirizzo-ip-dal-terminale-linux/
Pagina in costruzione !