![]() |
Click here to advertise with us
|
|
|||||||
| Net / Sockets Components for network and related use |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Hi All,
I'm trying to setup an asynchronous client and I can send/receive one command and response, but when I try to send the second command, the command is sent (I verified by code inspection), but I am getting no response. Im a begginner! Heres the code, a majority of it is from the Microsoft example. Option Explicit On Imports System Imports System.Net Imports System.Net.Sockets Imports System.Text Imports system.convert Imports System.Threading Imports System.Collections Imports System.ComponentModel Public Class ComClient Public Sub SendCommand(ByVal CommandString As String, ByRef ResponseString As String) sendNetwork(CommandString) sendDone.WaitOne() tcpdataarival(ResponseString) receiveDone.WaitOne() End Sub Public Sub sendNetwork(ByVal outstr As String) frmMain.sender1.BeginSend(outstr, 0, outstr.Length, 0, New AsyncCallback(AddressOf SendCallback), frmMain.sender1) End Sub Public Sub tcpdataarival(ByRef responsestr As String) Dim state As New StateObject state.workSocket = frmMain.sender1 Dim responsebyte As String = "" ReDim state.tcpBuffer(128) frmMain.sender1.BeginReceive(state.tcpBuffer, 0, StateObject.tcpBufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state) responsestr = DecodeResponse(state.tcpBuffer) End Sub Public Sub tcpconnect() Dim ipHostInfo As IPHostEntry = System.Net.Dns.GetHostEntry(frmMain.txtbIP.Text) Dim ipAddress As IPAddress = ipHostInfo.AddressList(0) Dim remoteEP As New IPEndPoint(ipAddress, 48362) frmMain.sender1 = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) frmMain.sender1.BeginConnect(remoteEP, New AsyncCallback(AddressOf ConnectCallback), frmMain.sender1) End Sub Public Sub tcpdisconnect() frmMain.sender1.Shutdown(SocketShutdown.Both) frmMain.sender1.Close() End Sub Private Shared Sub ConnectCallback(ByVal ar As IAsyncResult) Dim client As Socket = CType(ar.AsyncState, Socket) ' Complete the connection. client.EndConnect(ar) Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()) ' Signal that the connection has been made. connectDone.Set() End Sub 'ConnectCallback Private Shared Sub ReceiveCallback(ByVal ar As IAsyncResult) ' Retrieve the state object and the client socket ' from the asynchronous state object. Dim state As StateObject = CType(ar.AsyncState, StateObject) Dim client As Socket = state.workSocket ' Read data from the remote device. Dim bytesRead As Integer = client.EndReceive(ar) If bytesRead > 0 Then ' There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString(state.tcp Buffer, 0, bytesRead)) ' Get the rest of the data. client.BeginReceive(state.tcpBuffer, 0, StateObject.tcpBufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state) Else ' All the data has arrived; put it in response. If state.sb.Length > 1 Then response = state.sb.ToString() End If ' Signal that all bytes have been received. receiveDone.Set() End If Console.WriteLine(receiveDone) End Sub 'ReceiveCallback Private Shared Sub SendCallback(ByVal ar As IAsyncResult) ' Retrieve the socket from the state object. Dim client As Socket = CType(ar.AsyncState, Socket) ' Complete sending the data to the remote device. Dim bytesSent As Integer = client.EndSend(ar) Console.WriteLine("Sent {0} bytes to server.", bytesSent) ' Signal that all bytes have been sent. sendDone.Set() End Sub 'SendCallback End Class 'ComClient Public Class StateObject ' Client socket. Public workSocket As Socket = Nothing ' Size of receive buffer. Public Const tcpBufferSize As Integer = 128 ' Receive buffer. Public tcpBuffer(tcpBufferSize) As Byte ' Received data string. Public sb As New StringBuilder End Class 'StateObject Last edited by Jayrm14; 06-17-2009 at 6:37 PM. |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|