è un'applicazione server che si mette in ascolto sulla porta 515 ed attende la ricezione di dati!! quel thread, in particolare, riceve i dati inviati, li stampa in una casella di testo e converte i dati ricevuti in un file pdf!!
Deve essere sempre vera altrimenti la esegue solo una volta...
posto tutto il codice:
codice:
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Threading
Imports System.Net
Imports System.Threading.SynchronizationContext
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 DelegateAddText2(ByVal str2 As String)
Private MethodDelegateAddText2 As New DelegateAddText2(AddressOf AddText2)
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
Dim porta As Integer
porta = 515
Try
Listener.Start()
Dim serverThread As New Thread(New ThreadStart(AddressOf StartListen))
serverThread.Start()
Catch ex As Exception
End Try
End Sub
Private Sub StartListen()
While (True)
Try
Client = Listener.AcceptTcpClient()
'Inizializza lo stream
NetStr = Client.GetStream
' Socket Information
'Console.WriteLine("Client: " + clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString())
' Set Thread for each Web Browser Connection
Dim clientThread As New Thread(New ThreadStart(AddressOf Dati))
clientThread.Start()
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
'If Listener.Start Then
'clientSocket.Close()
' End If
End Try
End While
End Sub
Public Sub Dati()
While True
Try
Dim Bytes(Client.ReceiveBufferSize) As Byte
'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
NetStr.Read(Bytes, 0, Client.ReceiveBufferSize)
Dim AppoggioSwitch As String
Dim mess(0) As Byte
'Trasforma i bytes ricevuti In stringa
Dim dati As String
dati = System.Text.ASCIIEncoding.ASCII.GetString(Bytes)
If SwitchDati = False Then
AppoggioSwitch = Mid(dati, 1, 1)
If SwitchDati = False Then
mess(0) = CByte(0)
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(mess(0))
NetStr.Write(sendBytes, 0, sendBytes.Length)
End If
End If
'Visualizza il messaggio
Me.Invoke(MethodDelegateAddText, "[Ricezione dati]" & dati & vbLf & vbLf)
Me.Invoke(MethodDelegateAddText2, dati)
DatiStampa = Text2.Text
If dati.Contains("%%EOF") Then
mess(0) = CByte(0)
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(mess(0))
Client.Close()
Dim PsStart As Long
Dim PsEOF As Long
PsStart = InStr(1, DatiStampa, "%!PS")
PsEOF = InStr(1, DatiStampa, "%%EOF")
DatiStampa = Mid(DatiStampa, PsStart)
Listener.Stop()
Dim fs As FileStream
fs = New FileStream("c:\test2.ps", IO.FileMode.Append)
Dim s As New StreamWriter(fs)
s.WriteLine(DatiStampa)
s.Close()
Form2.Show()
Form2.Enabled = True
Form2.Refresh()
System.Windows.Forms.Application.DoEvents()
'Form2.Show()
gsapi.ConvertFile()
System.IO.File.Delete("c:\test2.ps")
System.Threading.Thread.Sleep(200)
DatiStampa = ""
DatiIntestazione = ""
SwitchDati = False
Listener.Start()
End If
dati = Nothing
If AppoggioSwitch = Chr(3) Then
SwitchDati = True
End If
Catch ex As Exception
End Try
End While
End Sub
End Class