Imports System Imports System.Net Imports System.Net.Sockets Imports System.IO Imports Microsoft.VisualBasic Imports System.Text Imports System.Threading '-------- The following code is a modified version of a VB sample code "NetworkStreamSampleVBReceiver". --------' '---------------- The orginal script was downloaded from www.winsocketdotnetworkprogramming.com ----------------' '------------ The name of the process has changed from "NetworkStreamSampleVBReceiver" to SWAsrvapp ------------' ' This sample demonstrates how To use the NetworkStream Class To ' perform IO between 2 sockets. When using TCP Sockets you have to ' build a listening Socket application that will receive a connection ' from another application. In Socket terminology the application that ' receives a connection is know as a server and the peer that connects ' to a server is know as a client. ' ' The focus of this sample is not on Sockets but on network streams. We ' call this application a NetworkStreamServer because it is designed to ' receive a Socket connection and perform IO using a NetworkStream. ' ' To run this sample, simply just run the program without parameters and ' it will listen for a client connection on TCP PortOUT 5150. If you want to ' use a different PortOUT than 5150 then you can optionally supply a command ' line parameter "/PortOUT " and the listening socket will use ' your PortOUT instead. Module Module1 ' Count the packet number (MODIFIED) 'Public OUTpkcounter As Integer = 0 '(MODIFIED) Public ServerSocketOUT As Socket = Nothing Public ListeningSocketOUT As Socket = Nothing ' Let's create a network stream to communicate over the connected Socket. Public ServerNetworkStreamOUT As NetworkStream = Nothing ' The following are default values, change accordingly Public ServerNameOUT As String = "0.0.0.0" '"127.0.0.1" Public PortOUT As Integer = 12345 'Dim pathSource As String = "incomingdata.bin" '(MODIFIED) 'Dim pathSource As String = "C:\Users\Ali\Documents\Data_for_Ali\From DPU - FFT - HK and Sci\SWA_PFM_FFT_Formal_08-08-2017.bin" '(MODIFIED) Public pathSource As String = "C:\Users\Ali\Documents\Data_for_Ali\From DPU - FFT - HK And Sci\Hex to ascii by Chandra\EAS2_3D_Nominal" '(MODIFIED) ' Count the packet number (MODIFIED) Public INpkcounter As Integer = 1 '(MODIFIED) Public ServerSocketIN As Socket = Nothing Public ListeningSocketIN As Socket = Nothing ' Let's create a network stream to communicate over the connected Socket. Public ServerNetworkStreamIN As NetworkStream = Nothing ' The following are default values, change accordingly Public ServerNameIN As String = "0.0.0.0" '"127.0.0.1" Public PortIN As Integer = 12346 Public BytesReadIN As Integer = 0 Public ReadBufferIN(4096) As Byte Public BytestotextIN As String = "" Public INdataresentOUT(4096) As Byte Public INcapturetimedate As Date = System.DateTime.UtcNow Public dtime = (System.DateTime.UtcNow - INcapturetimedate) Public INcapturefileName As String = "Data\Recent\IncomingSWAdata_" + System.DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ") + ".dat" ' The main entry point for the application. Sub Main() Console.Title = "SWA SIIS client" ' Parse command line arguments if any Dim args As String() = Environment.GetCommandLineArgs() Dim i As Integer For i = 0 To args.Length - 1 Try If args(i) = "/PortOUT" Then ' The PortOUT on which the server is listening i = i + 1 PortOUT = System.Convert.ToInt32(args(i).ToString()) End If Catch e As System.IndexOutOfRangeException Console.WriteLine("Usage: Receiver [/PortOUT ]") Exit Sub End Try Next Console.ForegroundColor = ConsoleColor.White 'SockOUT_connection() SockIN_connection() FStream_create() Do incomingtoSockIN() '''''''''''''''''''''''''''''''''Console.WriteLine("") '''''''''''''''''''''''''''''''''ListeningSocketIN.Close() Console.ForegroundColor = ConsoleColor.White Console.Write("Packet No. " + CStr(INpkcounter) + " received at ") Console.ForegroundColor = ConsoleColor.Yellow Console.WriteLine(System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")) Console.WriteLine("") INpkcounter += 1 '''''''''''''''''''''''''''''''''ServerNetworkStreamIN.Close() '''''''''''''''''''''''''''''''''ListeningSocketIN.Close() If BytesReadIN = 0 Then 'INcapturefileStream.Close() ServerNetworkStreamIN.Close() ListeningSocketIN.Close() ServerNetworkStreamOUT.Close() ListeningSocketOUT.Close() Exit Do End If Loop Console.ForegroundColor = ConsoleColor.White End Sub Sub SockOUT_connection() Try ' Setup a listening Socket to await a connection from a peer socket. ListeningSocketOUT = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP) Dim ListeningEndPointOUT As IPEndPoint = New IPEndPoint(IPAddress.Parse(ServerNameOUT), PortOUT) ListeningSocketOUT.Bind(ListeningEndPointOUT) Console.ForegroundColor = ConsoleColor.Green '(MODIFIED) Console.WriteLine("OUTBind is OK...") '(MODIFIED) ListeningSocketOUT.Listen(5) Console.WriteLine("OUTListen is OK...") '(MODIFIED) Console.ForegroundColor = ConsoleColor.Cyan '(MODIFIED) Console.WriteLine("Awaiting a TCP OUTconnection on IP: " _ + ListeningEndPointOUT.Address.ToString() _ + " Port: " _ + ListeningEndPointOUT.Port.ToString() _ + "...") 'Console.WriteLine("Please run your analysis client program, I'm waiting...") '(MODIFIED) ServerSocketOUT = ListeningSocketOUT.Accept() Console.ForegroundColor = ConsoleColor.Green '(MODIFIED) Console.WriteLine("OUTAccept is OK") '(MODIFIED) 'Dim proc As New System.Diagnostics.Process() '(MODIFIED) 'proc = Process.Start("SWA_TVsrvapp.bat", "") '(MODIFIED) Dim ipOUT As IPEndPoint = ServerSocketOUT.RemoteEndPoint '(MODIFIED) Console.ForegroundColor = ConsoleColor.Cyan '(MODIFIED) Console.WriteLine("Received an OUTconnection from IPaddress: " + CStr(ipOUT.Address.ToString) + " on Port No.: " + CStr(ipOUT.Port)) '(MODIFIED) Catch e As SocketException Console.ForegroundColor = ConsoleColor.DarkRed '(MODIFIED) Console.WriteLine("Failure to create OUTSockets: " + e.Message) Exit Sub Finally ' Close the listening socket - we do not plan to handle any ' additional connections. ListeningSocketOUT.Close() End Try Try ' Setup a network stream on the server Socket Console.ForegroundColor = ConsoleColor.Cyan '(MODIFIED) Console.WriteLine("Instantiate a NetworkStream object...") ServerNetworkStreamOUT = New NetworkStream(ServerSocketOUT, True) Catch e As Exception Console.ForegroundColor = ConsoleColor.DarkRed '(MODIFIED) Throw New Exception("Failed to create a Network Stream with error: " + e.Message) End Try End Sub Sub sendtoSockOUT(ReadBufferIN) Try Try Console.ForegroundColor = ConsoleColor.Cyan '(MODIFIED) Console.WriteLine("Sending the buffered bytes...") '(MODIFIED) '---------------------------------------------------------------------------------------------------------------------------------------------' '-------- This part of the code was copy pasted from https://msdn.microsoft.com/en-us/library/system.io.filestream.read(v=vs.110).aspx--------' Try 'Using fsSource As FileStream = New FileStream(pathSource, FileMode.Open, FileAccess.Read) ' Read the source file into a byte array. 'Dim bytes() As Byte = New Byte((fsSource.Length) - 1) {} 'Dim numBytesToRead As Integer = CType(fsSource.Length, Integer) 'Dim numBytesRead As Integer = 0 'While (numBytesToRead > 0) ' Read may return anything from 0 to numBytesToRead. 'Dim n As Integer = fsSource.Read(bytes, numBytesRead, numBytesToRead) ' Break when the end of the file is reached. 'If (n = 0) Then 'Exit While 'End If 'numBytesRead = (numBytesRead + n) 'numBytesToRead = (numBytesToRead - n) ' send the byte array to the socket 'For i = 1 To n INdataresentOUT = ReadBufferIN If ServerSocketOUT.Connected Then ServerNetworkStreamOUT.Write(INdataresentOUT, 0, INdataresentOUT.GetUpperBound(0)) Else Console.WriteLine("ServerSocketOUT was closed") 'If ListeningSocketOUT.Connected Then ListeningSocketOUT.Close() 'SockOUT_connection() 'If ListeningSocketOUT.Connected And ServerSocketOUT.Connected Then 'ServerNetworkStreamOUT.Write(INdataresentOUT, 0, INdataresentOUT.GetUpperBound(0)) 'End If End If 'Next 'End While 'numBytesToRead = bytes.Length 'End Using Catch ioEx As FileNotFoundException Console.WriteLine(ioEx.Message) End Try '---------------------------------------------------------------------------------------------------------------------------------------------' '---------------------------------------------------------------------------------------------------------------------------------------------' Console.ForegroundColor = ConsoleColor.Cyan '(MODIFIED) 'Console.WriteLine("We sent " + CStr(OUTpkcounter) + " packets.") '(MODIFIED) 'Console.ForegroundColor = ConsoleColor.Green '(MODIFIED) ' Console.WriteLine("Transfer completed.") '(MODIFIED) Catch e As Exception ServerNetworkStreamOUT.Close() 'Console.ForegroundColor = ConsoleColor.Cyan '(MODIFIED) 'Console.WriteLine("We sent " + CStr(OUTpkcounter) + " packets.") '(MODIFIED) ' Console.ForegroundColor = ConsoleColor.DarkRed '(MODIFIED) Throw New Exception("Failed to send NetworkStream with error: " + e.Message) End Try Catch e As Exception Console.ForegroundColor = ConsoleColor.DarkRed Console.WriteLine(e.Message) Finally ' We are finished with the NetworkStream, so we will close it. ' Note: the ServerSocketOUT will be closed by the NetworkStream. 'ServerNetworkStreamOUT.Close() End Try End Sub Sub SockIN_connection() Try ' Setup a listening Socket to await a connection from a peer socket. ListeningSocketIN = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP) Dim ListeningEndPointIN As IPEndPoint = New IPEndPoint(IPAddress.Parse(ServerNameIN), PortIN) ListeningSocketIN.Bind(ListeningEndPointIN) Console.ForegroundColor = ConsoleColor.DarkGreen '(MODIFIED) 'Console.WriteLine("INBind is OK...") '(MODIFIED) ListeningSocketIN.Listen(5) 'Console.WriteLine("INListen is OK...") '(MODIFIED) Console.ForegroundColor = ConsoleColor.Green '(MODIFIED) Console.WriteLine("TCP listening on IP: " _ + ListeningEndPointIN.Address.ToString() _ + " Port: " _ + ListeningEndPointIN.Port.ToString() _ + "...") 'Console.WriteLine("Please run your analysis client program, I'm waiting...") '(MODIFIED) ServerSocketIN = ListeningSocketIN.Accept() Console.ForegroundColor = ConsoleColor.DarkGreen '(MODIFIED) 'Console.WriteLine("INAccept is OK") '(MODIFIED) 'Dim proc As New System.Diagnostics.Process() '(MODIFIED) 'proc = Process.Start("SWA_TVsrvapp.bat", "") '(MODIFIED) Dim ipIN As IPEndPoint = ServerSocketIN.RemoteEndPoint '(MODIFIED) Console.ForegroundColor = ConsoleColor.Cyan '(MODIFIED) Console.WriteLine("Received an connection from IPaddress: " + CStr(ipIN.Address.ToString) + " on Port No.: " + CStr(ipIN.Port)) '(MODIFIED) Catch e As SocketException Console.ForegroundColor = ConsoleColor.DarkRed '(MODIFIED) Console.WriteLine("Failed to create sockets: " + e.Message) Exit Sub Finally ' Close the listening socket - we do not plan to handle any ' additional connections. ListeningSocketIN.Close() End Try Try ' Setup a network stream on the server Socket Console.ForegroundColor = ConsoleColor.DarkCyan '(MODIFIED) 'Console.WriteLine("Instantiate a NetworkStream object...") ServerNetworkStreamIN = New NetworkStream(ServerSocketIN, True) Catch e As Exception Console.ForegroundColor = ConsoleColor.DarkRed '(MODIFIED) Throw New Exception("Failed to create the stream with error; " + e.Message) End Try End Sub Sub incomingtoSockIN() Try Try Console.ForegroundColor = ConsoleColor.Cyan '(MODIFIED) Console.Write("Recieving a packet ") '(MODIFIED) 'Do Dim ReadBufferIN(4096) As Byte BytesReadIN = ServerNetworkStreamIN.Read(ReadBufferIN, 0, ReadBufferIN.GetUpperBound(0)) Console.WriteLine("of " + BytesReadIN.ToString() + " byte(s).") 'Console.WriteLine("ReadBufferIN length: " + ReadBufferIN.Length.ToString) 'Console.WriteLine("ReadBufferIN.GetUpperBound(0) size:" + CStr(ReadBufferIN.GetUpperBound(0))) For i = 0 To BytesReadIN - 1 BytestotextIN += ChrW(CInt(ReadBufferIN(i).ToString())) Next 'Loop Until BytesReadIN > 0 'Console.WriteLine(BytestotextIN) BytestotextIN = "" 'sendtoSockOUT(ReadBufferIN) FStream_appendwrite(ReadBufferIN) Catch e As Exception Console.ForegroundColor = ConsoleColor.DarkRed Throw New Exception("Failed to read from a network stream with error: " + e.Message) End Try Catch e As Exception Console.ForegroundColor = ConsoleColor.DarkRed Console.WriteLine(e.Message) Finally ' We are finished with the NetworkStream, so we will close it. ' Note: the ServerSocket will be closed by the NetworkStream. '''''''''''''''''''''''''''''''''ServerNetworkStreamIN.Close() '''''''''''''''''''''''''''''''''ListeningSocketIN.Close() End Try End Sub '--------------------------------------------------------------------------------------------------------------------------------------------------' '-------- This part of the code was copy pasted from https://msdn.microsoft.com/en-us/library/system.io.filestream.writebyte(v=vs.110).aspx--------' Sub FStream_create() Dim INcapturefileStream As FileStream = New FileStream(INcapturefileName, FileMode.Create) INcapturefileStream.Close() End Sub Sub FStream_appendwrite(ReadBufferIN) Dim INcapturefileStream = New FileStream(INcapturefileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite) Try ' Write the data to the file, byte by byte as it comes out from SIIS (Ascii hex) Dim asciihextoint As Integer For i As Integer = 0 To BytesReadIN - 1 Try asciihextoint = Convert.ToInt32(ChrW(CInt(ReadBufferIN(i).ToString())) + ChrW(CInt(ReadBufferIN(i + 1).ToString())), 16) INcapturefileStream.WriteByte(Convert.ToByte(asciihextoint)) i += 1 Catch e As Exception End Try Next i dtime = (System.DateTime.UtcNow - INcapturetimedate) If INcapturefileStream.Length >= (104857600 / 20) Then '5MB INcapturefileStream.Close() If dtime.TotalMinutes >= 1 Then 'Preventing from overwriting with same filename. INcapturefileName = "Data\Recent\IncomingSWAdata_" + System.DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ") + ".dat" INcapturetimedate = System.DateTime.UtcNow Else Thread.Sleep(1100) INcapturefileName = "Data\Recent\IncomingSWAdata_" + System.DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ") + ".dat" INcapturetimedate = System.DateTime.UtcNow End If FStream_create() Exit Try End If dtime = (System.DateTime.UtcNow - INcapturetimedate) If dtime.TotalMinutes >= 5 Then '5 minutes INcapturefileStream.Close() INcapturefileName = "Data\Recent\IncomingSWAdata_" + System.DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ") + ".dat" INcapturetimedate = System.DateTime.UtcNow FStream_create() Exit Try End If Finally INcapturefileStream.Close() End Try End Sub '--------------------------------------------------------------------------------------------------------------------------------------------------' '--------------------------------------------------------------------------------------------------------------------------------------------------' End Module