EDN Admin
Well-known member
Hi all. I am trying to create a socket class in my project to send data to ecr (electronic cash register).
Ecr comes with software that listens for commands on port 3535. Command are in forms of XML Files. It has only one catch, I must close the connection on socket, so the ecr would know when to process the command and respond. Since I only send one command
at a time, I need the simplest solution I can get.
How it works:
I create the socket and connect it to 192.168.1.67:3535
I start
<pre style=" client.BeginReceive<br/>[/code]
Then I send the command with
<pre style=" client.BeginSend
[/code]
And I do a
<pre style=" client.Disconnect(<span style="color:blue True)
[/code]
so I can get response from ECR.
And in 8 of 10 times it works just fine. The problem is this few times it doesnt work.
At that time I cant receive anything because it reports that the client is not connected in ReceiveCallback but it still manages to send the data to ECR just fine. The bill comes out on the ECR, but I get no answer about what happened.
What might be the problem ?
I call the class like this:
<pre style=" <span style="color:blue Dim dre <span style="color:blue As <span style="color:blue New <span style="color:#2b91af HCPSocket
Result2 = dre.Otkucaj(Command1, <span style="color:#a31515 "192.168.1.67")
<span style="color:#2b91af Debug.Print(Result2)
dre = <span style="color:blue Nothing[/code]
End the class looks like this:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System
<span style="color:Blue; Imports System.Net
<span style="color:Blue; Imports System.Net.Sockets
<span style="color:Blue; Imports System.Threading
<span style="color:Blue; Imports System.Text
<span style="color:Blue; Public <span style="color:Blue; Class HCPSocket
<span style="color:Blue; Dim clientSocket <span style="color:Blue; As <span style="color:Blue; New System.Net.Sockets.TcpClient()
<span style="color:Blue; Dim serverStream <span style="color:Blue; As NetworkStream
<span style="color:Blue; Dim PrviPut <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Dim Brojac <span style="color:Blue; As <span style="color:Blue; Integer = 0
<span style="color:Blue; #Region <span style="color:#A31515; "Dimensions"
<span style="color:Green; The port number for the remote device.
<span style="color:Blue; Private <span style="color:Blue; Const port <span style="color:Blue; As <span style="color:Blue; Integer = 3535 <span style="color:Green; 2425
<span style="color:Blue; Dim ipaddress <span style="color:Blue; As IPAddress = <span style="color:Blue; Nothing
<span style="color:Green; ManualResetEvent instances signal completion.
<span style="color:Blue; Private <span style="color:Blue; Shared connectDone <span style="color:Blue; As <span style="color:Blue; New ManualResetEvent(<span style="color:Blue; False)
<span style="color:Blue; Private <span style="color:Blue; Shared sendDone <span style="color:Blue; As <span style="color:Blue; New ManualResetEvent(<span style="color:Blue; False)
<span style="color:Blue; Private <span style="color:Blue; Shared receiveDone <span style="color:Blue; As <span style="color:Blue; New ManualResetEvent(<span style="color:Blue; False)
<span style="color:Blue; Private <span style="color:Blue; Shared DisconnectDone <span style="color:Blue; As <span style="color:Blue; New ManualResetEvent(<span style="color:Blue; False)
<span style="color:Green; Create a TCP/IP socket.
<span style="color:Blue; Dim client <span style="color:Blue; As <span style="color:Blue; New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
<span style="color:Green; The response from the remote device.
<span style="color:Blue; Private <span style="color:Blue; Shared response <span style="color:Blue; As <span style="color:Blue; String = <span style="color:Blue; String.Empty
<span style="color:Green; Delegate dimensions
<span style="color:Blue; Private <span style="color:Blue; Delegate <span style="color:Blue; Sub ShowReceiveDataDelegate(<span style="color:Blue; ByVal Data <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Private <span style="color:Blue; Delegate <span style="color:Blue; Sub DisconnectResponseDataDelegate()
<span style="color:Blue; Dim Rezultat <span style="color:Blue; As <span style="color:Blue; String = <span style="color:#A31515; "PG-ND-Server nije dostupan"
<span style="color:Blue; Public <span style="color:Blue; Shared Greska <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; #End Region
<span style=<span style="color:#A31515; "white-spacere"> <span style="color:Green; This is the data that is sent to ECR
<span style="color:Blue; Private <span style="color:Blue; Function Drekavac() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim StrXML <span style="color:Blue; As <span style="color:Blue; String = <span style="color:#A31515; ""
StrXML = StrXML & <span style="color:#A31515; "<?xml version=1.0 encoding=UTF-8 standalone=yes?>"
StrXML = StrXML & <span style="color:#A31515; "<NEFISKALNI_TEKST>"
StrXML = StrXML & vbTab & <span style="color:#A31515; "<DATA TXT=123123123/>"
StrXML = StrXML & <span style="color:#A31515; "</NEFISKALNI_TEKST>"
Drekavac = StrXML
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Private <span style="color:Blue; Function Komanda() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim Tekst <span style="color:Blue; As <span style="color:Blue; String
Tekst = <span style="color:#A31515; "<?xml version=1.0 encoding=UTF-8 standalone=yes?>" & vbCrLf
Tekst = Tekst & <span style="color:#A31515; "<KOMANDE>" & vbCrLf
Tekst = Tekst & vbTab & <span style="color:#A31515; "<DATA CMD=PROVERI_FISKALNI_DAN/>" & vbCrLf
Tekst = Tekst & <span style="color:#A31515; "</KOMANDE>"
Komanda = Tekst
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Public <span style="color:Blue; Function Otkucaj(<span style="color:Blue; ByVal Tekst <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal IPAdresa <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Try
Debug.Print(Brojac + 1)
[/code]
<div style="color:Black;background-color:White; <pre>
<br/>
[/code]
<div style="color:Black;background-color:White; <pre>
<span style=<span style="color:#A31515; "white-spacere"> <span style="color:Green; Test if the ecr is available
<span style="color:Blue; If <span style="color:Blue; Not <span style="color:Blue; My.Computer.Network.Ping(IPAdresa, 1000) <span style="color:Blue; Then
Otkucaj = <span style="color:#A31515; "GR-ND-Server kase nije dostupan"
<span style="color:Blue; Exit <span style="color:Blue; Function
<span style="color:Blue; End <span style="color:Blue; If
PrviPut = <span style="color:Blue; True
client = <span style="color:Blue; Nothing
client = <span style="color:Blue; New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Main(IPAdresa)
<span style="color:Green; Thread.Sleep(500)
Send(client, Komanda) <span style="color:Green; TextBox2.Text + " <EOF>")
sendDone.WaitOne()
client.Disconnect(<span style="color:Blue; True)
<span style="color:Green; DisconnectDone.WaitOne()
client.Close()
client = <span style="color:Blue; Nothing
client = <span style="color:Blue; New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Main(IPAdresa)
Thread.Sleep(500)
Send(client, Tekst) <span style="color:Green; TextBox2.Text + " <EOF>")
sendDone.WaitOne()
client.Disconnect(<span style="color:Blue; True)
<span style="color:Green; DisconnectDone.WaitOne()
<span style="color:Green; client.Close()
<span style="color:Blue; If client.Connected <span style="color:Blue; Then
<span style="color:Green; Release the socket.
client.Shutdown(SocketShutdown.Both)
client.Close()
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If Greska = <span style="color:Blue; True <span style="color:Blue; Then
Otkucaj = Rezultat
<span style="color:Blue; Else
<span style="color:Blue; If Rezultat.Length > 5 <span style="color:Blue; Then
Otkucaj = ProvjeriOdgovor(Rezultat)
<span style="color:Blue; Else
Otkucaj = <span style="color:#A31515; "GR-P-Provjera"
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Otkucaj = <span style="color:#A31515; "GR-GR-" & ex.Message
<span style="color:Green; Debug.Print(ex.Message)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Green; Otkucaj = Rezultat
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Public <span style="color:Blue; Function ProvjeriOdgovor(<span style="color:Blue; ByVal Tekst <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; If Left(Tekst, 1) = <span style="color:#A31515; "<" <span style="color:Blue; Then
<span style="color:Blue; Dim doc <span style="color:Blue; As <span style="color:Blue; New System.Xml.XmlDocument
doc.LoadXml(Tekst)
<span style="color:Blue; Dim Odgovor <span style="color:Blue; As <span style="color:Blue; String = <span style="color:#A31515; "GR-ND-NEMA ODGOVORA"
<span style="color:Blue; For <span style="color:Blue; Each n <span style="color:Blue; As Xml.XmlElement <span style="color:Blue; In doc.GetElementsByTagName(<span style="color:#A31515; "ERROR")
Odgovor = <span style="color:#A31515; "OK-" & n.Attributes(<span style="color:#A31515; "NUMBER").Value.ToString & <span style="color:#A31515; "-" & n.Attributes(<span style="color:#A31515; "DESCRIPTION").Value.ToString
<span style="color:Blue; Next
ProvjeriOdgovor = Odgovor
<span style="color:Blue; Else
ProvjeriOdgovor = Tekst
<span style="color:Blue; Exit <span style="color:Blue; Function
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Private <span style="color:Blue; Sub Main(<span style="color:Blue; ByVal IpAdresa <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Green; Establish the remote endpoint for the socket.
<span style="color:Blue; Dim remoteEP <span style="color:Blue; As <span style="color:Blue; New IPEndPoint(System.Net.IPAddress.Parse(IpAdresa), port)
<span style="color:Green; Connect to the remote endpoint.
client.BeginConnect(remoteEP, <span style="color:Blue; New AsyncCallback(<span style="color:Blue; AddressOf ConnectCallback), client)
<span style="color:Green; Wait for connect.
connectDone.WaitOne()
<span style="color:Green; If client.Connected = True Then
<span style="color:Blue; Try
<span style="color:Blue; If client.Poll(1, SelectMode.SelectWrite) <span style="color:Blue; Then
<span style="color:Green; Set socket in Receive mode
Receive(client)
Debug.Print(<span style="color:#A31515; "Konektovan")
<span style="color:Green; Button1.Text = "Disconnect"
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Server is not responding
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Server kase ne odgovara"
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Green; Else
<span style="color:Green; End If
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; Main
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Sub ConnectCallback(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Green; Retrieve the socket from the state object.
<span style="color:Blue; Dim sckclient <span style="color:Blue; As Socket = <span style="color:Blue; CType(ar.AsyncState, Socket)
<span style="color:Blue; Try
<span style="color:Green; Complete the connection.
sckclient.EndConnect(ar)
<span style="color:Blue; Catch
sckclient.Close()
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Green; Signal that the connection has been made.
connectDone.<span style="color:Blue; Set()
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; ConnectCallback
<span style="color:Blue; Private <span style="color:Blue; Sub Receive(<span style="color:Blue; ByVal client <span style="color:Blue; As Socket)
<span style="color:Blue; Try
<span style="color:Green; Create the state object.
<span style="color:Blue; Dim state <span style="color:Blue; As <span style="color:Blue; New StateObject2
state.workSocket = client
<span style="color:Blue; Do <span style="color:Blue; While <span style="color:Blue; Not client.Connected
Debug.Print(<span style="color:#A31515; "Cekam konektovanje")
<span style="color:Green; wait
<span style="color:Blue; Loop
<span style="color:Green; Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject2.BufferSize, 0, <span style="color:Blue; New AsyncCallback(<span style="color:Blue; AddressOf ReceiveCallback), state)
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Receive " & ex.Message
<span style="color:Green; Debug.Print("Receive - " & ex.Message)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; Receive
<span style="color:Blue; #Region <span style="color:#A31515; "Receive Data"
<span style="color:Blue; Private <span style="color:Blue; Sub ReceiveCallback(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Blue; Try
<span style="color:Green; Debug.Print("Pocinjem receive ")
<span style="color:Green; Retrieve the state object and the client socket
<span style="color:Green; from the asynchronous state object.
<span style="color:Blue; Dim state <span style="color:Blue; As StateObject2 = <span style="color:Blue; CType(ar.AsyncState, StateObject2)
<span style="color:Blue; Dim client <span style="color:Blue; As Socket = state.workSocket
<span style="color:Green; client.EndReceive(ar)
<span style="color:Blue; If <span style="color:Blue; Not client.Connected <span style="color:Blue; Then
<br/>
[/code]
<div style="color:Black;background-color:White; <pre>
<span style=<span style="color:#A31515; "white-spacere"> <span style="color:Green; This is where I get the error Not connected
[/code]
<div style="color:Black;background-color:White; <pre>
Debug.Print(<span style="color:#A31515; "Nije konektovan ")
<span style="color:Green; GoTo 100
receiveDone.<span style="color:Blue; Set()
<span style="color:Green; Dim dlg As New DisconnectResponseDataDelegate(AddressOf DisconnectResponse)
<span style="color:Green; Me.Invoke(dlg)
<span style="color:Green; dlg()
<span style="color:Blue; Exit <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Green; Debug.Print("Citam ")
<span style="color:Green; Read data from the remote device.
<span style="color:Blue; Dim bytesRead <span style="color:Blue; As <span style="color:Blue; Integer = client.EndReceive(ar)
<span style="color:Blue; If bytesRead > 0 <span style="color:Blue; Then
<span style="color:Green; Store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
Rezultat = state.sb.ToString()
<span style="color:Green; Put it in a response.
<span style="color:Blue; If state.sb.Length > 2 <span style="color:Blue; Then
response = state.sb.ToString()
<span style="color:Blue; Else
Rezultat = <span style="color:#A31515; "GR-GR-1Server nije vratio odgovor"
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Else
<span style="color:Green; Server Has disconnected
client.Close()
receiveDone.<span style="color:Blue; Set()
<span style="color:Green; Dim dlg As New DisconnectResponseDataDelegate(AddressOf DisconnectResponse)
<span style="color:Green; Me.Invoke(dlg)
<span style="color:Green; dlg()
Debug.Print(<span style="color:#A31515; "nije konektovan ")
<span style="color:Blue; Exit <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; If
response = <span style="color:Blue; Nothing
Array.Clear(state.buffer, 0, bytesRead)
state.sb.Remove(0, bytesRead)
<span style="color:Green; Signal that all bytes have been received.
receiveDone.<span style="color:Blue; Set()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Primanje " & ex.Message
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; ReceiveCallback
<span style="color:Blue; #End Region
<span style="color:Blue; #Region <span style="color:#A31515; "Messages and invokes to Form"
<span style="color:Blue; Private <span style="color:Blue; Sub DisconnectResponse()
client = <span style="color:Blue; New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; Change the Connect button Text
<span style="color:Blue; #End Region
<span style="color:Blue; #Region <span style="color:#A31515; "Send Data"
<span style="color:Blue; Private <span style="color:Blue; Sub Send(<span style="color:Blue; ByVal client <span style="color:Blue; As Socket, <span style="color:Blue; ByVal data <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Green; Convert the string data to byte data using ASCII encoding.
<span style="color:Blue; Try
<span style="color:Blue; Dim byteData <span style="color:Blue; As <span style="color:Blue; Byte() = Encoding.ASCII.GetBytes(data)
<span style="color:Green; Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, <span style="color:Blue; New AsyncCallback(<span style="color:Blue; AddressOf SendCallback), client)
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Slanje " & ex.Message
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; Send
<span style="color:Blue; Private <span style="color:Blue; Sub SendCallback(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Green; Retrieve the socket from the state object.
<span style="color:Blue; Try
<span style="color:Blue; Dim client <span style="color:Blue; As Socket = <span style="color:Blue; CType(ar.AsyncState, Socket)
<span style="color:Green; Complete sending the data to the remote device.
<span style="color:Blue; Dim bytesSent <span style="color:Blue; As <span style="color:Blue; Integer = client.EndSend(ar)
<span style="color:Green; Signal that all bytes have been sent.
sendDone.<span style="color:Blue; Set()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Poslato " & ex.Message
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; SendCallback
<span style="color:Blue; #End Region
<span style="color:Blue; #Region <span style="color:#A31515; "Disconnect call back"
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Sub DisconnectCallback(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Green; Retrieve the socket from the state object
<span style="color:Blue; Dim client <span style="color:Blue; As Socket = <span style="color:Blue; CType(ar.AsyncState, Socket)
<span style="color:Green; End the disconnect
client.EndDisconnect(ar)
<span style="color:Green; Signal the disconnect done
DisconnectDone.<span style="color:Blue; Set()
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; DisconnectCallBack
<span style="color:Blue; #End Region
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; #Region <span style="color:#A31515; "State Object Calss"
<span style="color:Blue; Public <span style="color:Blue; Class StateObject2
<span style="color:Green; Client socket.
<span style="color:Blue; Public workSocket <span style="color:Blue; As Socket = <span style="color:Blue; Nothing
<span style="color:Green; Size of receive buffer.
<span style="color:Blue; Public <span style="color:Blue; Const BufferSize <span style="color:Blue; As <span style="color:Blue; Integer = 256
<span style="color:Green; Receive buffer.
<span style="color:Blue; Public buffer(BufferSize) <span style="color:Blue; As <span style="color:Blue; Byte
<span style="color:Green; Received data string.
<span style="color:Blue; Public sb <span style="color:Blue; As <span style="color:Blue; New StringBuilder
<span style="color:Blue; End <span style="color:Blue; Class <span style="color:Green; StateObject
<span style="color:Blue; #End Region
[/code]
<br/>
<br/>
What can I do ?
View the full article
Ecr comes with software that listens for commands on port 3535. Command are in forms of XML Files. It has only one catch, I must close the connection on socket, so the ecr would know when to process the command and respond. Since I only send one command
at a time, I need the simplest solution I can get.
How it works:
I create the socket and connect it to 192.168.1.67:3535
I start
<pre style=" client.BeginReceive<br/>[/code]
Then I send the command with
<pre style=" client.BeginSend
[/code]
And I do a
<pre style=" client.Disconnect(<span style="color:blue True)
[/code]
so I can get response from ECR.
And in 8 of 10 times it works just fine. The problem is this few times it doesnt work.
At that time I cant receive anything because it reports that the client is not connected in ReceiveCallback but it still manages to send the data to ECR just fine. The bill comes out on the ECR, but I get no answer about what happened.
What might be the problem ?
I call the class like this:
<pre style=" <span style="color:blue Dim dre <span style="color:blue As <span style="color:blue New <span style="color:#2b91af HCPSocket
Result2 = dre.Otkucaj(Command1, <span style="color:#a31515 "192.168.1.67")
<span style="color:#2b91af Debug.Print(Result2)
dre = <span style="color:blue Nothing[/code]
End the class looks like this:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System
<span style="color:Blue; Imports System.Net
<span style="color:Blue; Imports System.Net.Sockets
<span style="color:Blue; Imports System.Threading
<span style="color:Blue; Imports System.Text
<span style="color:Blue; Public <span style="color:Blue; Class HCPSocket
<span style="color:Blue; Dim clientSocket <span style="color:Blue; As <span style="color:Blue; New System.Net.Sockets.TcpClient()
<span style="color:Blue; Dim serverStream <span style="color:Blue; As NetworkStream
<span style="color:Blue; Dim PrviPut <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Dim Brojac <span style="color:Blue; As <span style="color:Blue; Integer = 0
<span style="color:Blue; #Region <span style="color:#A31515; "Dimensions"
<span style="color:Green; The port number for the remote device.
<span style="color:Blue; Private <span style="color:Blue; Const port <span style="color:Blue; As <span style="color:Blue; Integer = 3535 <span style="color:Green; 2425
<span style="color:Blue; Dim ipaddress <span style="color:Blue; As IPAddress = <span style="color:Blue; Nothing
<span style="color:Green; ManualResetEvent instances signal completion.
<span style="color:Blue; Private <span style="color:Blue; Shared connectDone <span style="color:Blue; As <span style="color:Blue; New ManualResetEvent(<span style="color:Blue; False)
<span style="color:Blue; Private <span style="color:Blue; Shared sendDone <span style="color:Blue; As <span style="color:Blue; New ManualResetEvent(<span style="color:Blue; False)
<span style="color:Blue; Private <span style="color:Blue; Shared receiveDone <span style="color:Blue; As <span style="color:Blue; New ManualResetEvent(<span style="color:Blue; False)
<span style="color:Blue; Private <span style="color:Blue; Shared DisconnectDone <span style="color:Blue; As <span style="color:Blue; New ManualResetEvent(<span style="color:Blue; False)
<span style="color:Green; Create a TCP/IP socket.
<span style="color:Blue; Dim client <span style="color:Blue; As <span style="color:Blue; New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
<span style="color:Green; The response from the remote device.
<span style="color:Blue; Private <span style="color:Blue; Shared response <span style="color:Blue; As <span style="color:Blue; String = <span style="color:Blue; String.Empty
<span style="color:Green; Delegate dimensions
<span style="color:Blue; Private <span style="color:Blue; Delegate <span style="color:Blue; Sub ShowReceiveDataDelegate(<span style="color:Blue; ByVal Data <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Private <span style="color:Blue; Delegate <span style="color:Blue; Sub DisconnectResponseDataDelegate()
<span style="color:Blue; Dim Rezultat <span style="color:Blue; As <span style="color:Blue; String = <span style="color:#A31515; "PG-ND-Server nije dostupan"
<span style="color:Blue; Public <span style="color:Blue; Shared Greska <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; #End Region
<span style=<span style="color:#A31515; "white-spacere"> <span style="color:Green; This is the data that is sent to ECR
<span style="color:Blue; Private <span style="color:Blue; Function Drekavac() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim StrXML <span style="color:Blue; As <span style="color:Blue; String = <span style="color:#A31515; ""
StrXML = StrXML & <span style="color:#A31515; "<?xml version=1.0 encoding=UTF-8 standalone=yes?>"
StrXML = StrXML & <span style="color:#A31515; "<NEFISKALNI_TEKST>"
StrXML = StrXML & vbTab & <span style="color:#A31515; "<DATA TXT=123123123/>"
StrXML = StrXML & <span style="color:#A31515; "</NEFISKALNI_TEKST>"
Drekavac = StrXML
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Private <span style="color:Blue; Function Komanda() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim Tekst <span style="color:Blue; As <span style="color:Blue; String
Tekst = <span style="color:#A31515; "<?xml version=1.0 encoding=UTF-8 standalone=yes?>" & vbCrLf
Tekst = Tekst & <span style="color:#A31515; "<KOMANDE>" & vbCrLf
Tekst = Tekst & vbTab & <span style="color:#A31515; "<DATA CMD=PROVERI_FISKALNI_DAN/>" & vbCrLf
Tekst = Tekst & <span style="color:#A31515; "</KOMANDE>"
Komanda = Tekst
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Public <span style="color:Blue; Function Otkucaj(<span style="color:Blue; ByVal Tekst <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal IPAdresa <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Try
Debug.Print(Brojac + 1)
[/code]
<div style="color:Black;background-color:White; <pre>
<br/>
[/code]
<div style="color:Black;background-color:White; <pre>
<span style=<span style="color:#A31515; "white-spacere"> <span style="color:Green; Test if the ecr is available
<span style="color:Blue; If <span style="color:Blue; Not <span style="color:Blue; My.Computer.Network.Ping(IPAdresa, 1000) <span style="color:Blue; Then
Otkucaj = <span style="color:#A31515; "GR-ND-Server kase nije dostupan"
<span style="color:Blue; Exit <span style="color:Blue; Function
<span style="color:Blue; End <span style="color:Blue; If
PrviPut = <span style="color:Blue; True
client = <span style="color:Blue; Nothing
client = <span style="color:Blue; New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Main(IPAdresa)
<span style="color:Green; Thread.Sleep(500)
Send(client, Komanda) <span style="color:Green; TextBox2.Text + " <EOF>")
sendDone.WaitOne()
client.Disconnect(<span style="color:Blue; True)
<span style="color:Green; DisconnectDone.WaitOne()
client.Close()
client = <span style="color:Blue; Nothing
client = <span style="color:Blue; New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Main(IPAdresa)
Thread.Sleep(500)
Send(client, Tekst) <span style="color:Green; TextBox2.Text + " <EOF>")
sendDone.WaitOne()
client.Disconnect(<span style="color:Blue; True)
<span style="color:Green; DisconnectDone.WaitOne()
<span style="color:Green; client.Close()
<span style="color:Blue; If client.Connected <span style="color:Blue; Then
<span style="color:Green; Release the socket.
client.Shutdown(SocketShutdown.Both)
client.Close()
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If Greska = <span style="color:Blue; True <span style="color:Blue; Then
Otkucaj = Rezultat
<span style="color:Blue; Else
<span style="color:Blue; If Rezultat.Length > 5 <span style="color:Blue; Then
Otkucaj = ProvjeriOdgovor(Rezultat)
<span style="color:Blue; Else
Otkucaj = <span style="color:#A31515; "GR-P-Provjera"
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Otkucaj = <span style="color:#A31515; "GR-GR-" & ex.Message
<span style="color:Green; Debug.Print(ex.Message)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Green; Otkucaj = Rezultat
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Public <span style="color:Blue; Function ProvjeriOdgovor(<span style="color:Blue; ByVal Tekst <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; If Left(Tekst, 1) = <span style="color:#A31515; "<" <span style="color:Blue; Then
<span style="color:Blue; Dim doc <span style="color:Blue; As <span style="color:Blue; New System.Xml.XmlDocument
doc.LoadXml(Tekst)
<span style="color:Blue; Dim Odgovor <span style="color:Blue; As <span style="color:Blue; String = <span style="color:#A31515; "GR-ND-NEMA ODGOVORA"
<span style="color:Blue; For <span style="color:Blue; Each n <span style="color:Blue; As Xml.XmlElement <span style="color:Blue; In doc.GetElementsByTagName(<span style="color:#A31515; "ERROR")
Odgovor = <span style="color:#A31515; "OK-" & n.Attributes(<span style="color:#A31515; "NUMBER").Value.ToString & <span style="color:#A31515; "-" & n.Attributes(<span style="color:#A31515; "DESCRIPTION").Value.ToString
<span style="color:Blue; Next
ProvjeriOdgovor = Odgovor
<span style="color:Blue; Else
ProvjeriOdgovor = Tekst
<span style="color:Blue; Exit <span style="color:Blue; Function
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Private <span style="color:Blue; Sub Main(<span style="color:Blue; ByVal IpAdresa <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Green; Establish the remote endpoint for the socket.
<span style="color:Blue; Dim remoteEP <span style="color:Blue; As <span style="color:Blue; New IPEndPoint(System.Net.IPAddress.Parse(IpAdresa), port)
<span style="color:Green; Connect to the remote endpoint.
client.BeginConnect(remoteEP, <span style="color:Blue; New AsyncCallback(<span style="color:Blue; AddressOf ConnectCallback), client)
<span style="color:Green; Wait for connect.
connectDone.WaitOne()
<span style="color:Green; If client.Connected = True Then
<span style="color:Blue; Try
<span style="color:Blue; If client.Poll(1, SelectMode.SelectWrite) <span style="color:Blue; Then
<span style="color:Green; Set socket in Receive mode
Receive(client)
Debug.Print(<span style="color:#A31515; "Konektovan")
<span style="color:Green; Button1.Text = "Disconnect"
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Server is not responding
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Server kase ne odgovara"
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Green; Else
<span style="color:Green; End If
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; Main
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Sub ConnectCallback(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Green; Retrieve the socket from the state object.
<span style="color:Blue; Dim sckclient <span style="color:Blue; As Socket = <span style="color:Blue; CType(ar.AsyncState, Socket)
<span style="color:Blue; Try
<span style="color:Green; Complete the connection.
sckclient.EndConnect(ar)
<span style="color:Blue; Catch
sckclient.Close()
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Green; Signal that the connection has been made.
connectDone.<span style="color:Blue; Set()
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; ConnectCallback
<span style="color:Blue; Private <span style="color:Blue; Sub Receive(<span style="color:Blue; ByVal client <span style="color:Blue; As Socket)
<span style="color:Blue; Try
<span style="color:Green; Create the state object.
<span style="color:Blue; Dim state <span style="color:Blue; As <span style="color:Blue; New StateObject2
state.workSocket = client
<span style="color:Blue; Do <span style="color:Blue; While <span style="color:Blue; Not client.Connected
Debug.Print(<span style="color:#A31515; "Cekam konektovanje")
<span style="color:Green; wait
<span style="color:Blue; Loop
<span style="color:Green; Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject2.BufferSize, 0, <span style="color:Blue; New AsyncCallback(<span style="color:Blue; AddressOf ReceiveCallback), state)
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Receive " & ex.Message
<span style="color:Green; Debug.Print("Receive - " & ex.Message)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; Receive
<span style="color:Blue; #Region <span style="color:#A31515; "Receive Data"
<span style="color:Blue; Private <span style="color:Blue; Sub ReceiveCallback(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Blue; Try
<span style="color:Green; Debug.Print("Pocinjem receive ")
<span style="color:Green; Retrieve the state object and the client socket
<span style="color:Green; from the asynchronous state object.
<span style="color:Blue; Dim state <span style="color:Blue; As StateObject2 = <span style="color:Blue; CType(ar.AsyncState, StateObject2)
<span style="color:Blue; Dim client <span style="color:Blue; As Socket = state.workSocket
<span style="color:Green; client.EndReceive(ar)
<span style="color:Blue; If <span style="color:Blue; Not client.Connected <span style="color:Blue; Then
<br/>
[/code]
<div style="color:Black;background-color:White; <pre>
<span style=<span style="color:#A31515; "white-spacere"> <span style="color:Green; This is where I get the error Not connected
[/code]
<div style="color:Black;background-color:White; <pre>
Debug.Print(<span style="color:#A31515; "Nije konektovan ")
<span style="color:Green; GoTo 100
receiveDone.<span style="color:Blue; Set()
<span style="color:Green; Dim dlg As New DisconnectResponseDataDelegate(AddressOf DisconnectResponse)
<span style="color:Green; Me.Invoke(dlg)
<span style="color:Green; dlg()
<span style="color:Blue; Exit <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Green; Debug.Print("Citam ")
<span style="color:Green; Read data from the remote device.
<span style="color:Blue; Dim bytesRead <span style="color:Blue; As <span style="color:Blue; Integer = client.EndReceive(ar)
<span style="color:Blue; If bytesRead > 0 <span style="color:Blue; Then
<span style="color:Green; Store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
Rezultat = state.sb.ToString()
<span style="color:Green; Put it in a response.
<span style="color:Blue; If state.sb.Length > 2 <span style="color:Blue; Then
response = state.sb.ToString()
<span style="color:Blue; Else
Rezultat = <span style="color:#A31515; "GR-GR-1Server nije vratio odgovor"
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Else
<span style="color:Green; Server Has disconnected
client.Close()
receiveDone.<span style="color:Blue; Set()
<span style="color:Green; Dim dlg As New DisconnectResponseDataDelegate(AddressOf DisconnectResponse)
<span style="color:Green; Me.Invoke(dlg)
<span style="color:Green; dlg()
Debug.Print(<span style="color:#A31515; "nije konektovan ")
<span style="color:Blue; Exit <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; If
response = <span style="color:Blue; Nothing
Array.Clear(state.buffer, 0, bytesRead)
state.sb.Remove(0, bytesRead)
<span style="color:Green; Signal that all bytes have been received.
receiveDone.<span style="color:Blue; Set()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Primanje " & ex.Message
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; ReceiveCallback
<span style="color:Blue; #End Region
<span style="color:Blue; #Region <span style="color:#A31515; "Messages and invokes to Form"
<span style="color:Blue; Private <span style="color:Blue; Sub DisconnectResponse()
client = <span style="color:Blue; New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; Change the Connect button Text
<span style="color:Blue; #End Region
<span style="color:Blue; #Region <span style="color:#A31515; "Send Data"
<span style="color:Blue; Private <span style="color:Blue; Sub Send(<span style="color:Blue; ByVal client <span style="color:Blue; As Socket, <span style="color:Blue; ByVal data <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Green; Convert the string data to byte data using ASCII encoding.
<span style="color:Blue; Try
<span style="color:Blue; Dim byteData <span style="color:Blue; As <span style="color:Blue; Byte() = Encoding.ASCII.GetBytes(data)
<span style="color:Green; Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, <span style="color:Blue; New AsyncCallback(<span style="color:Blue; AddressOf SendCallback), client)
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Slanje " & ex.Message
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; Send
<span style="color:Blue; Private <span style="color:Blue; Sub SendCallback(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Green; Retrieve the socket from the state object.
<span style="color:Blue; Try
<span style="color:Blue; Dim client <span style="color:Blue; As Socket = <span style="color:Blue; CType(ar.AsyncState, Socket)
<span style="color:Green; Complete sending the data to the remote device.
<span style="color:Blue; Dim bytesSent <span style="color:Blue; As <span style="color:Blue; Integer = client.EndSend(ar)
<span style="color:Green; Signal that all bytes have been sent.
sendDone.<span style="color:Blue; Set()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Greska = <span style="color:Blue; True
Rezultat = <span style="color:#A31515; "GR-ND-Poslato " & ex.Message
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; SendCallback
<span style="color:Blue; #End Region
<span style="color:Blue; #Region <span style="color:#A31515; "Disconnect call back"
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Sub DisconnectCallback(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Green; Retrieve the socket from the state object
<span style="color:Blue; Dim client <span style="color:Blue; As Socket = <span style="color:Blue; CType(ar.AsyncState, Socket)
<span style="color:Green; End the disconnect
client.EndDisconnect(ar)
<span style="color:Green; Signal the disconnect done
DisconnectDone.<span style="color:Blue; Set()
<span style="color:Blue; End <span style="color:Blue; Sub <span style="color:Green; DisconnectCallBack
<span style="color:Blue; #End Region
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; #Region <span style="color:#A31515; "State Object Calss"
<span style="color:Blue; Public <span style="color:Blue; Class StateObject2
<span style="color:Green; Client socket.
<span style="color:Blue; Public workSocket <span style="color:Blue; As Socket = <span style="color:Blue; Nothing
<span style="color:Green; Size of receive buffer.
<span style="color:Blue; Public <span style="color:Blue; Const BufferSize <span style="color:Blue; As <span style="color:Blue; Integer = 256
<span style="color:Green; Receive buffer.
<span style="color:Blue; Public buffer(BufferSize) <span style="color:Blue; As <span style="color:Blue; Byte
<span style="color:Green; Received data string.
<span style="color:Blue; Public sb <span style="color:Blue; As <span style="color:Blue; New StringBuilder
<span style="color:Blue; End <span style="color:Blue; Class <span style="color:Green; StateObject
<span style="color:Blue; #End Region
[/code]
<br/>
<br/>
What can I do ?
View the full article