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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
Public Class server
Public Shared ipadress = System.Configuration.ConfigurationSettings.AppSettings("ipadress")
Public Shared port = System.Configuration.ConfigurationSettings.AppSettings("port")
Public Shared arret As Boolean = False
Public Shared Sub Main()
Dim erreurPort As Boolean = False
Dim E As Exception = Nothing
' creation of listening service
Dim ecoute As TcpListener = Nothing
Dim nbClients As Integer = 0
Try
ecoute = New TcpListener(IPAddress.Parse(ipadress), port)
' start service
ecoute.Start()
' reading loop
Dim Cust As TcpClient = Nothing
While True Or arret = True
' wait information
Cust = ecoute.AcceptTcpClient()
' when customer send something, server create one new thread which manage the request
nbClients += 1
Dim tache As Thread = New Thread(New ThreadStart(AddressOf New CustomerStatement(Cust, nbClients).Run))
tache.Start()
End While
Catch ex As Exception
erreur("Error : " + ex.Message, 3)
End Try
' fin du service
ecoute.Stop()
End Sub
end class
Public Class CustomerStatement
Private sql As String
Private Cust As TcpClient ' our customer
Private numClient As Integer ' customer number
Private [IN] As StreamReader ' reading flux
Private OUT As StreamWriter ' writting flux
' builder
Public Sub New(ByVal Cust As TcpClient, ByVal numClient As Integer)
Me.Cust = Cust
Me.numClient = numClient
End Sub
' méthode run
Public Sub Run()
Try
[IN] = New StreamReader(Cust.GetStream())
OUT = New StreamWriter(Cust.GetStream())
OUT.AutoFlush = True
'statement sub
statement()
'end statement
Cust.Close()
Catch e As Exception
errorInf("Error when the connection was closing" & e.ToString, 2)
End Try
End Sub
end class
Public Class SendInformation
'send information to one customer
Public Shared ipadress = "172.16.11.255"
Public Shared port = 101
Public Shared Sub Main(ByVal message As String)
Dim client As TcpClient = Nothing ' our customer
Dim [IN] As StreamReader = Nothing ' reading flux
Dim OUT As StreamWriter = Nothing ' writting flux
Dim close As Boolean = False
Dim answer As String = Nothing
Dim limit As Integer = 0 'limit the number of lap
Try
'connexion with the customer
client = New TcpClient(ipadress, port)
'Creation of flux in order to communicate
[IN] = New StreamReader(client.GetStream())
OUT = New StreamWriter(client.GetStream())
OUT.AutoFlush = True
While (close = False Or limit = 10)
' send information
OUT.WriteLine(message)
' read answer
answer = [IN].ReadLine()
answer = answer.Substring(3, 2)
If answer = "99" Then
close = True
End If
limit += 1
End While
If limit = 10 Then
MessageBox.Show("Retry this action", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
client.Close()
Catch ex As Exception
erreur(ex.Message, 3)
End Try
End Sub
end class |
Partager