473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Socket still sending?

Hello Fellow Developer,

I use the System.Net.Sockets 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 I want the the
'last-time-socket-used' variable set when the socket is finished sending.
When I send by System.Net.Sockets.Socket.Send(buffer()) (<--this can be
10Mb) then it imitially returns to my thread, so the (internal) Send
functions buffers it. I tried to Poll, but this only tells you if you could
write to the socket, and I tried the getSocketOption(Tried
Socket/TCP/IP,Tried
SendBuffer/ReceiveBuffer/SendLowWater/ReceiveLowWater/SendTimeout/ReceiveTim
eout) but that throws an Error. (Before u look in my code I would like to
explain that I want to catch all exceptions in the parent object.)

Regards,
Robert

Ok this is myconnectionclass.vb

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Collections
Imports System.Threading
Imports System.Diagnostics

Public Class SmartConnection
Public Event ConnectionOpened(ByVal pMe As SmartConnection)
Public Event DataReceived(ByVal pMe As SmartConnection, ByVal Data As
Message)
Public Event CouldntConnect(ByVal pMe As SmartConnection)
Public Event ConnectionClosed(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.Connected =
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("Socket is not
connected")
Receiving = ((Not _ReadThread Is Nothing) AndAlso
_ReadThread.IsAlive)
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(hostName, port, timeout, True)
Catch e As Exception
OnError(e)
End Try
End Sub

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

Private Sub StartConnect(ByRef 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("Already
trying to connect")
_ReadThread = New Thread(AddressOf ConnectThread)
pHost = hostName : pPort = port : pRead = read : pTimeout =
timeout
_ReadThread.Start()
Catch e As Exception
OnError(e)
End Try
End Sub

Private Sub ConnectThread()
Try
If pHost = "" Then Throw New ArgumentNullException("hostname")
If pPort < IPEndPoint.MinPort Or pPort > IPEndPoint.MaxPort Then
Throw New ArgumentOutOfRangeException("port", "ArgRange_Port")
_Socket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Try
_Socket.Connect(New
IPEndPoint(Dns.Resolve(pHost).AddressList(0), pPort))
_lport = CType(_Socket.LocalEndPoint,
System.Net.IPEndPoint).Port
If Not pRead Then
SetReadName("SC.Read[PreConnect]")
_ReadThread = Nothing
Else
SetReadName("SC.EarlyRead[" & _lport & "]")
End If
OnConnectionOpened()
If pRead Then Receive()
Catch e As SocketException 'e.ErrorCode = 10061
SetReadName("SC.Read[NoConnect]")
_ReadThread = Nothing
OnCouldntConnect()
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("Receiving was already
started")
_ReadThread = New Thread(AddressOf Receive)
SetReadName("SC.LateRead[" & _lport & "]")
_ReadThread.Start()
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.Available = 0
'dt = ds
'ds = "@" & _lport & ","
'ds += _Socket.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendBuffer) & ","
'ds += _Socket.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendLowWater) & ","
'ds += _Socket.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout)
'Debug.WriteLineIf(dt <> ds, ds)
_ReadThread.Sleep(ReceiveWait)
If IsClosing Then Exit Sub
If _Socket.Poll(0, SelectMode.SelectError) Then

'System.Runtime.InteropServices.Marshal.GetLastWin 32Error())
'_Socket.GetSocketOption(SocketOptionLevel.IP,
SocketOptionName.Error)
OnError(New Exception("Socket Error"))
End If
If pTimeout > 0 Then
'If _Socket.Poll(0, SelectMode.SelectWrite) Then
'lasttimeout = CurrTick() + pTimeout
'Else
If lasttimeout <= CurrTick() Then
OnTimeOut()
Close()
Exit Sub
End If
'Endif
End If
Loop
ReDim b(_Socket.Available - 1)
'SyncLock _Socket
i = _Socket.Receive(b, SocketFlags.None)
'End SyncLock
If i <> b.Length Then Throw New
Exception("Socket-data-length differs")
If i = 0 Then Throw New Exception("Socket-data-length = 0")
OnMessageReceived(New Message(b))
b = Nothing
Loop
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(data))
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("Socket isn't
connected")
_Iswriting = True
'SyncLock _Socket
_Socket.Send(data)
'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("Already is closing")
_Closing = True
_StopThread = New Thread(AddressOf DoClose)
_StopThread.Name = "SC.Stop[" & _lport & "]"
_StopThread.Start()
Catch e As Exception
OnError(e)
End Try
End Sub

Private Sub DoClose()
Try
_StopThread.Sleep(CloseWait)
Try
If Not _ReadThread Is Nothing Then
SetReadName("SC.Read[Close]")
If _ReadThread.IsAlive Then _ReadThread.Abort()
End If
Catch e As Exception
End Try
Try
If Not _Socket Is Nothing AndAlso _Socket.Connected Then
_Socket.Close()
Catch e As Exception
End Try
_ReadThread = Nothing
_Socket = Nothing
_Closing = False
_StopThread = Nothing
OnConnectionClosed()
Catch e As Exception
OnError(e)
End Try
End Sub
#End Region
#Region " OnEvent"
Protected Sub OnError(ByRef pErr As Exception) 'If 2 errors in diff
threads?
Debug.WriteLine(pErr.Message)
Debug.WriteLine(pErr.StackTrace)
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 OnCouldntConnect()
Try
RaiseEvent CouldntConnect(Me)
Catch e As Exception
OnError(e)
End Try
End Sub

Protected Sub OnConnectionClosed()
Try
RaiseEvent ConnectionClosed(Me)
Catch e As Exception
OnError(e)
End Try
End Sub

Protected Sub OnConnectionOpened()
Try
RaiseEvent ConnectionOpened(Me)
Catch e As Exception
OnError(e)
End Try
End Sub

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

Private Sub SetReadName(ByRef pName As String)
If Not _ReadThread Is Nothing AndAlso _ReadThread.Name Is Nothing
Then _ReadThread.Name = pName
End Sub

End Class
Nov 15 '05 #1
3 4605
Hi Robert,

Are you using Blocking or Non-Blocking mode?
AFAIK when used in blocking mode the method is blocked till it finishes.
Other solution would be to try asnyc sending..

Miha
Nov 15 '05 #2
Hello Miha,

Thanks for your reaction.

I tried _Socket.Blocking = True in the sub ConnectThread. But this made no
differends.

With asnyc sending (BeginSend..EndSend), After the async callsback the
socket is still sending (even if blocking is on)

Regards, Robert

"Miha Markic" <mi***@spin.NO.SPAM.si> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi Robert,

Are you using Blocking or Non-Blocking mode?
AFAIK when used in blocking mode the method is blocked till it finishes.
Other solution would be to try asnyc sending..

Miha

Nov 15 '05 #3
The code for async is this:

Private Sub SendFinish(ByVal pia As System.IAsyncResult)
Try
If Not Connected Then Throw New Exception("Socket isn't
connected")
Dim i As Long = _Socket.EndSend(pia)
_Iswriting = False
Catch e As Exception
_Iswriting = False
OnError(e)
End Try
End Sub

Public Overridable Overloads Sub Send(ByRef data() As Byte)
Try
If Not Connected Then Throw New Exception("Socket isn't
connected")
'A data arrival can be faster then the syncthread
Thread.CurrentThread.Sleep(500) 'This is probally _ReadThread
If _Iswriting Then Throw New Exception("Already sending")
_Iswriting = True 'Cant write more then once!! TX/RC/RC/TX no
RC/TX/TX/RC
'SyncLock _Socket
'_Socket.Send(data)
_Socket.BeginSend(data, 0, data.Length, SocketFlags.None,
AddressOf SendFinish, Nothing)
'End SyncLock
'_Iswriting = False
Catch e As Exception
'_Iswriting = False
OnError(e)
End Try
End Sub

"Robert A. van Ginkel" <ro****@stylegate.com> schreef in bericht
news:Om**************@TK2MSFTNGP10.phx.gbl...
Hello Miha,

Thanks for your reaction.

I tried _Socket.Blocking = True in the sub ConnectThread. But this made no
differends.

With asnyc sending (BeginSend..EndSend), After the async callsback the
socket is still sending (even if blocking is on)

Regards, Robert

"Miha Markic" <mi***@spin.NO.SPAM.si> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi Robert,

Are you using Blocking or Non-Blocking mode?
AFAIK when used in blocking mode the method is blocked till it finishes.
Other solution would be to try asnyc sending..

Miha


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephan Steiner | last post by:
Hi The project I'm currently working on involves sending large UDP broadcasts. As the .NET framework already provides an easy facility for sending and receiving UDP packets I thought it was a...
3
by: Robert A. van Ginkel | last post by:
In news:OZ0W9RsdDHA.2432@TK2MSFTNGP10.phx.gbl... I ask the question how I can see if all the data is on the other side of the connection. I got as answer that I should use the blocking property. I...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
3
by: Sells, Fred | last post by:
I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also switch to Linux for development if...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
2
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds...
1
by: keksy | last post by:
Hi every1, I am writing a small client/server application and in it I want to send an image asynchronous from the client to the server through a TCP socket. I found an example code on the MSDN...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.