a socket connect error problem when using .net cf on PDA

Discussion in 'Microsoft Dot NET Compact Framework' started by Guest, Dec 2, 2005.

  1. Guest

    Guest Guest

    hello,

    I have a problem that can not be solved.
    the problem is this:

    I want to use the socket class in CF to connect .and everytime before I
    connect ,i will first disconnect the socket.
    when I do the connect and disconnect operation (also i do the send recieve
    operation each for one time) for less than 70(may be more times) times,it
    runs very well.But when I go on to do the connect and disconnect operation
    for more times,it alwaysthrow the socketException! And then I cannot connect
    the socket successfully forever.

    The phenomenon won't occur when i use the activeSync version 3.7
    but when i use the activeSync version 3.71 or activeSync version 3.8,it
    will always happen.
    So I do have no idea whether it is the code error or is the activeSync
    version error.

    the envirement is this:
    PDA:Symbol MC50
    visual studio 2003
    ..NET COMPACT FRAMEWORK
    Pocket Pc
    Operation System:windows xp


    the code is this:
    :
    Option Explicit On
    Option Strict On

    Imports System.IO
    Imports System.Text
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading

    Public Enum NotifyCommand
    Connected '接続
    SentData 'é€ä¿¡
    ReceivedData 'å—ä¿¡
    SocketError 'socketエラー
    End Enum

    Public Class ClientSocket

    '''<summary> å—信データã®é•·ã•ã€‚</summary>
    Private m_iBytesRead As Integer
    ''' <summary>Notifyイベントã®ä»£ç†ã€‚</summary>
    Public Delegate Sub NotifyEventHandler(ByVal command As NotifyCommand,
    ByVal data As Object)
    '''<summary> Notifyイベント。</summary>
    Public Event Notify As NotifyEventHandler
    '''<summary> Socket対象。</summary>
    Private m_socket As Socket
    '''<summary>åŽå®¹å—信データã®ã‚¢ãƒ¬ã‚¤ </summary>
    Private m_readBuffer(1024) As Byte
    '''<summary> 制御Socketã®ã‚¹ãƒ¬ãƒƒãƒ‰ã®äº‹ä»¶ã€‚</summary>
    Private m_asyncEvent As ManualResetEvent = New ManualResetEvent(True)
    '''<summary>中止接続ã®ãƒ•ãƒ©ãƒƒã‚° </summary>
    Private m_disconnecting As Boolean = False

    Public ReadOnly Property Connected() As Boolean

    Get

    ' return right away if have not created socket
    If m_socket Is Nothing Then
    Return False
    End If

    ' the socket is not connected if the Connected property is false
    If m_socket.Connected = False Then
    Return False
    End If

    ' there is no guarantee that the socket is connected even if the
    ' Connected property is true
    Try
    ' poll for error to see if socket is connected
    Return Not m_socket.Poll(1, SelectMode.SelectError)
    Catch
    Return False
    End Try

    End Get

    End Property

    Public Function Connect(ByVal address As String, ByVal port As Integer)
    As IAsyncResult

    ' make sure disconnected
    Disconnect()

    ' connect to the server
    Dim ip As IPAddress = IPAddress.Parse(address)
    Dim endPoint As IPEndPoint = New IPEndPoint(ip, port)

    m_socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
    ProtocolType.Tcp)

    m_asyncEvent.Reset()

    m_socket.BeginConnect(endPoint, AddressOf ConnectCallback, Nothing)

    m_asyncEvent.WaitOne()

    End Function

    Public Sub Disconnect()

    ' return right away if have not created socket
    If m_socket Is Nothing Then
    Return
    End If

    ' set this flag so we don't raise any error notification
    ' events when disconnecting
    m_disconnecting = True

    Try
    ' first, shutdown the socket
    m_socket.Shutdown(SocketShutdown.Both)

    Catch e As Exception

    End Try

    Try
    ' next, close the socket which terminates any pending
    ' async operations
    m_socket.Close()
    ' wait for any async operations to complete
    m_asyncEvent.WaitOne()
    Catch

    End Try

    m_disconnecting = False

    End Sub

    Public Sub Send(ByVal data() As Byte)

    ' send the data
    m_asyncEvent.Reset()

    m_socket.BeginSend(data, 0, data.Length, SocketFlags.None, _
    AddressOf SendCallback, True)

    m_asyncEvent.WaitOne()

    End Sub

    Public Sub Receive()

    m_asyncEvent.Reset()

    m_socket.BeginReceive(m_readBuffer, 0, m_readBuffer.Length,
    SocketFlags.None, _
    AddressOf ReceiveCallback, Nothing)

    m_asyncEvent.WaitOne()

    End Sub

    Private Sub RaiseNotifyEvent(ByVal command As NotifyCommand, ByVal data
    As Object)

    ' the async operation has completed
    m_asyncEvent.Set()

    ' don't raise notification events when disconnecting
    If m_disconnecting = False Then
    RaiseEvent Notify(command, data)
    End If

    End Sub

    Public Sub ConnectCallback(ByVal ar As IAsyncResult)

    Try

    m_socket.EndConnect(ar)

    If Me.Connected = True Then
    RaiseNotifyEvent(NotifyCommand.Connected, Connected)
    End If

    Catch ex As Exception

    RaiseNotifyEvent(NotifyCommand.SocketError, False)

    End Try

    m_asyncEvent.Set()

    End Sub

    Private Sub SendCallback(ByVal ar As IAsyncResult)

    Dim bytessend As Integer

    Try

    bytessend = m_socket.EndSend(ar)

    If bytessend > 0 Then
    RaiseNotifyEvent(NotifyCommand.SentData, True)
    Else
    RaiseNotifyEvent(NotifyCommand.SentData, False)
    End If

    Catch ex As Exception

    RaiseNotifyEvent(NotifyCommand.SocketError, False)

    End Try

    m_asyncEvent.Set()

    End Sub

    Private Sub ReceiveCallback(ByVal ar As IAsyncResult)

    Dim read As Integer

    Try

    read = m_socket.EndReceive(ar)

    ' the server sends an acknowledgment back
    ' that is stored in the read buffer
    'If _readBuffer.Length() > 0 Then
    If read > 0 Then
    m_iBytesRead = read
    RaiseNotifyEvent(NotifyCommand.ReceivedData, True)
    Else
    RaiseNotifyEvent(NotifyCommand.ReceivedData, False)
    End If

    Catch ex As Exception

    RaiseNotifyEvent(NotifyCommand.SocketError, False)

    End Try

    End Sub

    Public Function GetMessage() As String

    Return ASCIIEncoding.Default.GetString(m_readBuffer, 0, m_iBytesRead)

    End Function

    Public Function Available() As Integer

    Try
    Return m_socket.Available
    Catch ex As Exception

    End Try

    Return 0

    End Function

    Public Sub New()

    End Sub

    End Class

    can anyone help?
    thanks!
     
    Guest, Dec 2, 2005
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.