Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Question on sockets (newbie at it) in VB6. Using WinSock controls on a form. When I run the following code, the socket trying to connect never goes into a state of sckConnected (sits in sckConnecting status). I've checked the source/destination IP addresses and ports, and they are fine (listening on one port, trying to connect into that port from another). Testing on LocalHost (using the .LocalIPAddress to use the physical IP and not hitting the DNS).
The Code:
(Socket attempting connection - runs a function)
Public Function SockConnect(sckSend As Winsock, strAddr As String, intPort As Integer) As Boolean
Dim intWait As Integer
sckSend.Close
sckSend.LocalPort = 451
sckSend.Protocol = sckTCPProtocol
' Call sckSend.Bind(intPort, strAddr)
sckSend.RemoteHost = sckSend.LocalIP
sckSend.RemotePort = intPort
sckSend.Connect
Call Sleep(1000)
While sckSend.State <> sckConnecting And intWait < 5
DoEvents
Call Sleep(1000)
intWait = intWait + 1
Wend
Call WriteLog(gstrLogFile, "State is " & sckSend.State)
If intWait >= 5 Then
SockConnect = False
Else
If sckSend.State = sckConnected Then
SockConnect = True
Else
SockConnect = False
End If
End If
End Function(Socket Receiving Connection Request)
Private Sub sckListen_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If sckListen(Index).State <> sckClosed Then
intSockets = intSockets + 1
Load sckListen(intSockets)
sckListen(intSockets).Close
sckListen(intSockets).LocalPort = 450
sckListen(intSockets).Accept requestID
lblStatus = "Connection " & requestID & " accepted from " & sckListen lblStatus.Refresh
frmMain.Refresh
(intSockets).RemoteHostIP & ":" & sckListen(1).RemotePort & "."
Call WriteLog(gstrLogFile, lblStatus.Caption)
' MsgBox ("Accepted")
Else
MsgBox ("Socket Closed - Cannot Accept Connection.")
End IfEnd Sub
---
Help Greatly Appreciated
Joe

Figured it out with some more reading on the web.
(1) Upped the port on the accept
(2) Put in DoEvents all over the place
(3) Rebooted the PC to alleviate a 'ports-in-limbo' issue that seemed to keep coming up.Learning the socket stuff so hopefully I can apply it at work with some client/server apps that can someday directly communicate with eachother.
Joe

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |