![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2006
Posts: 10
Rep Power: 0
![]() |
Asynchronous Network communication
I am trying to open 2 tcp connections to a server both asynchronous connections. My problem is when i setup the async call back only one of them works and the other gives me an error. Is there an easy way to setup two async call backs that do not conflict with one another.
Surferboy Private Sub CommandRecieveCallBack(ByVal arone As IAsyncResult)
Try
If CommandSocket.Connected = True Then
Dim bytes() As Byte = CType(arone.AsyncState, Byte())
Dim numbytes As Int32 = CommandSocket.EndReceive(arone)
If numbytes = 0 Then
CommandSocket.Shutdown(SocketShutdown.Both)
CommandSocket.Close()
Dim dlgone As New NoParamsDelegate(AddressOf DisConnectedUI)
Me.Invoke(dlgone)
Else
Dim Recv As String = ASCII.GetString(bytes, 0, numbytes)
' clear the array
Array.Clear(bytes, 0, bytes.Length)
' show changes in UI
Dim dlgone As New OneStringDelegate(AddressOf DisplayRecieveData)
Dim args() As Object = {Recv}
Me.Invoke(dlgone, args)
' Begin Recieving again
CommandSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf CommandRecieveCallBack, bytes)
End If
End If
Catch ex As Exception
System.Console.WriteLine("Disconnected")
End Try
End Sub
Private Sub CommandConnectCallBack(ByVal arone As IAsyncResult)
Try
CommandSocket.EndConnect(arone)
'SendCommandData()
Dim dlgone As New NoParamsDelegate(AddressOf ConnectedUI)
Me.Invoke(dlgone)
' begin recieve
' create buffer
Dim bytes(17000) As Byte
CommandSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf CommandRecieveCallBack, bytes)
Catch ex As SocketException
MessageBox.Show("The Server was either down or not responding. Please double check your server information that is under EtherNet->Configure.")
Console.WriteLine("Failed in ConnectCallBack")
End Try
End Sub |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Can you post the rest of the code??
__________________
JG-Webdesign |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2006
Posts: 10
Rep Power: 0
![]() |
Yes i can and here it is but there is kinda alot of it sorry for the long post
Imports System
Imports System.Net
Imports System.Net.Sockets
' Good examples of how to do this code can be found at
' www.franklins.net/video/sockets
Public Class Form1
'value in PulseWidth stored in Number of 50 ns clock cycles before the start of the first pulse
Public PulseWidth As Byte = 10
'value of StartDelay stored in number of 50 ns clock cycles before the end of the pulse
Public StartDelay As Byte = 10
'is the server connected?
Public Connected As Boolean = False
'To turn on debug change value to True
Public DEBUG As Boolean = True
Private ASCII As New System.Text.ASCIIEncoding()
'Declare Socket for connection to Command Server
Private CommandSocket As Socket
Private CommandAddress As String = "10.10.112.252"
Private CommandPort As String = "1963"
'Declare Socket For connection to Response Server
Private ResponseSocket As Socket
Private ResponseAddress As String = "10.10.112.252"
Private ResponsePort As String = "1945"
'GRAPH TESTS__________________________________________________________
'_____________________________________________________________________
'_____________________________________________________________________
Private xData(20) As Single
Private yData(20) As Single
''GRAPH TESTS__________________________________________________________
''_____________________________________________________________________
''_____________________________________________________________________
Private Sendtxt As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DisConnectedUI()
Dim sample As Double = 0
Dim counter As Integer = 0
xData(0) = 0
xData(1) = 0.1
xData(2) = 0.2
xData(3) = 0.3
xData(4) = 0.4
xData(5) = 0.5
xData(6) = 0.6
xData(7) = 0.7
xData(8) = 0.8
xData(9) = 0.9
xData(10) = 1
For counter = 0 To 10
yData(counter) = Math.Sin(counter)
Console.WriteLine(yData(counter))
Next
AxSST_RtChart1.ChnDspStyle = LortchtxLib.ChnDspStyles.RtChart_Lines
AxSST_RtChart1.ChartData(1, 10, yData, 0, xData, 0)
AxSST_RtChart1.PenColor = Color.DarkBlue
AxSST_RtChart1.Update()
End Sub
Private Sub CommandRecieveCallBack(ByVal arone As IAsyncResult)
Try
If CommandSocket.Connected = True Then
Dim bytes() As Byte = CType(arone.AsyncState, Byte())
Dim numbytes As Int32 = CommandSocket.EndReceive(arone)
If numbytes = 0 Then
CommandSocket.Shutdown(SocketShutdown.Both)
CommandSocket.Close()
Dim dlgone As New NoParamsDelegate(AddressOf DisConnectedUI)
Me.Invoke(dlgone)
Else
Dim Recv As String = ASCII.GetString(bytes, 0, numbytes)
' clear the array
Array.Clear(bytes, 0, bytes.Length)
' show changes in UI
Dim dlgone As New OneStringDelegate(AddressOf DisplayRecieveData)
Dim args() As Object = {Recv}
Me.Invoke(dlgone, args)
' Begin Recieving again
CommandSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf CommandRecieveCallBack, bytes)
End If
End If
Catch ex As Exception
System.Console.WriteLine("Disconnected")
End Try
End Sub
Private Sub ResponseRecieveCallBack(ByVal artwo As IAsyncResult)
Try
If ResponseSocket.Connected = True Then
Dim bytes() As Byte = CType(artwo.AsyncState, Byte())
Dim numbytes As Int32 = ResponseSocket.EndReceive(artwo)
If numbytes = 0 Then
ResponseSocket.Shutdown(SocketShutdown.Both)
ResponseSocket.Close()
Dim dlgtwo As New NoParamsDelegate(AddressOf DisConnectedUI)
Me.Invoke(dlgtwo)
Else
Dim Recv As String = ASCII.GetString(bytes, 0, numbytes)
' clear the array
Array.Clear(bytes, 0, bytes.Length)
' show changes in UI
Dim dlgtwo As New OneStringDelegate(AddressOf DisplayRecieveData)
Dim args() As Object = {Recv}
Me.Invoke(dlgtwo, args)
' Begin Recieving again
ResponseSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf ResponseRecieveCallBack, bytes)
End If
End If
Catch ex As Exception
System.Console.WriteLine("Disconnected")
End Try
End Sub
Private Sub CommandConnectCallBack(ByVal arone As IAsyncResult)
Try
CommandSocket.EndConnect(arone)
'SendCommandData()
Dim dlgone As New NoParamsDelegate(AddressOf ConnectedUI)
Me.Invoke(dlgone)
' begin recieve
' create buffer
Dim bytes(17000) As Byte
CommandSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf CommandRecieveCallBack, bytes)
Catch ex As SocketException
MessageBox.Show("The Server was either down or not responding. Please double check your server information that is under EtherNet->Configure.")
Console.WriteLine("Failed in ConnectCallBack")
End Try
End Sub
Private Sub ResponseConnectCallBack(ByVal artwo As IAsyncResult)
Try
ResponseSocket.EndConnect(artwo)
'SendCommandData()
Dim dlgtwo As New NoParamsDelegate(AddressOf ConnectedUI)
Me.Invoke(dlgtwo)
' begin recieve
' create buffer
Dim bytes(17000) As Byte
ResponseSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf ResponseRecieveCallBack, bytes)
Catch ex As SocketException
MessageBox.Show("The Server was either down or not responding. Please double check your server information that is under EtherNet->Configure.")
Console.WriteLine("Failed in ConnectCallBack")
End Try
End Sub
Private Delegate Sub NoParamsDelegate()
Private Sub ConnectedUI()
mnuConnect.Visible = False
mnuDisconnect.Visible = True
mnuSelftest.Visible = True
mnuNetconfig.Visible = False
ToolStripStatusLabel1.Text = "Status: Connected"
TimeSync()
Connected = True
End Sub
Private Sub DisConnectedUI()
mnuConnect.Visible = True
mnuDisconnect.Visible = False
mnuSelftest.Visible = False
mnuNetconfig.Visible = True
ToolStripStatusLabel1.Text = "Status: Disconnected"
Connected = False
End Sub
Private Sub TimeSync()
Dim bytes() As Byte = {22, 15, 65, 65, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 4}
Dim Year As UInt16 = System.DateTime.Now.Year
Dim Month As UInt16 = System.DateTime.Now.Month
Dim DayOfWeek As UInt16 = System.DateTime.Now.DayOfWeek
Dim Day As UInt16 = System.DateTime.Now.Day
Dim Hour As UInt16 = System.DateTime.Now.Hour
Dim Minute As UInt16 = System.DateTime.Now.Minute
Dim Second As UInt16 = System.DateTime.Now.Second
Dim MilliSeconds As UInt16 = System.DateTime.Now.Millisecond
'pushing year on the array
bytes(6) = Year >> 8
Year = Year << 8
bytes(5) = Year >> 8
'pushing month on the aray
bytes(8) = Month >> 8
Month = Month << 8
bytes(7) = Month >> 8
'pushing dayofweek on the array
bytes(10) = DayOfWeek >> 8
DayOfWeek = DayOfWeek << 8
bytes(9) = DayOfWeek >> 8
'pushing day on the array
bytes(12) = Day >> 8
Day = Day << 8
bytes(11) = Day >> 8
'pushing hour on the array
bytes(14) = Hour >> 8
Hour = Hour << 8
bytes(13) = Hour >> 8
'pushing minute on the array
bytes(16) = Minute >> 8
Minute = Minute << 8
bytes(15) = Minute >> 8
' pushing seconds on the array
bytes(18) = Second >> 8
Second = Second << 8
bytes(17) = Second >> 8
'pushing milliseconds on the array
bytes(20) = MilliSeconds >> 8
MilliSeconds = MilliSeconds << 8
bytes(19) = MilliSeconds >> 8
Dim chksum As Byte = Checksum(bytes)
bytes(21) = chksum
CommandSocket.Send(bytes)
'DEBUG: Print all the values in bytes
If DEBUG Then
Dim i As UInt16 = 0
Console.WriteLine("Time Values")
While i <= 23
Console.WriteLine(" {0:x8}", bytes(i))
i = i + 1
End While
End If
End Sub
Private Sub DisplayRecieveData(ByVal Data As String)
'With RecieveTxt
' .SelectionStart = .Text.Length
' .SelectedText = Data
'End With
'Console.Write(Data)
End Sub
Private Delegate Sub OneStringDelegate(ByVal Data As String)
Private Sub mnuSelftest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSelftest.Click
Dim bytes() As Byte = {22, 15, 49, 49, 48, 1, 0, 22, 4}
Dim chksum As Byte = Checksum(bytes)
If DEBUG Then
Console.WriteLine(chksum)
End If
bytes(6) = chksum
CommandSocket.Send(bytes)
Console.WriteLine(bytes)k
End Sub
Public Function Checksum(ByVal Number() As Byte)
Dim Sum As Long
Dim I As Int32
Dim Start As Int32 = LBound(Number) + 2
Dim Finish As Int32 = UBound(Number) - 3
Sum = 0
For I = Start To Finish
Sum = Sum + Not Number(I)
Next I
Checksum = CByte(Sum And &HFF)
End Function
''Public Function ValueOf(ByVal c() As Char)
'' ' this function is designed to take the char array and convert it to the integer value
'' Dim i As uInt16 = 0
'' Dim sum As uInt16 = 0
'' While i < c.Length
'' sum = Char.GetNumericValue(c(i)) * Math.Pow(10, i) + sum
'' i = i + 1
'' End While
'' ValueOf = sum
''End Function
Public Sub SendCommandData()
Dim bytes() As Byte = {&H16, &HF, &H41, &H41, &H30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &H16, &H4}
'send the change
'if the connection exists
If Connected Then
'Sending Change
'Startpulse 1
Dim StartPulse1 As UInt16 = StartDelay
bytes(6) = StartPulse1 >> 8
StartPulse1 = StartPulse1 << 8
bytes(5) = StartPulse1 >> 8
'stop pulse1
Dim StopPulse1 As UInt16 = StartDelay + PulseWidth
bytes(8) = StopPulse1 >> 8
StopPulse1 = StopPulse1 << 8
bytes(7) = StopPulse1 >> 8
''start pulse 2
'Dim StartPulse2 As Int16 = StartDelay + ValueOf(tbxSeparation.Text.ToCharArray)
Dim StartPulse2 As UInt16 = StartDelay + (tbxSeparation.Text / 0.05)
bytes(10) = StartPulse2 >> 8
StartPulse2 = StartPulse2 << 8
bytes(9) = StartPulse2 >> 8
'stop pulse 2
'Dim StopPulse2 As Int16 = StartDelay + ValueOf(tbxSeparation.Text.ToCharArray) + PulseWidth
Dim StopPulse2 As UInt16 = StartDelay + PulseWidth + (tbxSeparation.Text / 0.05)
bytes(12) = StopPulse2 >> 8
StopPulse2 = StopPulse2 << 8
bytes(11) = StopPulse2 >> 8
'pulse rate
Dim RepRt As UInt16 = tbxRepRate.Text
bytes(14) = RepRt >> 8
RepRt = RepRt << 8
Console.WriteLine("{0:x8}", RepRt)
bytes(13) = RepRt >> 8
If StrComp("DOUBLE", tbxMode.Text) = 0 Then
bytes(15) = 1
Else
bytes(15) = 0
End If
Dim chksum As Byte = Checksum(bytes)
bytes(16) = chksum
If DEBUG Then
Dim i As Int16 = 0
Console.WriteLine("Command Values")
While i <= 18
Console.WriteLine(" {0:x8}", bytes(i))
i = i + 1
End While
End If
CommandSocket.Send(bytes)
End If
End Sub
Private Sub mnuConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuConnect.Click
' create end point
Dim EP1 As IPEndPoint
Dim EP2 As IPEndPoint
' If either port or ip address are left blank assume port 80 on localhost
EP1 = New IPEndPoint(IPAddress.Parse(CommandAddress), CInt(CommandPort))
EP2 = New IPEndPoint(IPAddress.Parse(ResponseAddress), CInt(ResponsePort))
Try
CommandSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'ResponseSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' Open Connection
' Open connection async
CommandSocket.BeginConnect(EP1, AddressOf CommandConnectCallBack, Nothing)
'ResponseSocket.BeginConnect(EP2, AddressOf ResponseConnectCallBack, Nothing)
Catch ex As SocketException
MessageBox.Show("The Server was either down or not Responding")
Console.WriteLine("Failed in the mnuConnect_Click")
End Try
End Sub
Private Sub mnuDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDisconnect.Click
Try
CommandSocket.Shutdown(SocketShutdown.Both)
'ResponseSocket.Shutdown(SocketShutdown.Both)
CommandSocket.Close()
'ResponseSocket.Close()
Catch ex As Exception
MessageBox.Show("Failed to Disconnect... Try to restart the application")
Console.WriteLine("Failed to close connection in mnuDisconnect_click")
End Try
DisConnectedUI()
End Sub
Private Sub mnuNetconfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNetconfig.Click
If dlgConfig.ShowDialog() = DialogResult.OK Then
Console.WriteLine(dlgConfig.tbxAddress.Text)
End If
End Sub
Private Sub mnuPreferences_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPreferences.Click
End Sub
Private Sub mnuLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLoad.Click
End Sub
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
End Sub
Private Sub mnuReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReport.Click
End Sub
Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
End Sub
Private Sub StatusStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles StatusStrip.ItemClicked
End Sub
Private Sub btnMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMode.Click
If StrComp("DOUBLE", tbxMode.Text) = 0 Then
'change the text to single
tbxMode.Text = "SINGLE"
Else
tbxMode.Text = "DOUBLE"
End If
SendCommandData()
End Sub
Private Sub pbxRepRateDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbxRepRateDec.Click
tbxRepRate.Text = tbxRepRate.Text - 1
'Dim test As uInt16 = tbxRepRate.Text + 100
'Console.WriteLine("THIS IS A TEST")
'Console.WriteLine(test)
SendCommandData()
End Sub
Private Sub pbxRepRateInc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbxRepRateInc.Click
tbxRepRate.Text = tbxRepRate.Text + 1
SendCommandData()
End Sub
Private Sub pbxSeparationDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbxSeparationDec.Click
tbxSeparation.Text = tbxSeparation.Text - 0.05
SendCommandData()
End Sub
Private Sub pbxSeparationInc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbxSeparationInc.Click
tbxSeparation.Text = tbxSeparation.Text + 0.05
SendCommandData()
End Sub
Private Sub pbxTxMhzDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbxTxMhzDec.Click
End Sub
End Class |
|
|
|
|
|
#4 |
|
Man Bear Pig Hunter
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4
![]() |
Awesome, thanks for both the link and the code. Good man.
__________________
People who click "images" that end with .exe shouldn't have computers. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| network monitoring program | fresher | Java | 7 | Jan 16th, 2007 8:58 AM |
| VB6 Network Game - Server List | His_Ryanness | Visual Basic | 0 | Jun 14th, 2006 4:54 PM |
| network gaming | Nebula | Coder's Corner Lounge | 2 | Jun 11th, 2006 2:31 AM |
| Please Please Read - Very Big Bad Network Problems | bigguy | Coder's Corner Lounge | 9 | Feb 16th, 2006 8:46 PM |
| Help with asynchronous sockets? | Brent | C++ | 3 | Sep 9th, 2005 3:13 AM |