Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error 40006 ?

Status
Not open for further replies.

Diginight

Programmer
Jan 28, 2005
5
US
Hi everyone,
I'm starting on coding with winsock control and i've been studying and working this this client and server chat program example. The problem I am having is when I run the server for the first time everything works great. Client is able to connect to the server with no problem until the client until the user disconnects from the server. When the user tries to reconect to the server it seems to connect to server until the client tries to send data to the server and I get a run-time error 40006 "Wrong protocol or connection state for the request transaction or request". I then have to restart the server so it would accept incoming connections again. Am I missing something?
I read some where about using an array but not sure how to approach this. Here is the code I am working with. Any suggestions for pointers will be great.

Option Explicit


'It makes server ready to listen client request to
'Establish connection.
Private Sub Form_Load()
Dim s As String
'Enter in inputBox the service no. it can be any valid string.
'eg : 1234
s = InputBox("Enter the port to use (eg: 1234)")
tcpServer.LocalPort = s 'This service no. is assigned to local port
tcpServer.Listen 'Listen method makes server ready to listen clients request
End Sub

'It is used to set default focus to textbox.
'As unless form is visible it cannot set focus to any of its control
'and so i have used Activate event to set textbox focus
Private Sub Form_Activate()
'to set default focus to textbox
txtmsg.SetFocus
End Sub

'Event handles request made by client, that is each time client made
'request this event occurs.
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
'check for tcpserver state if it is NOT CLOSED then accept request
If tcpServer.State <> sckClosed Then tcpServer.Close

'Accepts request made by client.
tcpServer.Accept requestID
lstMsg.AddItem "Client Connected" 'for displaying connected message...
End Sub

'Whenever data is recieved this event occurs.
'Thus we can retrieve data just by invoking "GetData" method within it.
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
Dim strdata As String
tcpServer.GetData strdata 'storing recieved data in strdata
spk.Speak strdata
lstMsg.AddItem strdata 'displaying this data by adding it into listbox
End Sub


'command btn when pressed will send text message to client
'Its default property is set to True (which means its code
'will executed on pressing enter that is here will send data
'on just pressing enter.)
Private Sub cmdSend_Click()
Dim strdata As String
strdata = "Server : " & txtmsg.Text
tcpServer.SendData strdata 'SendData method is used to send data.
lstMsg.AddItem strdata 'adding send data into listbox to display it.
txtmsg.Text = "" 'Making textbox clear
End Sub


'Event occur when trying to close form
Private Sub Form_Unload(Cancel As Integer)
tcpServer.Close
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top