I have a simple need for a winsock. The program sends a message to a user, and the users machine processes it, not big thing. Problem is the first message goes through without a problem, but when I try to send additional messages, the machine that should do the receiving stops listening. Any help would be appreciated.
Problem: Sends on message fine, but then it stops working.
I've Tested: Sending machine does loop through address, but always stays at state 6 which is connecting, it never connects. Receiving machine' winsock1_connectionRequest never gets triggered. And the state of winsock1 doesn't go back to listening (2)
Any assistance would be greatly appreciated.
Here's the code for the machine that would do the listening:
'form_load calls init winsock to setup inital settings
Private Sub InitWinsock()
Winsock1.LocalPort = 1008
Winsock1.Listen
Debug.Print Winsock1.state & "init"
End Sub
Private Sub Winsock1_Close()
Debug.Print "winsock closing" & vbCrLf
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
On Error GoTo log_err
If Winsock1.state <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
Debug.Print Winsock1.state & vbCrLf
Exit Sub
log_err:
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strdata As String
On Error GoTo log_err
Winsock1.GetData strdata, vbString
ProccessData strdata
Debug.Print "dataarrived" & strdata & vbCrLf
Debug.Print Winsock1.state
Exit Sub
log_err:
End Sub
Here's the code for sending:
Public Sub AnnounceToNetwork(thedata As String)
Dim i As Integer
Dim x As Long
For i = 0 To UBound(SUPERIPS) 'superips hold a list if ipaddress
Do While Not CloseConnect ' wait for data to be sent before
closing
DoEvents: DoEvents: DoEvents: DoEvents
Loop
winsock.Close
winsock.RemoteHost = SUPERIPS(i)
winsock.RemotePort = 1008
winsock.Connect
Do Until winsock.state = sckConnected Or x > 1000
'only want to try for a short amout of time to connect
x = x + 1
DoEvents: DoEvents: DoEvents: DoEvents
Loop
If winsock.state = sckConnected Then 'send if connected
CloseConnect = False
thedata = thedata & "|" & GetXtension(winsock.LocalHostName) & "|" & Time$
winsock.SendData thedata
End If
x = 0
Next
End Sub
Private Sub winsock_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)
CloseConnect = True
End Sub
Private Sub winsock_SendComplete()
CloseConnect = True
winsock.Close
End Sub
When Winsock closes, it stops listening (actually, probably when it accepts the connection). You have to tell it to start listening again.
Josh
Get these: MozillaOperaOpenBSD
I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.
Originally posted by JoshT When Winsock closes, it stops listening (actually, probably when it accepts the connection). You have to tell it to start listening again.
You are correct. When the server uses only one socket, it has to go from listening to connected. Then when it sees the close from the client, it has to go back to the listening state.
I am attaching a small client/server demo that I put together to help with a number of issues like this and passing UDTs. Both the client and server are small, but are heavily commented. As I get the time, I will add more error checking, but I feel that it is a better example than the one found on this site which has the socket being closed immediately after the send. That code has caused a lot of forum members a lot of grief.
I would like to see the app but at the moment the link is broken and I wondered if you get this message, could you please mail me the app. at [email protected] if you don't mind please.
The outline of the problem I have is that when I send a file which is 300 bytes long, Winsock works no problem but when the reply from that send comes back and I resend the file which is now 600 bytes long, Winsock sits in the Listening position even though I haven't closed it inbetween the file first being sent and the retransmission of the file as 600 bytes...
I am new to Winsock so I need some pointers and I am hoping your app will help me out.
Thanks for the reply..
"Three minutes thought would suffice to find this out; but thought is irksome and three minutes is a long time."
"The supreme irony of life is that hardly anyone gets out of it alive."