-
Jun 3rd, 2010, 05:46 AM
#1
Thread Starter
New Member
VB.net and Winsock
Hi,
Basically my project is to create a client in vb.net to connect to an ip address and 10 ports on that ip address and wait for the data to come through.
I've already made the software in VB.net that uses the new system.net.sockets but it has problems when reading the data (I'm reading every second and doing a split on the data into a textbox and from there i'm reading the textbox and moving the data from the textbox into an array) but it reads some of the data and then reads the next bit of data a second later instead of waiting for all of the data like the old Winsock control used to do.
I'm wondering if anyone has managed to get the Winsock control working in VB.Net (with multi-connections) as the 'DataArrival' function would stop the problem that i'm having?
-
Jun 3rd, 2010, 12:01 PM
#2
-
Jun 6th, 2010, 02:23 PM
#3
Re: VB.net and Winsock
Why are you using Winsock in .net?
-
Jun 7th, 2010, 03:31 AM
#4
Thread Starter
New Member
Re: VB.net and Winsock
Hi guys thanks for the reply,
The reason why i want to use winsock in vb.net is because im using .net sockets now and i'm having some issues, i'm currently listening to 10 ports on an ip address and i want to wait for data to come through and then do something with that data.
The problem i'm having is that i'm running the script on a 1 second timer and sometimes i get the first part of the data from the com port and then a second later i get the rest...i can't allow this to happen. Do you have any ideas?....my code is below:
Code:
Dim clientSocket(10) As System.Net.Sockets.TcpClient 'set variable for tcp client connection
Dim serverStream As NetworkStream 'set variable for network stream of data
Dim RtnCmd As String 'return command recieved from data
Dim strLocalCopyReturnData(10) As String 'convert ascii characters into strings and return
Dim strReturnData As String 'data returned from network stream
Dim RtnAscii As String 'ascii code returned
Dim intNumberofPorts As Integer = 2 ' Number of sockets for test
#Region "Timers"
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timRefresh_Read_Ports.Tick
'******************************************************************************************************
'
'
' Subroutine : Timer1_Tick
'
' Description : Check for a connection on the ports, waits for data to be available then
' moves the data into a textbox. Then breakdowns the data to display and writes
' it into the SQL database
'
' Author : M Mills
'
' Date : 11 May 2010
'
' Version : 1.00
'
'
'******************************************************************************************************
Dim myCommand As SqlCommand
Dim ra As Integer
Dim db_strlocalcopydata As String = Nothing
Dim db_datetime As DateTime = Nothing
Dim db_milliseconds As Integer = Nothing
Dim db_crane As String = Nothing
Dim db_direction As String = Nothing
Dim db_string As String
Dim db_cmd As String
Dim db_x As String
Dim db_y As String
Dim db_f As String
Dim strSQL_Data As String
Dim StrTime As String = Nothing
ToolStripStatusLabel4.Text = "Last Update : " & Now.ToString
Dim myarray(10) As String
Dim temparray(10) As String
For INTX = 1 To intNumberofPorts
Dim sqlarray() As String = Nothing
If clientSocket(INTX).Connected Then
TimeSinceLastScan(INTX) = TimeSinceLastScan(INTX) + 1
While clientSocket(INTX).GetStream.DataAvailable
Dim serverStream As NetworkStream = clientSocket(INTX).GetStream()
Dim buffSize As Integer
serverStream.Flush()
Dim inStream(10024) As Byte
buffSize = clientSocket(INTX).ReceiveBufferSize
serverStream.Read(inStream, 0, buffSize)
Dim returndata As String = System.Text.Encoding.ASCII.GetString(inStream)
If clientSocket(INTX).GetStream.CanRead Then
TimeSinceLastScan(INTX) = 0
Select Case INTX
Case 1
TextBox1.Text = returndata
myarray = Split(TextBox1.Text, vbCrLf, -1, CompareMethod.Text)
Case 2
TextBox2.Text = returndata
myarray = Split(TextBox2.Text, vbCrLf, -1, CompareMethod.Text)
End Select
Dim counter2 As Integer
For counter2 = 0 To myarray.Count - 1
For counter = 1 To Len(myarray(counter2))
RtnAscii = Asc(Mid(myarray(counter2), counter, 1))
Select Case RtnAscii
Case "255"
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & "<LF>" & "<CR>" & " Crane " : RtnAscii = ""
Case "254"
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & "<LF>" & "<CR>" & " Host " : RtnAscii = ""
Case "253"
RtnAscii = ""
Case "3"
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <ETX>" : RtnAscii = ""
Case "5"
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <ENC>" : RtnAscii = ""
Case "6"
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <ACK>" : RtnAscii = ""
Case "10"
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <LF>" : RtnAscii = ""
Case "13"
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <CR>" : RtnAscii = ""
Case "4"
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <EOT>" : RtnAscii = ""
Case "2"
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <STX>" : RtnAscii = ""
Case Is >= 32
strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & Mid(myarray(0), counter, 1) 'returndata
End Select
Next counter
sqlarray(counter2) = StrTime & " " & strLocalCopyReturnData(INTX)
ListView1.Items(INTX - 1).SubItems.Item(4).Text = strLocalCopyReturnData(INTX)
strLocalCopyReturnData(INTX) = ""
Next counter2
If sqlarray IsNot Nothing Then
For i = 0 To sqlarray.Count - 1
If sqlarray(i) IsNot Nothing And (Mid(sqlarray(i), 26, 5) <> "<EOT>" Or Mid(sqlarray(i), 26, 5) <> "<ENC>") Then
Select Case Mid(sqlarray(i), 31, 1)
Case "G"
db_cmd = Mid(sqlarray(i), 31, 1)
db_x = Mid(sqlarray(i), 32, 3)
db_y = Mid(sqlarray(i), 35, 3)
db_f = Mid(sqlarray(i), 38, 3)
Case Else
db_cmd = Nothing
db_x = Nothing
db_y = Nothing
db_f = Nothing
End Select
If i = sqlarray.Count - 1 Then
ListView1.Items(INTX - 1).SubItems.Item(1).Text = DateTime.Now.ToString("dd/MM/yyyy H:mm:ss.fff")
ListView1.Items(INTX - 1).SubItems.Item(2).Text = db_crane
ListView1.Items(INTX - 1).SubItems.Item(3).Text = db_direction
ListView1.Items(INTX - 1).SubItems.Item(5).Text = db_cmd
ListView1.Items(INTX - 1).SubItems.Item(6).Text = db_x
ListView1.Items(INTX - 1).SubItems.Item(7).Text = db_y
ListView1.Items(INTX - 1).SubItems.Item(8).Text = db_f
ListView1.Refresh()
End If
db_strlocalcopydata = sqlarray(i)
If db_strlocalcopydata <> Nothing Then
{I then write the string to a SQL database}
End If
End If
Next i
End If
End If
End While
Next
myConnection.Close()
End Sub
#End Region
-
Jun 7th, 2010, 05:06 AM
#5
Re: VB.net and Winsock
It's a bit hard to follow your code, what with the ambiguous variable names and all, but if I understand you correctly...you say that you receive the first part of the data after at one time, and the other part the next time you read from the socket, and you want to read ALL at once?
-
Jun 7th, 2010, 05:30 AM
#6
Thread Starter
New Member
Re: VB.net and Winsock
Yea thats correct. I want to read everything thats come in and then move it to the textbox and from the textbox I can do whatever I want with it then. Is doing it on a 1 second timer the best way to do it or can I do it another way?
-
Jun 7th, 2010, 05:47 AM
#7
Re: VB.net and Winsock
Since the read operations are blocking you could very well spawn a thread for each socket that'll read data directly when it arrives at the socket. What you're doing now is polling to see if data has arrived, and might not be the best solution.
Anyway, what you're doing right now is just saying "here's a buffer with a size of X, fill it as much as possible with all data that has arrived". All data might not have arrived on the underlying socket and it'll then just give you what it has accumulated so far.
You should read the data in a more controlled manner. I assume you have a way to determine when you have reached the end of a message?
-
Jun 7th, 2010, 05:55 AM
#8
Thread Starter
New Member
Re: VB.net and Winsock
ok thanks, yea most of the data that arrives ends with <CR> <LF>, how would I go about doing it in a more controlled manner?...this is my first .net sockets project so i'm new to all this. How would I spawn a thread for each socket and wait for all the data to arrive?
-
Jun 15th, 2010, 02:44 AM
#9
New Member
Re: VB.net and Winsock
Hi, curiously I have de same problem, the server´s request arrive in two parts but in my case I cannot determinate the end of the request.
How can I solve it?
Last edited by newbiene; Jun 15th, 2010 at 06:09 AM.
-
Jun 24th, 2010, 10:26 AM
#10
Lively Member
Re: VB.net and Winsock
Can you use events instead of polling for data?
I wrote a book Visual Studio 2008 Programming
Amazon.com / Visual Studio 2008 Programming / By Jamie Plenderleith & Steve Bunn
-
Jun 27th, 2010, 04:25 AM
#11
Re: VB.net and Winsock
Originally Posted by Millsy
ok thanks, yea most of the data that arrives ends with <CR> <LF>, how would I go about doing it in a more controlled manner?...this is my first .net sockets project so i'm new to all this. How would I spawn a thread for each socket and wait for all the data to arrive?
You'd have two options as far as I can see; Read from the stream one byte at a time, and check for <CR> followed by <LF>, at which point you've received one full message. Alternatively, you can read big chunks of data from the stream at once, and perform the check for <CR><LF> in the data afterwards. It's all just several ways to skin a cat, and I'd go for the first option.
Originally Posted by newbiene
Hi, curiously I have de same problem, the serverĀ“s request arrive in two parts but in my case I cannot determinate the end of the request.
How can I solve it?
If the application protocol is really designed in a way such that there is no end of a request or a response, then you have to treat it as such. There is no way to know when a request or response, nor should you ever have to know.
Originally Posted by plender
Can you use events instead of polling for data?
Since the receive calls are all blocking, you don't need to poll for data. The method will call the blocking thread until it has received data, or an error occured.
Being as such, raising event when data has been received is no harder than raising an event directly after the call to the receive method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|