Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Communiqué

Post new topic   Reply to topic
winsock(AX not API) memory leak ?
View previous topic :: View next topic  
Author Message
johnny_skunk
Newbie


Joined: 21 Sep 2005
Posts: 9
Location: Austin, TX

PostPosted: Sep 21st, 2005 03:07 PM    Post subject: winsock(AX not API) memory leak ? Reply with quote

i've been trying to create a multi-client/single server messenger for quite some time, and every time i run into the same damned problem.

the problem is not that i don't know how to code it, it's that when i have a client connected and the client sends 2 messages in a short period of time, they overlap eachother... heres an example:

the format of my message is as follows

[$timestamp/$command/$command_data]

no say they are sending to chat, which is the command.. and the data is the message; if they type too fast, or send a message twice i get:

[$timman$dat.... it's all compiled into one message, truncating part of each message they sent.

anyone have any clue as to why this happens.. or even what i'm talking about?

thanks
_________________
whoa.. it's me.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Sep 22nd, 2005 10:54 PM    Post subject: Reply with quote

Hi Johnny,

I suspect your problem may be that you're not waiting for the previous send to complete before allowing the client to issue another one.

The SendComplete event triggers when a message has been sent. It's like the opposite of DataArrival. I'm sure that there's a better way of doing it but I use something like:
Code:

Public boSent as boolean

Private Sub SendToServer(strMessage)
boSent = False
wsock.SendData strMessage
'
' SendComplete event sets boSent = True
' when the message has been sent
'
Do Until boSent = True
    Doevents
Loop
End Sub

Private Sub wsock_SendComplete()
boSent = True
End Sub

You do run the risk that if, for some reason, the SendData doesn't complete the Client is stuck in a loop until he / she takes some action so it may be more apprpriate to start a timer immediately before the Send and if the timer expires, timeout the send.eg
Code:

Public boSent as Boolean
Public boTimedOut as Boolean

Private Sub SendToServer(strMessage)
boSent = False
boTimedOut = False
Timer.Interval = 20000  ' This can be set at Design time or in Form_Load
Timer1.Enabled = True
wsock.SendData strMessage
'
' SendComplete event sets boSent = True
' when the message has been sent
'
Do Until boSent = True or boTimedOut = True
    Doevents
Loop
If boTimedOut = True then
    MsgBox "Timeout occurred on last transmission to Server - please reconnect"
    wsock.close
Timer.Enabled = False
End Sub

Private Sub Timer1_Timer()
boTimedOut = True
End Sub

Here the client will loop until either the data sends ok or 20 seconds has elapsed - whether 20 seconds is long enough or not, who knows. It depends on the amount of data being transmitted, speed of connection etc etc. In the example above, no recovery action has been attempted and the Client will be disconnected. I've yet to find a way of cancelling a transmission in progress other than disconnecting which seems a bit drastic if the timeout was due, say, to network congestion.

(That well known Law says that transmission completes exactly 1 microsecond after the timeout period has expired)

Of course, you should also have similar code in the server although you can be a bit more clever and assign a boSent flag for each Client and check it for True just before you send data to the client. That way, your server can process requests for other Clients whilst waiting for the message to this Client to complete.

Regards
Doug
_________________
If you can see the light at the end of the tunnel, it probably means there's a Train coming.
Back to top
View user's profile Send private message
johnny_skunk
Newbie


Joined: 21 Sep 2005
Posts: 9
Location: Austin, TX

PostPosted: Sep 24th, 2005 01:42 AM    Post subject: Reply with quote

cool dude, that does the trick. much appreciated.
_________________
whoa.. it's me.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
johnny_skunk
Newbie


Joined: 21 Sep 2005
Posts: 9
Location: Austin, TX

PostPosted: Sep 24th, 2005 01:59 AM    Post subject: Reply with quote

how would i establish several connections on the same port. one server, several clients?
_________________
whoa.. it's me.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Sep 24th, 2005 05:25 AM    Post subject: Reply with quote

Hi,

Here's one I made earlier.......

Code:

Dim intClients as Integer

Private Sub Form_Load()
'
' Initialise constants and establish the Listening Port
'
intClients = 0

wsock(0).Protocol = sckTCPProtocol
wsock(0).LocalPort = 1001
wsock(0).Listen
End Sub

Private Sub wsock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
'
' Someone is trying to connect
' Set-up a socket for them to communicate
' and accept the request
'
intClients = intClients + 1
Load wsock(intClients)
wsock(intClients).Protocol = sckTCPProtocol
wsock(intClients).LocalPort = 0
wsock(intClients).Accept requestID

< here you might send the new client a welcome message>
< or ask for credentials, or whatever>

End Sub


A word of explanation.....
You need to set-up the listening socket as the first in an arryay of sockets (ie set its Index property to 0)

When a connection request comes through you create another socket element for this connection and accept the request. intClients get incremented each time a socket is added so the next available client socket is intClient + 1.

Regards
Doug
_________________
If you can see the light at the end of the tunnel, it probably means there's a Train coming.
Back to top
View user's profile Send private message
johnny_skunk
Newbie


Joined: 21 Sep 2005
Posts: 9
Location: Austin, TX

PostPosted: Sep 25th, 2005 09:08 PM    Post subject: Reply with quote

it works, but i'm curious as to why you set the local port as 0? i've never seen that done before.
_________________
whoa.. it's me.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Sep 26th, 2005 12:44 AM    Post subject: Reply with quote

Hi,

Doesn't have to be zero, just different from the listening socket.My code was just an example I suppose if you were going to use it in anger you should use TCP well known port numbers where appropriate.

regards
Doug
_________________
If you can see the light at the end of the tunnel, it probably means there's a Train coming.
Back to top
View user's profile Send private message
ajani
Newbie


Joined: 24 Jun 2006
Posts: 2

PostPosted: Jul 27th, 2006 01:35 PM    Post subject: Reply with quote

Agreed with topic starter (from [new user link]). See also: [new user link], [new user link], [new user link]
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Communiqué All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic