Differenze tra le versioni di "Trasmissione di dati fra due o più programmi Gambas mediante la Classe Socket del Componente gb.net"

Da Gambas-it.org - Wikipedia.
 
(6 versioni intermedie di uno stesso utente non sono mostrate)
Riga 6: Riga 6:
 
<BR>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''.
 
<BR>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'' . <SUP>&#091;[[#Note|Nota 1]]&#093;</sup>
+
Questa pagina mostrerà come creare un programma ''Server Socket'' con le Classi "ServerSocket" e "Socket"  del Componente Gambas ''gb.net'' e due o più programmi ''Client Socket'' mediante la Classe "Socket". <SUP>&#091;[[#Note|Nota 1]]&#093;</sup>
 
<BR>Dopo aver lanciato il ''Server'' e i due ''Client'', si potrà inviare del testo dal primo ''Client'' al secondo e viceversa.  
 
<BR>Dopo aver lanciato il ''Server'' e i due ''Client'', si potrà inviare del testo dal primo ''Client'' al secondo e viceversa.  
  
 
Creiamo il "<B>Server Socket</b>":
 
Creiamo il "<B>Server Socket</b>":
  Public Clients As New Object[]
+
  Private Button1 As Button
  Private MySock As Socket
+
Private TextArea1 As TextArea
 +
Private Clients As New Object[]
 +
  Private ServerSocket1 As ServerSocket
 
   
 
   
 
   
 
   
 
  '''Public''' Sub Form_Open()
 
  '''Public''' Sub Form_Open()
 
    
 
    
 +
  With Button1 = New Button(Me) As "Button1"
 +
    .Text = "  Attiva il Server...  "
 +
    .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 ServerSocket1 = New ServerSocket As "ServerSocket1"
 
  <FONT Color=gray>' ''Si assegna un numero qualsiasi della porta:''</font>
 
  <FONT Color=gray>' ''Si assegna un numero qualsiasi della porta:''</font>
  ServerSocket1.Port = 12345
+
    .Port = 12345
  ServerSocket1.Type = Net.Internet
+
    .Type = Net.Internet
 +
  End With
 
      
 
      
  Button1.Text = "Attiva il Server..."
 
 
 
 
  '''End'''
 
  '''End'''
 
   
 
   
Riga 32: Riga 47:
 
   
 
   
 
  '''Public''' Sub Button1_Click()
 
  '''Public''' Sub Button1_Click()
   
+
 
 +
  <FONT Color.gray>' ''Nell'argomento del Metodo ".Listen() si passa il numero di "Client" che dovranno essere serviti:''</font>
 
   ServerSocket1.Listen(2)
 
   ServerSocket1.Listen(2)
 
    
 
    
Riga 46: Riga 62:
 
  '''Public''' Sub ServerSocket1_Connection(HostIP As String)
 
  '''Public''' Sub ServerSocket1_Connection(HostIP As String)
 
    
 
    
   Me.Text = "Connessione da... " & HostIP
+
   Dim sock As Socket
 
    
 
    
   MySock = ServerSocket1.Accept()
+
   Me.Caption = "Connessione da... " & HostIP
   Clients.Add(MySock)
+
 
   Object.Attach(MySock, Me, "Forza")
+
  sock = ServerSocket1.Accept()
 +
   Clients.Add(sock)
 +
   Object.Attach(sock, Me, "Sock")
 
    
 
    
 
   Write #Clients[Clients.Max], "Connessione al Server accettata !\n"
 
   Write #Clients[Clients.Max], "Connessione al Server accettata !\n"
Riga 57: Riga 75:
 
   
 
   
 
   
 
   
  '''Public''' Sub Forza_Read()
+
  '''Public''' Sub Sock_Read()
 
    
 
    
   Dim strMsg As String
+
   Dim msg As String
 
    
 
    
 
   If Last.Status <> Net.Connected Then Return
 
   If Last.Status <> Net.Connected Then Return
 
    
 
    
   Read #Last, strMsg, Lof(Last)
+
   Read #Last, msg, Lof(Last)
 
    
 
    
   TextArea1.Text &= strMsg
+
   TextArea1.Text &= msg
 
    
 
    
 
   If Clients.Count = 1 Then
 
   If Clients.Count = 1 Then
     Write #Clients[0], strMsg
+
     Write #Clients[0], msg
 
   Else
 
   Else
 
     If Last.RemotePort == Clients[0].RemotePort Then
 
     If Last.RemotePort == Clients[0].RemotePort Then
       Write #Clients[1], strMsg
+
       Write #Clients[1], msg
 
     Else
 
     Else
       Write #Clients[0], strMsg
+
       Write #Clients[0], msg
 
     Endif
 
     Endif
 
   Endif
 
   Endif
 
    
 
    
 
  '''End'''
 
  '''End'''
 
  
  
Riga 90: Riga 107:
 
   
 
   
 
   
 
   
  '''Public''' Sub form_Open()
+
  '''Public''' Sub Form_Open()
 
    
 
    
 
   With Me
 
   With Me
Riga 128: Riga 145:
 
   
 
   
 
  '''Public''' Sub Button1_Click()
 
  '''Public''' Sub Button1_Click()
 
 
  Dim sBuf As String
 
 
    
 
    
 
   SockClient1 = New Socket As "Server"
 
   SockClient1 = New Socket As "Server"
 
  <FONT Color=gray>' ''Si connette al proprgramma "Server" impostando l'indirizzo IP privato e il numero della porta del "Server" medesimo:''</font>
 
  <FONT Color=gray>' ''Si connette al proprgramma "Server" impostando l'indirizzo IP privato e il numero della porta del "Server" medesimo:''</font>
   SockClient1.Connect("<FONT Color=gray>'''Indirizzo_IP_privato'''</font>", 12345)
