|
-
Feb 6th, 2006, 05:05 PM
#1
Thread Starter
New Member
No DataArrival?
Hi i am new to visual basic..I have some knowledge about the winsocks in vbasic and i have succesfully made a chat program..Why am i saying all this?
Because i can't explain the problem i have..Ok here goes:
I have made a winsocket that listens to a specific port.The client can succesfully connect and i can see that when i accept the connection request..But when i try to receive the data the client sends i receive nothing, that is the DataArrival is never called..Here is the code part.I can't figure out what's the problem because i use the same code i used in my chat program
VB Code:
Winsock23.LocalPort = 23
Winsock23.Listen
Private Sub Winsock23_ConnectionRequest(ByVal requestID As Long)
On Error Resume Next
'Now accept the Request
Winsock23.Accept requestID
txtEvent.Text = txtEvent.Text & vbCrLf & " connected"
End Sub
Private Sub Winsock23_DataArrival(ByVal bytesTotal As Long)
Dim str As String
MsgBox str
Winsock23.GetData str, vbString
End Sub
The msbox should at least display but nothing happens which means that i don't receive anything
For the client i use
VB Code:
Winsock1.Connect txtIp.Text, 23
Winsock1.SendData "Test"
I don't think there's a client problem for not receiving anything right?
Anyway i would really appreciate any help because i am on the finishing touches of my project and i am really stuck
Last edited by MyTHoS_; Feb 6th, 2006 at 05:10 PM.
-
Feb 6th, 2006, 06:38 PM
#2
Re: No DataArrival?
You have 2 mistakes.
Regarding this code:
VB Code:
Private Sub Winsock23_ConnectionRequest(ByVal requestID As Long)
On Error Resume Next
Winsock23.Accept requestID
txtEvent.Text = txtEvent.Text & vbCrLf & " connected"
End Sub
You are accepting the connection on the Listening socket, and you can't do that...
You have to Close the Listening state, THEN Accept the connection.
On top of that you have "On Error Resume Next" wich will ignore the error that you should get in this case (accepting the connection while in Listening state)
So, you should change your code to this:
VB Code:
Private Sub Winsock23_ConnectionRequest(ByVal requestID As Long)
[b]Winsock23.Close[/b]
Winsock23.Accept requestID
txtEvent.Text = txtEvent.Text & vbCrLf & " connected"
End Sub
Next mistake is on the client side...
You send the data right after you give the Connect command....
It takes time to connect, and if you send data BEFORE it's actually connected, it won't send anything.
You should send data AFTER you receive the "Connect" winsock event.
-
Feb 6th, 2006, 06:47 PM
#3
Re: No DataArrival?
Just spoted a 3'rd mistake:
VB Code:
Private Sub Winsock23_DataArrival(ByVal bytesTotal As Long)
Dim str As String
MsgBox str
Winsock23.GetData str, vbString
End Sub
If you want to show a message with the data you receive on the server, I sugest to show the message AFTER you read the data... like this:
VB Code:
Private Sub Winsock23_DataArrival(ByVal bytesTotal As Long)
Dim str As String
[b]Winsock23.GetData str, vbString ' read the data FIRST
MsgBox str ' THEN display it[/b]
End Sub
-
Feb 6th, 2006, 07:08 PM
#4
Thread Starter
New Member
Re: No DataArrival?
Thank you very much man!I corrected the other mistakes before your post..The problem was on the " on Error resume next and the fact that i didn't close the winsock.Thank you very much!
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
|