🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

WinSock controls in VB

Started by
4 comments, last by MatrixCubed 22 years, 9 months ago
Hi all, I''ve been busy working on stuff, and managed to add VB to my skillset I''m trying to write client/server application in Visual Basic, and attempting to link these up using the Microsoft Winsock Control (6.0). The main stumbling block is that I cannot seem to successfully use WinSock.SendData more than once in the same sub/function, else the control (or TCP?) concatenates everything into one packet, then sends it all as one stream of data. I''m not certain that it''s the control that does this (though I have a sneaky suspicion)... can someone enlighten me on whether VB WinSock has a sort of "Flush" command that allows you to send data immediately so that the recipient may retrieve items individually without having to parse? I suppose the alternative is to compose a long string and separate with designated symbols, but I''m curious to know if there''s a way around that method. Ideas? MatrixCubed
http://MatrixCubed.org
Advertisement
This will work...

Sub SendStuff
With winsock
.Senddata "test1" & vbcrlf
.Senddata "test2" & vbcrlf
.Senddata "test3" & vbcrlf
.Senddata "test4" & vbcrlf
.Senddata "test5" & vbcrlf
End With
End sub

The vbcrlf does the job...

www.persistentrealities.com for Inline ASM for VB, VB Fibre, and other nice code samples in C++, PHP, ASP, etc.<br/>Play Yet Another Laser Game!<br/>
Hmmm, I actually found that adding vbCrLf literally added that character to the string, but did not actually break SendData items up... I would have had to parse through the transmitted string using vbCrLf as the InStr() delimiter.

Instead I found it easier to transmit first the size of the string, then the actual string. The receiver seems to really pick this up well.

  ''Client-sideWith tcpClient    .SendData WSM_LOGINREQUEST    .SendData Len(txtUsername.Text)    .SendData txtUsername.Text    .SendData Len(txtPassword.Text)    .SendData txtPassword.TextEnd With''Server-sideDim dwMessage as LongtcpServer(Index).GetData dwMessage, vbLongIf dwMessage = WSM_LOGINREQUEST Then    With frmServer.tcpServer(Index)        .GetData dwLength, vbLong        .GetData szUsername, vbString, dwLength        .GetData dwLength, vbLong        .GetData szPassword, vbString, dwLength    End WithEnd IF  


This seems to work perfectly. I don''t know how slow it is when the server has many active connections, but I will report my findings here, as there is a lack of much info on VB WinSock programming on the web.



MatrixCubed
http://MatrixCubed.org






The problem you are experiencing is not an issue with the Winsock control, it is an issue of the protocol you are using. TCP is a stream based protocol and does not support discreet messaging. If you want discreet messaging at the protocol level you must use UDP.

The TCP transport driver typically collects the data and sends them in larger packets.

Winsock also has its own internal buffer I believe so you can actually end up getting even large chunks of data.



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Hmm... or you could close the connection after every send. Although I wouldn''t recommend it though... it will be an UDP variant then, but slower.

btw, your system adds 8 bytes to every packet. If you can live with it, no problem... but with larger packets you might add lots of extra overhead.

So this gives me another idea.

Directplay uses a ''buffer'' method. Everything you sent is first added toa buffer (by yourself) using "AddDataToBuffer", and then you''ll sent it over the line. Thenk on the other side you''ll get the data from the byte array with the exact same type. So If the first item was a long, you''ve got to get the long first.
This method works well and could be worked out in pure vb code easily I believe (I used something similar for the winsock API webserver I made for my remote admin for my game)

For some more background information about this, check the code of my DirectPlay tutorial on http://www.vbexplorer.com/directx4vb
Might help you understand it a bit more, how this method works.

good luck =-)
www.persistentrealities.com for Inline ASM for VB, VB Fibre, and other nice code samples in C++, PHP, ASP, etc.<br/>Play Yet Another Laser Game!<br/>
Just a quick thought, try putting a DoEvents after the SendData call.

Trying is the first step towards failure.
Trying is the first step towards failure.

This topic is closed to new replies.

Advertisement