Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Components & Controls > Net / Sockets

Net / Sockets Components for network and related use

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-17-2009, 5:50 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jun 2009
Age: 25
Posts: 1
Reputation: 0
Jayrm14 is on a distinguished programming path ahead
Default Cannot receive more than 1 response when sending asynchronously

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 11:28 AM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.