+
   SockClient1.Connect("<FONT Color=#B22222>''Indirizzo_IP_privato''</font>", 12345)
 
    
 
    
 
   Do While (SockClient1.Status <> 7) And (SockClient1.Status > 0)
 
   Do While (SockClient1.Status <> 7) And (SockClient1.Status > 0)
Riga 140: Riga 155:
 
    
 
    
 
   If SockClient1.Status <> 7 Then
 
   If SockClient1.Status <> 7 Then
     Print "Error"
+
     Print "\e[31mErrore !\e[0m"
 +
    SockClient1.Close
 
     Quit
 
     Quit
 
   Endif
 
   Endif
Riga 175: Riga 191:
  
 
Il codice del secondo "<B>Client Socket</b>" sarà uguale al codice del primo "Client Socket".
 
Il codice del secondo "<B>Client Socket</b>" sarà uguale al codice del primo "Client Socket".
 +
  
  
Riga 180: Riga 197:
 
[1] Da vedere le seguenti pagine web:
 
[1] Da vedere le seguenti pagine web:
 
<BR> - http://gambaswiki.org/wiki/doc/network
 
<BR> - http://gambaswiki.org/wiki/doc/network
 +
<BR> - http://gambaswiki.org/wiki/comp/gb.net/serversocket
 
<BR> - http://gambaswiki.org/wiki/comp/gb.net/socket
 
<BR> - http://gambaswiki.org/wiki/comp/gb.net/socket
 
<BR> - http://captainbodgit.blogspot.com/2016/08/using-sockets-with-gambas.html
 
<BR> - http://captainbodgit.blogspot.com/2016/08/using-sockets-with-gambas.html
Riga 188: Riga 206:
 
<BR> - https://noviello.it/come-trovare-il-proprio-indirizzo-ip-su-linux/
 
<BR> - https://noviello.it/come-trovare-il-proprio-indirizzo-ip-su-linux/
 
<BR> - https://linuxiano.altervista.org/2019/11/indirizzo-ip-dal-terminale-linux/
 
<BR> - https://linuxiano.altervista.org/2019/11/indirizzo-ip-dal-terminale-linux/
 
 
 
<FONT Color=red size=5><B>Pagina in costruzione !</b></font>
 

Versione attuale delle 16:26, 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 con le Classi "ServerSocket" e "Socket" del Componente Gambas gb.net e due o più programmi Client Socket mediante la Classe "Socket". [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":

Private Button1 As Button
Private TextArea1 As TextArea
Private Clients As New Object[]
Private ServerSocket1 As ServerSocket


Public Sub Form_Open()
 
 With Button1 = New Button(Me) As "Button1"
   .Text = "   Attiva il Server...  "
   .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 ServerSocket1 = New ServerSocket As "ServerSocket1"
' Si assegna un numero qualsiasi della porta:
   .Port = 12345
   .Type = Net.Internet
 End With 
   
End

Public Sub Form_Close()

 ServerSocket1.Close
 
End


Public Sub Button1_Click()
 
' Nell'argomento del Metodo ".Listen() si passa il numero di "Client" che dovranno essere serviti:
 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)
 
 Dim sock As Socket
 
 Me.Caption = "Connessione da... " & HostIP
 
 sock = ServerSocket1.Accept()
 Clients.Add(sock)
 Object.Attach(sock, Me, "Sock")
 
 Write #Clients[Clients.Max], "Connessione al Server accettata !\n"
 
End


Public Sub Sock_Read()
 
 Dim msg As String
 
 If Last.Status <> Net.Connected Then Return
 
 Read #Last, msg, Lof(Last)
 
 TextArea1.Text &= msg
 
 If Clients.Count = 1 Then
   Write #Clients[0], msg
 Else
   If Last.RemotePort == Clients[0].RemotePort Then
     Write #Clients[1], msg
   Else
     Write #Clients[0], msg
   Endif
 Endif
 
End


Quindi scriviamo il codice del primo "Client Socket".
Bisognerà individuare il proprio indirizzo IP privato. Questo indirizzo IP privato potrà essere ricavato lanciando da Terminale il comando: ~$ hostname -I

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 Form_Close()
 
 If SockClient1.Status > Net.Inactive Then SockClient1.Close
 
End


Public Sub Button1_Click()
 
 SockClient1 = New Socket As "Server"
' Si connette al proprgramma "Server" impostando l'indirizzo IP privato e il numero della porta del "Server" medesimo:
 SockClient1.Connect("Indirizzo_IP_privato", 12345)
 
 Do While (SockClient1.Status <> 7) And (SockClient1.Status > 0)
   Wait 0.01
 Loop
 
 If SockClient1.Status <> 7 Then
   Print "\e[31mErrore !\e[0m"
   SockClient1.Close
   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 TextArea1_MouseUp()
 
 TextArea1.Clear

End


Il codice del secondo "Client Socket" sarà uguale al codice del primo "Client Socket".


Note

[1] Da vedere le seguenti pagine web:
- http://gambaswiki.org/wiki/doc/network
- http://gambaswiki.org/wiki/comp/gb.net/serversocket
- 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/