Torna al Thread
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Threading
Imports System.Net
Imports System.Printing
Public Class Form1
Dim DatiStampa As String
Dim DatiIntestazione
Dim SwitchDati As Boolean
Public Listener As New TcpListener(515)
Dim Client As TcpClient
Dim NetStr As NetworkStream
Private Delegate Sub DelegateAddText(ByVal str As String)
Private MethodDelegateAddText As New DelegateAddText(AddressOf AddText)
Private Delegate Sub Aggiorna()
Private MetodoAgg As New Aggiorna(AddressOf agg)
Private Delegate Sub DelegateAddText2(ByVal str2 As String)
Private MethodDelegateAddText2 As New DelegateAddText2(AddressOf AddText2)
Private Sub agg()
Me.Refresh()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub AddText(ByVal str As String)
Text1.Text = str & Text1.Text
End Sub
Private Sub AddText2(ByVal str As String)
Text2.Text = Text2.Text & str
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Listener.Start()
Dim serverThread As New Thread(New ThreadStart(AddressOf StartListen))
serverThread.Start()
Catch ex As Exception
Debug.WriteLine(ex.StackTrace.ToString())
End Try
End Sub
Private Sub StartListen()
While True
Try
Client = Listener.AcceptTcpClient()
'Inizializza lo stream
NetStr = Client.GetStream
Dati()
Catch ex As Exception
Debug.WriteLine(ex.StackTrace.ToString())
End Try
End While
End Sub
Public Sub Dati()
Dim dati As String
While True
System.Windows.Forms.Application.DoEvents()
Try
If NetStr.CanRead Then
'Legge Client.ReceiveBufferSize bytes a partire dal primo
'dallo stream e li deposita In Bytes
'se ci sono bytes nulli, non verranno contati
'di Default, Client.ReceiveBufferSize = 8129
Dim Bytes(Client.ReceiveBufferSize) As Byte
NetStr.Read(Bytes, 0, Bytes.Length)
Dim AppoggioSwitch As String
Dim mess(0) As Byte
'Trasforma i bytes ricevuti In stringa
dati = System.Text.Encoding.ASCII.GetString(Bytes)
If SwitchDati = False Then
Client.NoDelay = True
AppoggioSwitch = dati.Substring(1)
If NetStr.CanWrite Then
mess(0) = CByte(0)
NetStr.Write(mess, 0, mess.Length)
End If
End If
Me.Invoke(MethodDelegateAddText, "[Ricezione dati]" & dati & vbLf & vbLf)
Me.Invoke(MethodDelegateAddText2, dati)
DatiStampa = DatiStampa & dati
If dati.Contains("%%EOF") Then
mess(0) = CByte(0)
NetStr.Write(mess, 0, mess.Length)
Client.Close()
Dim PsStart As Long
PsStart = DatiStampa.IndexOf("%!PS")
Dim DatiS As String
DatiS = DatiStampa.Substring(PsStart)
Dim fs As FileStream
fs = New FileStream("c:\test2.ps", IO.FileMode.Append)
Dim s As New StreamWriter(fs)
s.WriteLine(DatiS)
s.Close()
'Form2.Show()
'Form2.Enabled = True
'Form2.Refresh()
Dim nomefile As String
nomefile = NFile()
gsapi.ConvertFile(nomefile)
System.IO.File.Delete("c:\test2.ps")
dati = Nothing
DatiStampa = ""
DatiIntestazione = ""
SwitchDati = False
Me.Invoke(MetodoAgg)
System.Threading.Thread.Sleep(1000)
Exit While
End If
If AppoggioSwitch = Chr(3) Then
SwitchDati = True
End If
End If
Catch ex As Exception
Debug.WriteLine(ex.StackTrace.ToString())
End Try
End While
End Sub
Public Function NFile() As String
Dim num As Integer
Dim num2 As Integer
Dim nome As String
Dim n1 As Integer
Dim n2 As Integer
Dim dt As String
Dim nomefile As String
num = DatiStampa.IndexOf("%%Title:") + 8
num2 = (DatiStampa.IndexOf("%%Creator:")) - num
nome = DatiStampa.Substring(num, num2)
If nome.IndexOf(".") Then
nome = nome.Replace(".", "")
End If
nome = nome.Trim
n1 = DatiStampa.IndexOf("%%CreationDate:") + 15
n2 = (DatiStampa.IndexOf("%%For:")) - n1
dt = DatiStampa.Substring(n1, n2)
If dt.IndexOf("/") Or dt.IndexOf(":") Then
dt = dt.Replace("/", "-")
dt = dt.Replace(":", "-")
End If
dt = dt.Trim
nomefile = nome & "_" & dt
Return nomefile
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
stampante.AddPrinter()
End Sub
End Class