Solved

Winsock Buffering

Posted on 2001-07-14
9
451 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
ID: 6282960
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
ID: 6283181
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
ID: 6283882
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 3

Expert Comment

by:wpsjr1
ID: 6283885
btw, open the listening form before attempting to connect :)
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6285119
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
ID: 6285155
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
ID: 6285492
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
ID: 7153431
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
ID: 7182146
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

815 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

9 Experts available now in Live!

Get 1:1 Help Now