|
-
September 29th, 2003, 10:46 AM
#1
winsock sending data problem
hello everybody,
i have a problem using winsock in VB.
i have develop a client server application using winsock but i'm facing a problem that is my server application only received data from client application after i have finish the process.
what i tryin to said here is, i have some function like :
private function_send1()
Call send_now()
end function
private function_send2()
Call Send_now()
end function
which will call the send_now() function.
in my send_now() function(), i have:
Private Function Send_now()
with FrmClient.sckClient(FrmClient.MaxCN)
if .State=SckConnected then
.SendData "~@" & name & "~~" & age & "~~" & ....& "~~" & FrmClient.Concurrent & "~~"
else
MsgBox "Error in Connection"
Exit Function
end if
end with
end Function
why my server application can not get the data sequantially.
it get the data at one time. it did not receive the data twice that is from function_send1() then function_send2().
it receive the data right after the function_send2(). why?
isn't it should receive according to order like 1 first the follow by 2.
but my winsock application receive it at one time.
how do i solve this problem?
thanks,
gechin
-
September 29th, 2003, 06:07 PM
#2
How are you calling the Send1 and Send2 functions? Do you have "DoEvents" in there? If you're not waiting for a return code before you call the Send2 function, its probably falling through without waiting for a response from the server.
Be nice to Harley riders...
-
September 30th, 2003, 12:38 AM
#3
hello Twodogs,
i call the send1 and send2 function by calling another function. like this:
Private Function process()
Call send1() '<--------here the data did not send
Call send2() '<--------the data(All) send here, why?
End Function
where in this send1 and send2, i will call the send_now() function.
i did not use the 'DoEvent' cause i don't know how to use it.
is it the soluction to my problem?
i wonder why the client did not send the data separally? it wrap it all up and then send them all together.
my sample data is like this:
~@gechin~~25~~....~~0~~ '<---here it should have send
but it send with the data from send2() which make my data look like this:
~@gechin~~25~~....~~0~~~@twodogs~~25~~....~~0~~
my VB application can only read the first data then this make my second data lost.
so how do i over come this problem? how do i make the client send the data one by one but no all at a time.
thanks,
gechin
-
September 30th, 2003, 01:03 AM
#4
I'm only guessing here, but because your commands are relatively close together (ie a microsecond apart) there might not have been time for the socket to actually send the first data before the second data arrived.
try:
Code:
Private Function process()
Call send1()
DoEvents
Call send2()
End Function
Be nice to Harley riders...
-
September 30th, 2003, 05:32 AM
#5
so Twodogs,
is that possible for me to delay the time before it execute the function send2()?
if i can delay/sleep the time, i think maybe......just maybe the first data can be receive by server and also the second data as well.
Private Function process()
Call send1()
'DoEvents
Delay(2000) 'milisecond
Sleep(2000) ' can i use something like this???
Call send2()
End Function
i will try and let you all know the progress. so wait for my result tomorrow afternoon.
thanks,
gechin
-
September 30th, 2003, 06:48 PM
#6
DoEvents should work - if it doesn't you might need to use a timer, or I think there is an event within winsock that you can use - something like StillSending or StillActive or something like that.
ie
While Winsock.StillActive DO
...
Loop
Be nice to Harley riders...
-
September 30th, 2003, 07:28 PM
#7
Had a similar problem a few years back.
The fix was the following.
From Function1 (or code) which called Winsock to send/receive data, I set a Boolean (called Done - as in 'Dim Done as Boolean') to false, and when the Function1 (or code) completed its Winsock data transfer, change the value to True, then I used code like this (from top of my head)
Code:
<Function1Name>'run winsock code, and set Done to True when code is finished
do ’start the loop
doevents
loop until Done = true ’exit only when the code above has completed and Done has changed to True
call <Function2Name> ’start the code in Function2
The Boolean variable Done is set to true when Winsock has done its stuff, and this exits the loop to commence Function2.
The DoEvents statement on it’s own will not hold a VB app until an ActiveX or a DLL had completed a task. These are usually multi-threading.
HTH
Jason.
-
September 30th, 2003, 09:09 PM
#8
That was what I was trying to get at...I just couldn't remember how I did it (ages & thousands of lines of code ago!) Thanks Jason!
Be nice to Harley riders...
-
October 1st, 2003, 05:57 AM
#9
twodogs,
i have try the DoEvents method. it can make my server receive data from function2.
but when i want to send more data, my server can not. it receive some and lost some of the data. what you have mention before is the processing time which are too short(milisecond).
twodogs and jason,
how do i use the method that jason have mention here?
should i place the 'Done' variable at the end of the :
.send "~@" & ....
Done = True <--------is this what you mean here?
how do i know when winsock have finish it's data transfer?
actually, i have a loop in each of the function to call the send data. the function1 and function2 did not call send data one time only. but they might call send many time(the most is 24 each).
example:
Private function Function1()
for i=0 then
send_now()
next i
end Fucntion
the same go for function2 and maybe more function like this too.
so where so i place this statement:
do ’start the loop
doevents
loop until Done = true ’exit only when the code above has completed and Done has changed to True
THANKS,
gechin
-
October 1st, 2003, 01:23 PM
#10
I think the best solution to your problem is to define a start and end token for each strings sent to your server.
This way, your server will be able to deal with more then one string received in the same event. And even put incomplete string in a temporary buffer in the case the end of a string is missing. (And that will happen if you have to send too many data at the same time)
Exemple : Start token is chr(1) and End token is chr(2) , or char that will never be present within your originals strings.
That's how i deal with the data in my sockets applications.
-
October 1st, 2003, 10:42 PM
#11
From getchin :
how do i know when winsock have finish it's data transfer?
Hi getchin, you can rely on the SendComplete event of the Winsock control. The following code use a module level flag variable just for this purpose..
Code:
Dim m_Sent As Boolean
Private Sub Winsock1_SendComplete()
m_Sent = True 'Set flag
End Sub
Private Sub SendSynch(Byval sData As String)
'Reset flag
m_Sent = False
'Send data
Winsock1.SendData sData
'Wait until sent
Do Until m_Sent
DoEvents
Loop
End Sub
Busy 
-
October 4th, 2003, 09:25 AM
#12
hello everybody,
sorry for the late reply cause this few days was busy.
i want to say thank you for all your reply. thanks
i have try the method that Thread1, you have show me.
i add the sckwinsock message handler:
Private Sub winsock1_SendComplete()
m_bSent = True 'Set flag
End Sub
i also add the code to my send() function, just like what you have show me:
Private Function Send()
'Reset flag
m_bSent = False
'Send data
Winsock1.SendData "~@" & name & "~~" & age & "~~"....
'Wait until sent
Do Until m_bSent = True
DoEvents
Loop
End Function
i declare my m_bSend variable as a public variable.
here is the poblem i face after.
if i call two function the result is ok cause the server able to receive all the data.
but when i call three function, the server only be able to receive two data only.
i think the server application did not have enough time to hear/receive data from the client application.
this is because, after received the data from the client, the server need to add this data to 6 database(*.mdb). the processing time maybe too long until the server application is still in the processing status when client is sending it's third data.
is my assumption correct?
Hello Heulsay, thanks for your reply too.
actually i was thinking to find the charather "~@" in the data that send by client.
i know if we want to find a char we can used "Mid" but that function are limited because we must know the length of the string first.
now, how do i find a char's position using VB?
i know in VC++ where a string function
find("char_that_we_want", start_of_str_index)
is there any function find in VB?
thanks,
gechin
-
October 5th, 2003, 09:37 PM
#13
is there any function find in VB?
Check out the INSTR() function - it brings back the position in the string of another string.
Be nice to Harley riders...
-
October 6th, 2003, 07:48 AM
#14
Yeah, use INSTR to find the the position of a string within another string. You may also need to check for the end token cause you don't want your server to work on incomplete string. (and the strings length seems to vary in your case)
-
October 7th, 2003, 12:19 PM
#15
hello everybody,
i have try the InStr(). now i can get the position char that i want.thanks for the help.
i want to ask how about my previous question regarding the receive data problem.
i still can not solve that problem.
maybe you guys can give me some idea?
thanks,
gechin
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|