Is socketbuffer empty?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert A. van Ginkel

    #1

    Is socketbuffer empty?

    Hello Fellow Developer,

    This looks like a long mail, but at the end of this post is my socket
    wrapper attached.
    I want to make a timeout procedure that starts counting down after the
    socketbuffer is empty.
    This connection is being used to transmit data following a RFC so I cann't
    create a control protocol that tells me the data was received.
    (And even if I could, I want the timeout procedure to start after I send all
    data, because that makes for me the most sense. (say I am sending 128mb the
    socket will take longer sending then the timeout interval))
    Any questions/remarks, NOT related to my question, pls ask directly via
    email, dont post on the news server because It toke me alot of effort to
    create this post.

    Sample case:
    if u send 1 byte from a client in Canada to a server in Australia with a 28k
    modem thats being shared by an office of 30 people connected to the slowest
    profider. (etc.)
    What I want to say is that 1 byte may take a couple of minutes, and I use 1
    byte as an
    example because I don't want to make a question more complicated.

    TCP/IP is a async protocol, but due to the Berkley Connection engine it
    keeps track of what was send and received and what was not, so when there is
    a packet loss it will send again.
    So a socket keeps the data sequential, and if we are optimistic, the only
    thing that can go wrong is a socket error.

    There are three stadiums(simpli fied):
    (A) My object sends to the Winsock, (buffer is being fed, no data on the
    network, no data on the remote point)
    (B) Winsock processes his buffer and sends on the network and waits for a
    confirment that the package is succesfull transmitted(buf fer is 'empty',
    data on the network, no data on the remote point)
    (C) All data is correctly processed and transported (buffer is truely empty,
    no data on the network,data on the remote point)

    Relative to the Sample case:
    I achieve (A) ofcourse, I would really want to know (C), but I would be
    satisfied with (B). I know (B) can be achieved, I hope (C) is too.

    VB.NET FrameWork 2003(all patched and updated):
    I use the System.Net.Sock ets to send/receive data (no
    tcpclient/tcplistener), I made a receivethread in my wrapper, the
    receivethread loops/sleeps while waiting for data and then fires a
    datareceived event.
    Within the waitingloop there is a timeout function but this doesn't work
    properly (It will never timeout).
    When I send by System.Net.Sock ets.Socket.Send (buffer()) (<--this can be
    10Mb) then it imitially returns to my thread, so the (internal) Send method
    buffers it.
    The System.Net.Sock ets.Socket is a wrapper around the Win32 Socket 2 API's,
    it makes internal calls to those api's, so I started reading if they forgot
    implementing some functions, but they are all implemented.

    I tried:
    - The beginSend/EndSend (Async way with callback when command is finished)
    This calls within the async object the Socket.Send method, so its no
    help, I can better do Socket.Send myself.
    - The Socket.Blocking Property, it only blocks the Receive method,
    Socket.Send goes directly in the buffer and then it returns immitially.
    - Socket.Poll Method, returns the accessrights of the socket, "may I use
    this socket to write?"
    -
    getSocketOption (Socket/TCP/IP,SendBuffer/ReceiveBuffer/SendLowWater/ReceiveL
    owWater/SendTimeout/ReceiveTimeout) Method
    Throws an Exception

    MyWrapper.vb:
    Imports System
    Imports System.Net
    Imports System.Net.Sock ets
    Imports System.IO
    Imports System.Collecti ons
    Imports System.Threadin g
    Imports System.Diagnost ics

    Public Class SmartConnection
    Public Event ConnectionOpene d(ByVal pMe As SmartConnection )
    Public Event DataReceived(By Val pMe As SmartConnection , ByVal Data As
    Message)
    Public Event CouldntConnect( ByVal pMe As SmartConnection )
    Public Event ConnectionClose d(ByVal pMe As SmartConnection )
    Public Event [Error](ByVal pMe As SmartConnection , ByVal pErr As
    Exception)
    Public Event TimeOut(ByVal pme As SmartConnection )

    Private _Socket As Socket
    Private _ReadThread As Thread
    Private _StopThread As Thread 'Lookup threadingpool
    Private _Closing As Boolean = False
    Private _InError As Boolean = False
    Private _lport As Long
    Private lasttimeout As Long
    Private pHost As String
    Private pPort As Long
    Private pRead As Boolean
    Private pTimeout As Long
    Private _Iswriting As Boolean = False

    Protected Overrides Sub Finalize()
    _ReadThread = Nothing
    _StopThread = Nothing
    _Socket = Nothing
    MyBase.Finalize ()
    End Sub

    #Region " Properties"
    Public ReadOnly Property IsClosing() As Boolean
    Get
    IsClosing = (_Closing Or _Socket Is Nothing)
    End Get
    End Property

    Public ReadOnly Property Connected() As Boolean
    Get
    Try
    Connected = ((Not IsClosing) AndAlso _Socket.Connect ed =
    True)
    Catch e As Exception
    OnError(e)
    End Try
    End Get
    End Property

    Protected ReadOnly Property Receiving() As Boolean
    Get
    Try
    If Not Connected Then Throw New Exception("Sock et is not
    connected")
    Receiving = ((Not _ReadThread Is Nothing) AndAlso
    _ReadThread.IsA live)
    Catch e As Exception
    OnError(e)
    End Try
    End Get
    End Property

    Public ReadOnly Property IPlocal() As String
    Get
    Try
    If Connected Then IPlocal = EP2IPS(_Socket. LocalEndPoint)
    Catch e As Exception
    OnError(e)
    End Try
    End Get
    End Property

    Public ReadOnly Property IPremote() As String
    Get
    Try
    If Connected Then IPremote = EP2IPS(_Socket. RemoteEndPoint)
    Catch e As Exception
    OnError(e)
    End Try
    End Get
    End Property
    #End Region
    #Region " Connect"
    Public Overridable Sub ConnectTo(ByRef hostName As String, ByRef port As
    Long, Optional ByVal timeout As Long = 0)
    Try
    StartConnect(ho stName, port, timeout, True)
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Protected Sub ConnectToWithou tReading(ByRef hostName As String, ByRef
    port As Long, Optional ByVal timeout As Long = 0)
    Try
    StartConnect(ho stName, port, timeout, False)
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Private Sub StartConnect(By Ref hostName As String, ByRef port As Long,
    ByRef timeout As Long, ByRef read As Boolean)
    Try
    If Not _ReadThread Is Nothing Then Throw New Exception("Alre ady
    trying to connect")
    _ReadThread = New Thread(AddressO f ConnectThread)
    pHost = hostName : pPort = port : pRead = read : pTimeout =
    timeout
    _ReadThread.Sta rt()
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Private Sub ConnectThread()
    Try
    If pHost = "" Then Throw New ArgumentNullExc eption("hostnam e")
    If pPort < IPEndPoint.MinP ort Or pPort > IPEndPoint.MaxP ort Then
    Throw New ArgumentOutOfRa ngeException("p ort", "ArgRange_Port" )
    _Socket = New Socket(AddressF amily.InterNetw ork,
    SocketType.Stre am, ProtocolType.Tc p)
    Try
    _Socket.Blockin g = True
    _Socket.Connect (New
    IPEndPoint(Dns. Resolve(pHost). AddressList(0), pPort))
    _lport = CType(_Socket.L ocalEndPoint, IPEndPoint).Por t
    If Not pRead Then
    SetReadName("SC .Read[PreConnect]")
    _ReadThread = Nothing
    Else
    SetReadName("SC .EarlyRead[" & _lport & "]")
    End If
    OnConnectionOpe ned()
    If pRead Then Receive()
    Catch e As SocketException 'e.ErrorCode = 10061
    SetReadName("SC .Read[NoConnect]")
    _ReadThread = Nothing
    OnCouldntConnec t()
    Close()
    End Try
    Catch e As Exception
    SetReadName("SC .Read[Error]")
    _ReadThread = Nothing
    OnError(e)
    End Try
    End Sub
    #End Region
    #Region " Receive"
    Protected Sub StartReceive()
    Try
    If Receiving Then Throw New Exception("Rece iving was already
    started")
    _ReadThread = New Thread(AddressO f Receive)
    SetReadName("SC .LateRead[" & _lport & "]")
    _ReadThread.Sta rt()
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Private Sub Receive()
    Dim i As Long
    Dim b() As Byte
    'Dim ds As String, dt As String
    Try
    Do While Connected
    If pTimeout > 0 Then lasttimeout = CurrTick() + pTimeout
    Do While _Socket.Availab le = 0
    'dt = ds
    'ds = "@" & _lport & ","
    'ds += _Socket.GetSock etOption(Socket OptionLevel.Soc ket,
    SocketOptionNam e.SendBuffer) & ","
    'ds += _Socket.GetSock etOption(Socket OptionLevel.Soc ket,
    SocketOptionNam e.SendLowWater) & ","
    'ds += _Socket.GetSock etOption(Socket OptionLevel.Soc ket,
    SocketOptionNam e.SendTimeout)
    'Debug.WriteLin eIf(dt <> ds, ds)
    _ReadThread.Sle ep(ReceiveWait)
    If IsClosing Then Exit Sub
    If _Socket.Poll(0, SelectMode.Sele ctError) Then

    'System.Runtime .InteropService s.Marshal.GetLa stWin32Error())
    '_Socket.GetSoc ketOption(Socke tOptionLevel.IP ,
    SocketOptionNam e.Error)
    OnError(New Exception("Sock et Error"))
    End If
    If pTimeout > 0 Then
    'If _Socket.Poll(0, SelectMode.Sele ctWrite) Then
    'lasttimeout = CurrTick() + pTimeout
    'Else
    If lasttimeout <= CurrTick() Then
    OnTimeOut()
    Close()
    Exit Sub
    End If
    'Endif
    End If
    Loop
    ReDim b(_Socket.Avail able - 1)
    'SyncLock _Socket
    i = _Socket.Receive (b, SocketFlags.Non e)
    'End SyncLock
    If i < b.Length Then ReDim b(i - 1)
    OnMessageReceiv ed(New Message(b))
    b = Nothing
    Loop
    Catch e As ThreadAbortExce ption
    Exit Sub
    Catch e As Exception
    OnError(e)
    End Try
    End Sub
    #End Region
    #Region " Send"
    Public Overridable Overloads Sub Send(ByRef data As String)
    Try
    Send(getbytes(d ata))
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Public Overridable Overloads Sub Send(ByRef data() As Byte)
    Try
    If Not Connected Then Throw New Exception("Sock et isn't
    connected")
    _Iswriting = True
    'SyncLock _Socket
    _Socket.Send(da ta)
    'End SyncLock
    _Iswriting = False
    Catch e As Exception
    _Iswriting = False
    OnError(e)
    End Try
    End Sub
    #End Region
    #Region " Close"
    Public Overridable Sub Close() 'OR QUEE IT?
    Try
    If IsClosing Then Throw New Exception("Alre ady is closing")
    _Closing = True
    _StopThread = New Thread(AddressO f DoClose)
    _StopThread.Nam e = "SC.Stop[" & _lport & "]"
    _StopThread.Sta rt()
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Private Sub DoClose()
    Try
    _StopThread.Sle ep(CloseWait)
    Try
    If Not _ReadThread Is Nothing Then
    SetReadName("SC .Read[Close]")
    If _ReadThread.IsA live Then _ReadThread.Abo rt()
    End If
    Catch e As Exception
    End Try
    Try
    If Not _Socket Is Nothing AndAlso _Socket.Connect ed Then
    _Socket.Close()
    Catch e As Exception
    End Try
    _ReadThread = Nothing
    _Socket = Nothing
    _Closing = False
    _StopThread = Nothing
    OnConnectionClo sed()
    Catch e As Exception
    OnError(e)
    End Try
    End Sub
    #End Region
    #Region " OnEvent"
    Protected Overridable Sub OnError(ByRef pErr As Exception) 'If 2 errors
    in diff threads?
    Try
    If Not _InError Then
    _InError = True
    Close()
    _InError = False
    End If
    RaiseEvent Error(Me, pErr)
    Catch e As Exception
    Throw e
    End Try
    End Sub

    Protected Sub OnTimeOut()
    Try
    RaiseEvent TimeOut(Me)
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Protected Sub OnCouldntConnec t()
    Try
    RaiseEvent CouldntConnect( Me)
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Protected Sub OnConnectionClo sed()
    Try
    RaiseEvent ConnectionClose d(Me)
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Protected Sub OnConnectionOpe ned()
    Try
    RaiseEvent ConnectionOpene d(Me)
    Catch e As Exception
    OnError(e)
    End Try
    End Sub

    Protected Sub OnMessageReceiv ed(ByRef data As Message)
    Try
    RaiseEvent DataReceived(Me , data)
    Catch e As Exception
    OnError(e)
    End Try
    End Sub
    #End Region

    Private Sub SetReadName(ByR ef pName As String)
    If Not _ReadThread Is Nothing AndAlso _ReadThread.Nam e Is Nothing
    Then _ReadThread.Nam e = pName
    End Sub

    End Class


  • Ron Alberda [MSFT]

    #2
    Re: Is socketbuffer empty?

    Actually Socket.Poll doesn't return the access rights, it tells you the
    state of the socket. For example if you do a Send and then do a:

    Socket.Poll(Sel ectMode.SelectR ead)

    It will return false until the server sends a response.

    I think that's the closest you can get to what you want.

    -Ron

    "Robert A. van Ginkel" <robert@stylega te.com> wrote in message
    news:uSKXef2eDH A.1820@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hello Fellow Developer,
    >
    > This looks like a long mail, but at the end of this post is my socket
    > wrapper attached.
    > I want to make a timeout procedure that starts counting down after the
    > socketbuffer is empty.
    > This connection is being used to transmit data following a RFC so I cann't
    > create a control protocol that tells me the data was received.
    > (And even if I could, I want the timeout procedure to start after I send[/color]
    all[color=blue]
    > data, because that makes for me the most sense. (say I am sending 128mb[/color]
    the[color=blue]
    > socket will take longer sending then the timeout interval))
    > Any questions/remarks, NOT related to my question, pls ask directly via
    > email, dont post on the news server because It toke me alot of effort to
    > create this post.
    >
    > Sample case:
    > if u send 1 byte from a client in Canada to a server in Australia with a[/color]
    28k[color=blue]
    > modem thats being shared by an office of 30 people connected to the[/color]
    slowest[color=blue]
    > profider. (etc.)
    > What I want to say is that 1 byte may take a couple of minutes, and I use[/color]
    1[color=blue]
    > byte as an
    > example because I don't want to make a question more complicated.
    >
    > TCP/IP is a async protocol, but due to the Berkley Connection engine it
    > keeps track of what was send and received and what was not, so when there[/color]
    is[color=blue]
    > a packet loss it will send again.
    > So a socket keeps the data sequential, and if we are optimistic, the only
    > thing that can go wrong is a socket error.
    >
    > There are three stadiums(simpli fied):
    > (A) My object sends to the Winsock, (buffer is being fed, no data on the
    > network, no data on the remote point)
    > (B) Winsock processes his buffer and sends on the network and waits for a
    > confirment that the package is succesfull transmitted(buf fer is 'empty',
    > data on the network, no data on the remote point)
    > (C) All data is correctly processed and transported (buffer is truely[/color]
    empty,[color=blue]
    > no data on the network,data on the remote point)
    >
    > Relative to the Sample case:
    > I achieve (A) ofcourse, I would really want to know (C), but I would be
    > satisfied with (B). I know (B) can be achieved, I hope (C) is too.
    >
    > VB.NET FrameWork 2003(all patched and updated):
    > I use the System.Net.Sock ets to send/receive data (no
    > tcpclient/tcplistener), I made a receivethread in my wrapper, the
    > receivethread loops/sleeps while waiting for data and then fires a
    > datareceived event.
    > Within the waitingloop there is a timeout function but this doesn't work
    > properly (It will never timeout).
    > When I send by System.Net.Sock ets.Socket.Send (buffer()) (<--this can be
    > 10Mb) then it imitially returns to my thread, so the (internal) Send[/color]
    method[color=blue]
    > buffers it.
    > The System.Net.Sock ets.Socket is a wrapper around the Win32 Socket 2[/color]
    API's,[color=blue]
    > it makes internal calls to those api's, so I started reading if they[/color]
    forgot[color=blue]
    > implementing some functions, but they are all implemented.
    >
    > I tried:
    > - The beginSend/EndSend (Async way with callback when command is finished)
    > This calls within the async object the Socket.Send method, so its no
    > help, I can better do Socket.Send myself.
    > - The Socket.Blocking Property, it only blocks the Receive method,
    > Socket.Send goes directly in the buffer and then it returns immitially.
    > - Socket.Poll Method, returns the accessrights of the socket, "may I use
    > this socket to write?"
    > -
    >[/color]
    getSocketOption (Socket/TCP/IP,SendBuffer/ReceiveBuffer/SendLowWater/ReceiveL[color=blue]
    > owWater/SendTimeout/ReceiveTimeout) Method
    > Throws an Exception
    >
    > MyWrapper.vb:
    > Imports System
    > Imports System.Net
    > Imports System.Net.Sock ets
    > Imports System.IO
    > Imports System.Collecti ons
    > Imports System.Threadin g
    > Imports System.Diagnost ics
    >
    > Public Class SmartConnection
    > Public Event ConnectionOpene d(ByVal pMe As SmartConnection )
    > Public Event DataReceived(By Val pMe As SmartConnection , ByVal Data As
    > Message)
    > Public Event CouldntConnect( ByVal pMe As SmartConnection )
    > Public Event ConnectionClose d(ByVal pMe As SmartConnection )
    > Public Event [Error](ByVal pMe As SmartConnection , ByVal pErr As
    > Exception)
    > Public Event TimeOut(ByVal pme As SmartConnection )
    >
    > Private _Socket As Socket
    > Private _ReadThread As Thread
    > Private _StopThread As Thread 'Lookup threadingpool
    > Private _Closing As Boolean = False
    > Private _InError As Boolean = False
    > Private _lport As Long
    > Private lasttimeout As Long
    > Private pHost As String
    > Private pPort As Long
    > Private pRead As Boolean
    > Private pTimeout As Long
    > Private _Iswriting As Boolean = False
    >
    > Protected Overrides Sub Finalize()
    > _ReadThread = Nothing
    > _StopThread = Nothing
    > _Socket = Nothing
    > MyBase.Finalize ()
    > End Sub
    >
    > #Region " Properties"
    > Public ReadOnly Property IsClosing() As Boolean
    > Get
    > IsClosing = (_Closing Or _Socket Is Nothing)
    > End Get
    > End Property
    >
    > Public ReadOnly Property Connected() As Boolean
    > Get
    > Try
    > Connected = ((Not IsClosing) AndAlso _Socket.Connect ed =
    > True)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Get
    > End Property
    >
    > Protected ReadOnly Property Receiving() As Boolean
    > Get
    > Try
    > If Not Connected Then Throw New Exception("Sock et is not
    > connected")
    > Receiving = ((Not _ReadThread Is Nothing) AndAlso
    > _ReadThread.IsA live)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Get
    > End Property
    >
    > Public ReadOnly Property IPlocal() As String
    > Get
    > Try
    > If Connected Then IPlocal = EP2IPS(_Socket. LocalEndPoint)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Get
    > End Property
    >
    > Public ReadOnly Property IPremote() As String
    > Get
    > Try
    > If Connected Then IPremote =[/color]
    EP2IPS(_Socket. RemoteEndPoint)[color=blue]
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Get
    > End Property
    > #End Region
    > #Region " Connect"
    > Public Overridable Sub ConnectTo(ByRef hostName As String, ByRef port[/color]
    As[color=blue]
    > Long, Optional ByVal timeout As Long = 0)
    > Try
    > StartConnect(ho stName, port, timeout, True)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Protected Sub ConnectToWithou tReading(ByRef hostName As String, ByRef
    > port As Long, Optional ByVal timeout As Long = 0)
    > Try
    > StartConnect(ho stName, port, timeout, False)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Private Sub StartConnect(By Ref hostName As String, ByRef port As Long,
    > ByRef timeout As Long, ByRef read As Boolean)
    > Try
    > If Not _ReadThread Is Nothing Then Throw New[/color]
    Exception("Alre ady[color=blue]
    > trying to connect")
    > _ReadThread = New Thread(AddressO f ConnectThread)
    > pHost = hostName : pPort = port : pRead = read : pTimeout =
    > timeout
    > _ReadThread.Sta rt()
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Private Sub ConnectThread()
    > Try
    > If pHost = "" Then Throw New ArgumentNullExc eption("hostnam e")
    > If pPort < IPEndPoint.MinP ort Or pPort > IPEndPoint.MaxP ort[/color]
    Then[color=blue]
    > Throw New ArgumentOutOfRa ngeException("p ort", "ArgRange_Port" )
    > _Socket = New Socket(AddressF amily.InterNetw ork,
    > SocketType.Stre am, ProtocolType.Tc p)
    > Try
    > _Socket.Blockin g = True
    > _Socket.Connect (New
    > IPEndPoint(Dns. Resolve(pHost). AddressList(0), pPort))
    > _lport = CType(_Socket.L ocalEndPoint, IPEndPoint).Por t
    > If Not pRead Then
    > SetReadName("SC .Read[PreConnect]")
    > _ReadThread = Nothing
    > Else
    > SetReadName("SC .EarlyRead[" & _lport & "]")
    > End If
    > OnConnectionOpe ned()
    > If pRead Then Receive()
    > Catch e As SocketException 'e.ErrorCode = 10061
    > SetReadName("SC .Read[NoConnect]")
    > _ReadThread = Nothing
    > OnCouldntConnec t()
    > Close()
    > End Try
    > Catch e As Exception
    > SetReadName("SC .Read[Error]")
    > _ReadThread = Nothing
    > OnError(e)
    > End Try
    > End Sub
    > #End Region
    > #Region " Receive"
    > Protected Sub StartReceive()
    > Try
    > If Receiving Then Throw New Exception("Rece iving was already
    > started")
    > _ReadThread = New Thread(AddressO f Receive)
    > SetReadName("SC .LateRead[" & _lport & "]")
    > _ReadThread.Sta rt()
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Private Sub Receive()
    > Dim i As Long
    > Dim b() As Byte
    > 'Dim ds As String, dt As String
    > Try
    > Do While Connected
    > If pTimeout > 0 Then lasttimeout = CurrTick() + pTimeout
    > Do While _Socket.Availab le = 0
    > 'dt = ds
    > 'ds = "@" & _lport & ","
    > 'ds +=[/color]
    _Socket.GetSock etOption(Socket OptionLevel.Soc ket,[color=blue]
    > SocketOptionNam e.SendBuffer) & ","
    > 'ds +=[/color]
    _Socket.GetSock etOption(Socket OptionLevel.Soc ket,[color=blue]
    > SocketOptionNam e.SendLowWater) & ","
    > 'ds +=[/color]
    _Socket.GetSock etOption(Socket OptionLevel.Soc ket,[color=blue]
    > SocketOptionNam e.SendTimeout)
    > 'Debug.WriteLin eIf(dt <> ds, ds)
    > _ReadThread.Sle ep(ReceiveWait)
    > If IsClosing Then Exit Sub
    > If _Socket.Poll(0, SelectMode.Sele ctError) Then
    >
    > 'System.Runtime .InteropService s.Marshal.GetLa stWin32Error())
    > '_Socket.GetSoc ketOption(Socke tOptionLevel.IP ,
    > SocketOptionNam e.Error)
    > OnError(New Exception("Sock et Error"))
    > End If
    > If pTimeout > 0 Then
    > 'If _Socket.Poll(0, SelectMode.Sele ctWrite) Then
    > 'lasttimeout = CurrTick() + pTimeout
    > 'Else
    > If lasttimeout <= CurrTick() Then
    > OnTimeOut()
    > Close()
    > Exit Sub
    > End If
    > 'Endif
    > End If
    > Loop
    > ReDim b(_Socket.Avail able - 1)
    > 'SyncLock _Socket
    > i = _Socket.Receive (b, SocketFlags.Non e)
    > 'End SyncLock
    > If i < b.Length Then ReDim b(i - 1)
    > OnMessageReceiv ed(New Message(b))
    > b = Nothing
    > Loop
    > Catch e As ThreadAbortExce ption
    > Exit Sub
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    > #End Region
    > #Region " Send"
    > Public Overridable Overloads Sub Send(ByRef data As String)
    > Try
    > Send(getbytes(d ata))
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Public Overridable Overloads Sub Send(ByRef data() As Byte)
    > Try
    > If Not Connected Then Throw New Exception("Sock et isn't
    > connected")
    > _Iswriting = True
    > 'SyncLock _Socket
    > _Socket.Send(da ta)
    > 'End SyncLock
    > _Iswriting = False
    > Catch e As Exception
    > _Iswriting = False
    > OnError(e)
    > End Try
    > End Sub
    > #End Region
    > #Region " Close"
    > Public Overridable Sub Close() 'OR QUEE IT?
    > Try
    > If IsClosing Then Throw New Exception("Alre ady is closing")
    > _Closing = True
    > _StopThread = New Thread(AddressO f DoClose)
    > _StopThread.Nam e = "SC.Stop[" & _lport & "]"
    > _StopThread.Sta rt()
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Private Sub DoClose()
    > Try
    > _StopThread.Sle ep(CloseWait)
    > Try
    > If Not _ReadThread Is Nothing Then
    > SetReadName("SC .Read[Close]")
    > If _ReadThread.IsA live Then _ReadThread.Abo rt()
    > End If
    > Catch e As Exception
    > End Try
    > Try
    > If Not _Socket Is Nothing AndAlso _Socket.Connect ed Then
    > _Socket.Close()
    > Catch e As Exception
    > End Try
    > _ReadThread = Nothing
    > _Socket = Nothing
    > _Closing = False
    > _StopThread = Nothing
    > OnConnectionClo sed()
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    > #End Region
    > #Region " OnEvent"
    > Protected Overridable Sub OnError(ByRef pErr As Exception) 'If 2[/color]
    errors[color=blue]
    > in diff threads?
    > Try
    > If Not _InError Then
    > _InError = True
    > Close()
    > _InError = False
    > End If
    > RaiseEvent Error(Me, pErr)
    > Catch e As Exception
    > Throw e
    > End Try
    > End Sub
    >
    > Protected Sub OnTimeOut()
    > Try
    > RaiseEvent TimeOut(Me)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Protected Sub OnCouldntConnec t()
    > Try
    > RaiseEvent CouldntConnect( Me)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Protected Sub OnConnectionClo sed()
    > Try
    > RaiseEvent ConnectionClose d(Me)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Protected Sub OnConnectionOpe ned()
    > Try
    > RaiseEvent ConnectionOpene d(Me)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    >
    > Protected Sub OnMessageReceiv ed(ByRef data As Message)
    > Try
    > RaiseEvent DataReceived(Me , data)
    > Catch e As Exception
    > OnError(e)
    > End Try
    > End Sub
    > #End Region
    >
    > Private Sub SetReadName(ByR ef pName As String)
    > If Not _ReadThread Is Nothing AndAlso _ReadThread.Nam e Is Nothing
    > Then _ReadThread.Nam e = pName
    > End Sub
    >
    > End Class
    >
    >[/color]


    Comment

    • K_Lee

      #3
      Re: Is socketbuffer empty?

      .....[color=blue][color=green]
      > > There are three stadiums(simpli fied):
      > > (A) My object sends to the Winsock, (buffer is being fed, no data on the
      > > network, no data on the remote point)
      > > (B) Winsock processes his buffer and sends on the network and waits for a
      > > confirment that the package is succesfull transmitted(buf fer is 'empty',
      > > data on the network, no data on the remote point)
      > > (C) All data is correctly processed and transported (buffer is truely[/color]
      > empty,[color=green]
      > > no data on the network,data on the remote point)
      > >
      > > Relative to the Sample case:
      > > I achieve (A) ofcourse, I would really want to know (C), but I would be
      > > satisfied with (B). I know (B) can be achieved, I hope (C) is too.
      > >[/color][/color]

      From this high level descriptions of the problem, I would create a
      Application level Msg/Ack handshake to truely make
      sure the remote is responding to my local requests.

      I don't think peeking into the TCP stack at one system can give
      your enough info to confirm the data get to the remote.





      --
      Browse source code and document for .NET CLI, Mozilla, Apache, NetBSD at
      http://www.slink-software.com?E=SLinkEvn_1004

      Comment

      Working...