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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

winsock sckError!!

Status
Not open for further replies.

siuk

Programmer
Aug 23, 2001
38
GB
Hi im trying to get a simple Visual Basic client server message system to work using Winsock...

currently the client program uses the following code:
############################################################
Private Sub btnone_Click()
Winsock1.Close
Winsock1.RemoteHost = "127.0.0.1"
Winsock1.RemotePort = 1000

Winsock1.Connect

Do Until Winsock1.State = sckConnected

DoEvents: DoEvents: DoEvents: DoEvents
If Winsock1.State = sckError Then
MsgBox "Problem connecting!"
Exit Sub
End If
Loop
Winsock1.SendData (txtmessage.Text)
Winsock1.Close
End Sub

Private Sub Form_Unload(Cancel As Integer)
Winsock1.Close
End Sub
############################################################

and the server uses the following code:
############################################################
Private Sub Form_Load()
Winsock1.Close
Winsock1.LocalPort = 1000
Winsock1.Listen
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strIncoming As String
Winsock1.GetData strIncoming
MsgBox strIncoming
End Sub

Private Sub Form_Unload(Cancel As Integer)
Winsock1.Close
End Sub
############################################################

So I start the client program type my message and press the button to send it, the program sits there for a while a then displays the error message 'problem connecting!' as to say no connection has been made. However if I do a 'netstat-a' in dos, while the client it sitting around, this is what happens:

TCP: ME:1000 ME.co.uk:3515 CLOSE_WAIT
TCP: ME:3515 ME.co.uk:1000 FIN_WAIT_2

Please help me to get my program to work!!!

cheers



 
Add two form level variables (and initialise then to false in form load):

Private bConnected As Boolean
Private bError As Boolean

Amend your procedure:

Private Sub btnone_Click()
Winsock1.Close
Winsock1.RemoteHost = &quot;127.0.0.1&quot;
Winsock1.RemotePort = 1000

Winsock1.Connect

Do Until bConnected or bError
DoEvents
Loop

if bError then
' Do whatever
end if

Winsock1.SendData (txtmessage.Text)
Winsock1.Close
End Sub

Find the following events and add the code:

Private Sub Winsock1_Connect()
bConnected = True
End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

bError = True
End Sub


I haven't tested it, but I have used this alot.

Madlarry
 
I agree with MadLarry, put code in the Connection event which runs when the connection has been made.

I'm not convinced that the SendData code should be btNone_Click in MadLarrys code - that DoEvent loop is pretty inefficient and unnecessary to boot.

Have a function (SendData) which buffers what you want to send to the server and then do the send in the Connection event of the Winsock control.

I'd do an example, but its tired and I'm going home.

Chaz
 
Im still not able to get this to work, any chance of a working example??? plz plz :)

cheers

 
hi, ok i did get it working, however im only able to send one message then the program displayes problem connecting... any ideas?
 
Like scorpio66 said, create separate procedures for the connection and senddata functions (ie don't have the close, connect and senddata in your btnone_Click procedure)

When you close the connection you have to wait for it to finish, here is an example of a close function...

Private Sub Winsock1_Close()
Dim LStartTime As Date

LStartTime = Now()

' Close the connection
Winsock1.Close

' Wait until the status is closed
Do Until Winsock1.State = sckClosed
Debug.Print &quot;Closing..... State: &quot; & Winsock1.State
DoEvents
If DateDiff(&quot;s&quot;, LStartTime, Now) > 60 Then
Err.Raise vbObjectError, &quot;CCommsServer&quot;, &quot;Request Timeout&quot;
End If
Loop

End Sub


Cheers,

Madlarry

 
si,

One thing to be aware of when using the winsock control - you really must take into account the event driven nature of TCP/IP communications.

For example, when you use the Close method you will receive a SocketClosed event when that action is complete. When you send data you will get a SendComplete event when the send is successful, etc.

You MUST use these. To implement timeouts I would use a separate timer control. For example

Timer1.Interval = 60
Winsock1.SendData &quot;MyData&quot;
Timer1.Enabled = True

Sub Winsock1_SendComplete
Timer1.Enabled=False
End Sub

Sub Timer1_Timer
MsgBox &quot;Socket timed out&quot;
End Sub

Do not put DoEvents loops in your code as this will take up all of your CPU.

Chaz
 
ok thanks guys, but i still cant get it to work correctly even with the timer, so im gonnna try it again using 2 forms? the first one is where you can enter the remote ip and remote port, then the second to connect to the remote computer?

what should i use instead of the DoEvents because the program stalled without them!

cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top