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
|
Imports System.Net.sockets
Imports System
Imports System.Net
Imports System.Text
Imports System.Threading
Imports Microsoft.VisualBasic
Imports System.ServiceProcess.ServiceBase
Public Class Calcule
Public Shared reception As New Threading.ManualResetEvent(False)
Private listener As Net.Sockets.TcpListener
Const PORT_NUM As Integer = 1039
Dim EndPoint As Net.IPEndPoint
Protected Overrides Sub OnStart(ByVal args() As String)
connec()
End Sub
Public Sub connec()
Try
Dim iip As Long = 16777343
EndPoint = New Net.IPEndPoint(iip, 1039)
Dim listener As New TcpListener(EndPoint)
listener.Start()
reception.Reset()
listener.BeginAcceptSocket(New AsyncCallback(AddressOf ReadCallback), listener)
reception.WaitOne()
Catch ex As Exception
Dim toto As System.IO.StreamWriter = New IO.StreamWriter("c:\data.txt")
toto.Write(ex.Message)
toto.Close()
End Try
End Sub
Public Shared Sub ReadCallback(ByVal ar As IAsyncResult)
Dim bit(1024) As Byte
Dim ch1, ch2, ch3, ch4 As String
Dim nb1, nb2, nb3 As Double
Dim tab() As String
ch1 = Nothing
ch2 = Nothing
ch3 = Nothing
Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim soock As Socket = listener.EndAcceptSocket(ar)
soock.Receive(bit, bit.Length, SocketFlags.None)
ch1 = System.Text.Encoding.ASCII.GetString(bit, 0, bit.Length)
tab = ch1.Split("/")
ch2 = tab.GetValue(0)
ch3 = tab.GetValue(1)
nb1 = Double.Parse(ch2)
nb2 = Double.Parse(ch3)
nb3 = nb1 + nb2
ch4 = nb3.ToString
Dim tata As System.IO.StreamWriter = New IO.StreamWriter("c:\data2.txt")
tata.Write(ch4)
tata.Close()
reception.Set()
End Sub
Protected Overrides Sub OnStop()
listener.Stop()
End Sub
End Class |
Partager