View Full Version : Winsock problems
liquidchild
05-06-2002, 09:00 AM
Alright, I am still working on the coed for an instant messaging program, and I have run into a problem. After two computers connect using the winsock control on vb on tcp port 6122, if one sends two data items in a row,
for the client:
tcpClient.senddata login
tcpClient.senddata strUserFrom
and for the server:
tcpServer.getdata login
tcpServer.getdata strUserFrom
the result is that both variables sent are combined into the one getdata login. Can anyone tell me what is wrong and how to fix it.
liquidchild
05-10-2002, 08:00 PM
wow, you guys are a great help!
comrade
06-18-2002, 12:50 PM
WinSock.SendData and GetData have no way of knowing how to separate data, since all is sent at once. Perhaps you should do something like this:
tcpClient.senddata login & "!" & strUserFrom
Dim nPos As Long
tcpServer.getdata data
nPos = InStr(data, "!") - 1
login = Left(data, nPos - 1)
strUserFrom = Right(data, Len(data)-nPos)
Just make sure login does not contain "!" unless you change "!" to another separator.
LabelTN
05-16-2004, 11:08 PM
Or you can just set a maximum length for your variables on both sides, clients and server.
Like :
Dim login as String * 40 'for 40 chars.
Dim strUserFrom * 40
When you send datas or get it, the program will know that 40 character is the lenght of the vars. The on the server remove the empty chars, such as spaces and the end of the string. Because even if your text, for example in Login, is only 6 char long, it will be 6 chars of your text and 34 chars of space at the end. Just remove them using Right$ and Left$ methods.
For any numerical variables you don't need to do this, because the len is fixed by default.
For more precisions, just send me a private msg ;)
suprize
02-18-2005, 04:53 PM
the only problem with the fixed length method is its a waste of bandwidth. he said hes working on an instant messager? and you would want the messages to be at least 200 chars long so even when your sending messages that are 5 chars long, the packet would be 200. not very efficient...
ive found the best method is to preceed all your packets with a 32bit length. when you send the data...
Dim lLength as Long
lLength = Len(strData)
wskClient.SendData lLength & strData
then in the dataarrival...
Dim lLen as Long
wskServer.GetData lLen,vbLong,4
Dim strData as String
wskserver.GetData strData,vbString,lLen
and basically you loop it if theres more data left in the buffer. you would check that by looking at the length of wskServer.BytesReceived. tht will always reflect how much data is in the buffer.
now keep in mind that if you start getting incomplete packets youll need to check the buffer to make sure the full length of data is in there and if not then exit sub and wait for more data to arrive. but this should get you started...
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.