Solved

Winsock Buffering

Posted on 2001-07-14
9
440 Views
Last Modified: 2013-11-13
I am making a little version of pong, but enabling it for the net. They way I have made the net code is that every time your paddle is moved, the GetData method of Winsock is called, sending the paddles Top property to the server. The problem I have is that the server recieves several values at once, like 200 200 200 200 220 220 220. When what I want is that on data arrival to be is that it recieves 200 by itself and then 220 in a seperate arrival. Instead, Winsock seems to buffer the values being sent together. What I have done to get round is to take the first value from this string and use that, the problem with this is that the movement is very jumpy. What I am after is two simple programs, one a server program and one a client program. I want the programs to connect and when the up key or down key is pressed on the client program, the block moves and sends its position to the server through the Winsock, and the block on the server side moves SMOOTHLY. Anything similar to this will be fine.
0
Comment
Question by:willa2
9 Comments
 
LVL 3

Expert Comment

by:wpsjr1
Comment Utility
Using Winsock API its possible to disable winsocks way of combining packets.  You'd call setsockopt with TCP_NODELAY and Winsock1.SocketHandle.  I'm not certain if its possible with the Winsock control, but its worth a try.

Const TCP_NODELAY = 1

0
 

Author Comment

by:willa2
Comment Utility
Well I was wondering if it would be possible to use DirectX, as I have been reading about how it can use DirectPlay to make net connections
0
 
LVL 3

Accepted Solution

by:
wpsjr1 earned 100 total points
Comment Utility
DX may be possible, I've never used it.  I did however verify that the Winsock control can handle TCP_NODELAY.  This will allow the client to send packets immediately.  You should still check for multiple packets sent, just in case.

Here is a little sample, so you can see how this works:
(add a timer, command button {named cmdStart} and a winsock control to the Client form)

' begin Client code

Option Explicit

Private Declare Function setsockopt Lib "wsock32.dll" (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, ByVal optlen As Long) As Long

Const IPPROTO_TCP = 6
Const TCP_NODELAY = 1

Private Sub cmdStart_Click()
  Dim x As Long

  Winsock1.LocalPort = 0 ' get a new available port, keeps from having "Address In Use" error
  Winsock1.Connect "127.0.0.1", 100 ' connect to myself on port 100
  x = setsockopt(Winsock1.SocketHandle, IPPROTO_TCP, TCP_NODELAY, 1, 4) ' disable the norgle algo, allow for small packets
  Debug.Print x & " <- setsockopt" ' 0 = succeeded
  Timer1.Enabled = True ' start sending
End Sub

Private Sub Form_Load()
  Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()
  If Winsock1.State = 7 Then Winsock1.SendData "A"
End Sub


' ___________________________________________________

'( add a command button {named cmdListen} and a winsock control to the server form)

' begin Server code

Option Explicit

Dim sBuffer As String

Private Sub cmdListen_Click()
  Dim x As Long
 
  Winsock1.LocalPort = 100
  Winsock1.listen
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  Winsock1.Close
  Winsock1.accept requestID
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Winsock1.GetData sBuffer
 
  Debug.Print sBuffer & vbCrLf
End Sub



0
 
LVL 3

Expert Comment

by:wpsjr1
Comment Utility
btw, open the listening form before attempting to connect :)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 4

Expert Comment

by:VincentLawlor
Comment Utility
Ok you know the size of the packets coming back from the socket whay don't you just parse the packets out it will make things a lot smoother for you.

As an example

Your data arrival from the winsock is a asynchronous i.e can happen at any time.

Add a timer to your program.

Create a public collection for your values (this will form queue for the data).

Add a function Parse

'This will be a recursive call
Function Parse(strInput as String) as boolean

   Dim strTemp1 as String
   Dim strTemp2 as String
   
   strTemp1 = Mid(strInput,1,3)
   colData.Add strTemp1
   strTemp2 = Mid(strInput,4)

   if Len(strTemp2) > 3 then




end Function





 
0
 
LVL 4

Expert Comment

by:VincentLawlor
Comment Utility
Ok you know the size of the packets coming back from the socket whay don't you just parse the packets out it will make things a lot smoother for you.

As an example

Your data arrival from the winsock is a asynchronous i.e can happen at any time.

Add a timer to your program.

Create a public collection for your values (this will form queue for the data).

Add a function Parse

'This will be a recursive call
Sub Parse(strInput as String)

   Dim strTemp1 as String
   Dim strTemp2 as String

   strTemp1 = Mid(strInput,1,3)
   colData.Add strTemp1
   strTemp2 = Mid(strInput,4)

   if Len(strTemp2) > 3 then
       Parse(strTemp2)
   end if

end Function

When data arrives from the socket call this Sub on the data it will add the data to your queue.

Now you need to pull the data off the queue you can do this on a FIFO basis from your program.

You can do this in your timer control or wherever you were originally calling your movement code from.

strData = colData.Item(1)
colData.Remove(1)

strData is the piece of data you want to work with.

This means your data will be arriving as quickly as possible and you should get a nice smooth movement.

Vin.
 
0
 
LVL 4

Expert Comment

by:VincentLawlor
Comment Utility
You could also delimit your data transmission with a , at the end of each packet you send i.e. Winsock1.Send "200,"
and use Split to parse the packets. Using this you can transmit 200,2500,3000, etc.



e.g

Sub Parse(strTemp)


   Dim strValues()  
   
   strValues = Split(strTemp, ",")

   for each strVal in strValues  
       if strVal <> "" 'If each value is delimited with "," then you will get a blank one at the end of the array
           colData.Add strVal
       end if
   next strVal

end sub

Vin.
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Hi willa2,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: wpsjr1 and VincentLawlor@devx

willa2, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Per recommendation, force-accepted.

Netminder
CS Moderator

VincentLawlor: points for you at http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20328906
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now