Winsock32, Connect fails to connect to a peer


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Winsock32, Connect fails to connect to a peer

  1. #1
    Chris.G Guest

    Winsock32, Connect fails to connect to a peer


    There,
    This is a module in my TCP/IP class which basically
    uses the wsock32 API to make a connection oriented
    socket (Type SOCK_STREAM).

    All the following works in initiating a connection.
    1)Initialize Winsock API
    2)Create new socket
    3)Bind socket to LocalHostIP
    4)Bind socket to RemoteHost

    After performing all these steps when I try to use the Connect function to
    connect to the remote host, the function
    returns the value SOCKET_ERROR.
    ___________________________________________________________
    Connect portion of the code.
    WSAResult = connect(mlngSocket, msaRemoteAddr, Len (msaRemoteAddr))

    If (WSAResult = SOCKET_ERROR) Then
    SetLastErrorCode "Error in OpenConnection::connect"
    Else
    OpenConnection = True
    End If
    ____________________________________________________________

    When I check the return value of WSAGetLastError it is 0, I
    I am unable to determine the cause of the failure as both
    the server are located in the same domain.

    1)Do the same program needs to run as well as in the peer server(parallely)
    to which the
    connection is made?
    2)Do I need to do anything else besides these steps?.

    Like someone told me that I need code for the listening
    socket???. If that is the case where does the code for the
    listening socket should exist, in the client socket program
    or sever socket Program?.


    Can anyone tell me what would be the reason my conection fails.

    Kindest regards,
    Chris.G

    This is the function
    --------------------
    Public Function OpenConnection() As Boolean
    Dim WSAResult As Long
    OpenConnection = False

    'Initialize Winsock API
    WSAResult = WSAStartup(&H101, mwsaData)
    If WSAResult <> WSANOERROR Then
    SetLastErrorCode "Error OpenConnection::WSAStartup"
    Exit Function
    End If

    'Create new socket
    mlngSocket = socket(PF_INET, SOCK_STREAM, 0)
    If (mlngSocket = INVALID_SOCKET) Then
    SetLastErrorCode "Error in OpenConnection::socket"
    Exit Function
    End If

    'Bind socket to LocalHostIP
    msaLocalAddr.sin_family = PF_INET
    msaLocalAddr.sin_port = 0
    msaLocalAddr.sin_addr.S_addr =
    inet_addr(mstrLocalHostIP)
    If (msaLocalAddr.sin_addr.S_addr = INADDR_NONE) Then
    SetLastErrorCode "Error OpenConnection::inet_addr"
    Exit Function
    End If
    WSAResult = bind(mlngSocket, msaLocalAddr, Len
    (msaLocalAddr))

    If (WSAResult = SOCKET_ERROR) Then
    SetLastErrorCode "Error in OpenConnection::bind"
    Exit Function
    End If

    'Connect with remote host
    msaRemoteAddr.sin_family = PF_INET
    msaRemoteAddr.sin_port = htons(mlngRemotePort)
    msaRemoteAddr.sin_addr.S_addr = inet_addr(mstrRemoteHostIP)

    If (msaLocalAddr.sin_addr.S_addr = INADDR_NONE) Then
    SetLastErrorCode "Error
    OpenConnection::inet_addr=INADDR_NONE"
    Exit Function
    End If

    msaRemoteAddr.sin_zero(0) = 0
    WSAResult = connect(mlngSocket, msaRemoteAddr, Len
    (msaRemoteAddr))

    If (WSAResult = SOCKET_ERROR) Then
    SetLastErrorCode "Error in OpenConnection::connect"
    Else
    OpenConnection = True
    End If
    End Function


  2. #2
    Bob Butler Guest

    Re: Winsock32, Connect fails to connect to a peer

    "Chris.G" <chrisg@apdomail.sgp.hp.com> wrote in message
    news:3a0f67f7$1@news.devx.com...
    <cut>
    > When I check the return value of WSAGetLastError it is 0


    VB does other API work and that changes that value -- use Err.LastDLLError
    to get the value you are looking for.

    > 1)Do the same program needs to run as well as in the peer

    server(parallely)
    > to which the
    > connection is made?


    The peer must be listening on the remote port you are trying to connect to.
    It does not necessarily have to be the same application.





  3. #3
    Bob Butler Guest

    Re: Winsock32, Connect fails to connect to a peer

    "Chris.G" <chrisg@apdomail.sgp.hp.com> wrote in message
    news:3a0f67f7$1@news.devx.com...
    <cut>
    > When I check the return value of WSAGetLastError it is 0


    VB does other API work and that changes that value -- use Err.LastDLLError
    to get the value you are looking for.

    > 1)Do the same program needs to run as well as in the peer

    server(parallely)
    > to which the
    > connection is made?


    The peer must be listening on the remote port you are trying to connect to.
    It does not necessarily have to be the same application.





  4. #4
    Michael Guest

    Re: Winsock32, Connect fails to connect to a peer


    Hi Chris...

    I'm using two PCs - one for the client and one for the server. I'm also using
    ws2_32.dll. The server must be running and listening. Feel free to send me
    email if you have any questions...

    Michael

    My WSA client:

    'all WSA init functions have been performed successfully...
    lSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)
    lNetAddr = inet_addr(REMOTE_ADDRESS) 'app specific const
    lPort = htons(REMOTE_PORT) 'another app specific const
    tSockAddrIn.sin_family = AF_INET
    tSockAddrIn.sin_addr = lNetAddr
    tSockAddrIn.sin_port = lPort
    lRet = connect(lSocket, tSockAddrIn, LenB(tSockAddrIn))

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'disconnect the socket
    Call shutdown(lSocket, SD_BOTH)
    Call closesocket(lSocket)
    Exit Function
    End If

    My WSA server:

    'on startup, after WSA init functions...
    'find out who we are
    sLocalHost = String(256, 32)
    Call gethostname(sLocalHost, Len(sLocalHost))
    sLocalHost = Left(sLocalHost, InStr(sLocalHost, Chr(0)) - 1)

    lHost = gethostbyname(sLocalHost)
    ' convert to network ordering.
    lLocalPort = htons(SAP_PORT) 'app const

    If lHost <> 0 Then
    MemCopy tHost, ByVal lHost, LenB(tHost)
    MemCopy lLocalHost, ByVal tHost.h_addr_list, 4
    MemCopy lLocalIP, ByVal lLocalHost, tHost.h_length
    Else
    ' translate the error.
    sLogDesc = GetWinsockErrorText(WSAGetLastError())

    'log the error
    Output sLogDesc

    Exit Sub
    End If

    'I have a listen command button..on click
    'create a socket
    lLocalSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)

    If lLocalSocket = INVALID_SOCKET Then
    ' translate the error.
    sLogDesc = GetWinsockErrorText(WSAGetLastError())

    'log the error
    Output sLogDesc & vbCrLf

    Exit Function
    End If

    'bind to our local address
    tSockAddr.sin_family = AF_INET
    tSockAddr.sin_addr = lLocalIP
    tSockAddr.sin_port = lLocalPort
    lRet = bind(lLocalSocket, tSockAddr, LenB(tSockAddr))

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    'put socket into listen mode
    Output "*** Listening" & vbCrLf
    lRet = listen(lLocalSocket, SOMAXCONN)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    'get a handle to internal winsock event for asynch mode
    lLocalEvent = WSACreateEvent()

    If lLocalEvent = WSA_INVALID_EVENT Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    ' specify what events we want
    lRet = WSAEventSelect(lLocalSocket, lLocalEvent, FD_ACCEPT)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    ' loop and wait for the events
    bExit = False
    Do
    DoEvents
    lRet = WSAEnumNetworkEvents(lLocalSocket, lLocalEvent, tWSAEvent)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    If Not bExit Then
    Output sLogDesc & vbCrLf
    DisconnectSocket lLocalSocket
    End If
    Exit Function
    Else
    Select Case tWSAEvent.lNetworkEvents
    Case FD_ACCEPT
    Call AcceptSocket

    End Select
    End If
    Loop Until bExit

    'the accept socket routine...
    lSockAddrLen = LenB(tSockAddr)
    lRemoteSocket = accept(lLocalSocket, tSockAddr, lSockAddrLen)

    If lRemoteSocket = INVALID_SOCKET Then
    ' translate the error.
    sLogDesc = GetWinsockErrorText(WSAGetLastError())

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    ' who did we connect to? convert to host ordering.
    sRemoteIP = String(32, 32)
    lRet = inet_ntoa(tSockAddr.sin_addr)
    If lRet > 0 Then
    lLen = lstrlen(lRet)
    If lLen > 32 Then lLen = 32
    MemCopy ByVal sRemoteIP, ByVal lRet, lLen
    sRemoteIP = Left(sRemoteIP, lLen)
    Else
    sRemoteIP = "255.255.255.255"
    End If

    sRemotePort = Str(ntohs(tSockAddr.sin_port))

    Output "Accepted connection from " & sRemoteIP & ":" & sRemotePort & vbCrLf

    'get a handle to internal winsock event for asynch mode
    lRemoteEvent = WSACreateEvent()

    If lRemoteEvent = WSA_INVALID_EVENT Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lRemoteSocket
    Exit Function
    End If

    ' specify what events we want
    lRet = WSAEventSelect(lRemoteSocket, lRemoteEvent, FD_READ Or FD_CLOSE)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lRemoteSocket
    Exit Function
    End If

    bRemoteExit = False
    cmTerminate.Enabled = True

    ' loop and wait for the events
    Do
    DoEvents
    lRet = WSAEnumNetworkEvents(lRemoteSocket, lRemoteEvent, tWSAEvent)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lRemoteSocket
    Exit Function
    Else
    Select Case tWSAEvent.lNetworkEvents
    Case FD_READ
    'incoming data
    ReadSocket lRemoteSocket

    Case FD_CLOSE
    'terminated by remote client
    DisconnectSocket lRemoteSocket
    CloseEvent lRemoteEvent
    sLogDesc = "Connection closed by remote client"
    Output sLogDesc & vbCrLf
    Exit Do

    End Select
    End If
    Loop Until bRemoteExit

    DisconnectSocket lRemoteSocket
    CloseEvent lRemoteEvent

    'and some other helpers
    Private Sub DisconnectSocket(lSocket As Long)

    On Error Resume Next

    If lSocket = INVALID_SOCKET Then Exit Sub

    Call shutdown(lSocket, SD_BOTH)
    Call closesocket(lSocket)
    lSocket = INVALID_SOCKET

    On Error GoTo 0

    End Sub

    Private Sub CloseEvent(lEvent As Long)

    On Error Resume Next

    If lEvent = WSA_INVALID_EVENT Then Exit Sub

    Call WSACloseEvent(lEvent)
    lEvent = WSA_INVALID_EVENT

    On Error GoTo 0

    End Sub

    "Chris.G" <chrisg@apdomail.sgp.hp.com> wrote:
    >
    > There,
    > This is a module in my TCP/IP class which basically
    > uses the wsock32 API to make a connection oriented
    > socket (Type SOCK_STREAM).
    >
    > All the following works in initiating a connection.
    > 1)Initialize Winsock API
    > 2)Create new socket
    > 3)Bind socket to LocalHostIP
    > 4)Bind socket to RemoteHost
    >
    >After performing all these steps when I try to use the Connect function

    to
    >connect to the remote host, the function
    >returns the value SOCKET_ERROR.
    >___________________________________________________________
    >Connect portion of the code.
    >WSAResult = connect(mlngSocket, msaRemoteAddr, Len (msaRemoteAddr))
    >
    >If (WSAResult = SOCKET_ERROR) Then
    > SetLastErrorCode "Error in OpenConnection::connect"
    >Else
    > OpenConnection = True
    >End If
    >____________________________________________________________
    >
    >When I check the return value of WSAGetLastError it is 0, I
    >I am unable to determine the cause of the failure as both
    >the server are located in the same domain.
    >
    >1)Do the same program needs to run as well as in the peer server(parallely)
    >to which the
    > connection is made?
    >2)Do I need to do anything else besides these steps?.
    >
    > Like someone told me that I need code for the listening
    > socket???. If that is the case where does the code for the
    > listening socket should exist, in the client socket program
    > or sever socket Program?.
    >
    >
    >Can anyone tell me what would be the reason my conection fails.
    >
    >Kindest regards,
    >Chris.G
    >
    >This is the function
    >--------------------
    > Public Function OpenConnection() As Boolean
    > Dim WSAResult As Long
    > OpenConnection = False
    >
    > 'Initialize Winsock API
    > WSAResult = WSAStartup(&H101, mwsaData)
    > If WSAResult <> WSANOERROR Then
    > SetLastErrorCode "Error OpenConnection::WSAStartup"
    > Exit Function
    > End If
    >
    > 'Create new socket
    > mlngSocket = socket(PF_INET, SOCK_STREAM, 0)
    > If (mlngSocket = INVALID_SOCKET) Then
    > SetLastErrorCode "Error in OpenConnection::socket"
    > Exit Function
    > End If
    >
    > 'Bind socket to LocalHostIP
    > msaLocalAddr.sin_family = PF_INET
    > msaLocalAddr.sin_port = 0
    > msaLocalAddr.sin_addr.S_addr =
    > inet_addr(mstrLocalHostIP)
    > If (msaLocalAddr.sin_addr.S_addr = INADDR_NONE) Then
    > SetLastErrorCode "Error OpenConnection::inet_addr"
    > Exit Function
    > End If
    > WSAResult = bind(mlngSocket, msaLocalAddr, Len
    >(msaLocalAddr))
    >
    > If (WSAResult = SOCKET_ERROR) Then
    > SetLastErrorCode "Error in OpenConnection::bind"
    > Exit Function
    > End If
    >
    > 'Connect with remote host
    > msaRemoteAddr.sin_family = PF_INET
    > msaRemoteAddr.sin_port = htons(mlngRemotePort)
    > msaRemoteAddr.sin_addr.S_addr = inet_addr(mstrRemoteHostIP)
    >
    > If (msaLocalAddr.sin_addr.S_addr = INADDR_NONE) Then
    > SetLastErrorCode "Error
    > OpenConnection::inet_addr=INADDR_NONE"
    > Exit Function
    > End If
    >
    > msaRemoteAddr.sin_zero(0) = 0
    > WSAResult = connect(mlngSocket, msaRemoteAddr, Len
    > (msaRemoteAddr))
    >
    > If (WSAResult = SOCKET_ERROR) Then
    > SetLastErrorCode "Error in OpenConnection::connect"
    > Else
    > OpenConnection = True
    > End If
    >End Function
    >



  5. #5
    Michael Guest

    Re: Winsock32, Connect fails to connect to a peer


    Hi Chris...

    I'm using two PCs - one for the client and one for the server. I'm also using
    ws2_32.dll. The server must be running and listening. Feel free to send me
    email if you have any questions...

    Michael

    My WSA client:

    'all WSA init functions have been performed successfully...
    lSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)
    lNetAddr = inet_addr(REMOTE_ADDRESS) 'app specific const
    lPort = htons(REMOTE_PORT) 'another app specific const
    tSockAddrIn.sin_family = AF_INET
    tSockAddrIn.sin_addr = lNetAddr
    tSockAddrIn.sin_port = lPort
    lRet = connect(lSocket, tSockAddrIn, LenB(tSockAddrIn))

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'disconnect the socket
    Call shutdown(lSocket, SD_BOTH)
    Call closesocket(lSocket)
    Exit Function
    End If

    My WSA server:

    'on startup, after WSA init functions...
    'find out who we are
    sLocalHost = String(256, 32)
    Call gethostname(sLocalHost, Len(sLocalHost))
    sLocalHost = Left(sLocalHost, InStr(sLocalHost, Chr(0)) - 1)

    lHost = gethostbyname(sLocalHost)
    ' convert to network ordering.
    lLocalPort = htons(SAP_PORT) 'app const

    If lHost <> 0 Then
    MemCopy tHost, ByVal lHost, LenB(tHost)
    MemCopy lLocalHost, ByVal tHost.h_addr_list, 4
    MemCopy lLocalIP, ByVal lLocalHost, tHost.h_length
    Else
    ' translate the error.
    sLogDesc = GetWinsockErrorText(WSAGetLastError())

    'log the error
    Output sLogDesc

    Exit Sub
    End If

    'I have a listen command button..on click
    'create a socket
    lLocalSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)

    If lLocalSocket = INVALID_SOCKET Then
    ' translate the error.
    sLogDesc = GetWinsockErrorText(WSAGetLastError())

    'log the error
    Output sLogDesc & vbCrLf

    Exit Function
    End If

    'bind to our local address
    tSockAddr.sin_family = AF_INET
    tSockAddr.sin_addr = lLocalIP
    tSockAddr.sin_port = lLocalPort
    lRet = bind(lLocalSocket, tSockAddr, LenB(tSockAddr))

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    'put socket into listen mode
    Output "*** Listening" & vbCrLf
    lRet = listen(lLocalSocket, SOMAXCONN)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    'get a handle to internal winsock event for asynch mode
    lLocalEvent = WSACreateEvent()

    If lLocalEvent = WSA_INVALID_EVENT Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    ' specify what events we want
    lRet = WSAEventSelect(lLocalSocket, lLocalEvent, FD_ACCEPT)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    ' loop and wait for the events
    bExit = False
    Do
    DoEvents
    lRet = WSAEnumNetworkEvents(lLocalSocket, lLocalEvent, tWSAEvent)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    If Not bExit Then
    Output sLogDesc & vbCrLf
    DisconnectSocket lLocalSocket
    End If
    Exit Function
    Else
    Select Case tWSAEvent.lNetworkEvents
    Case FD_ACCEPT
    Call AcceptSocket

    End Select
    End If
    Loop Until bExit

    'the accept socket routine...
    lSockAddrLen = LenB(tSockAddr)
    lRemoteSocket = accept(lLocalSocket, tSockAddr, lSockAddrLen)

    If lRemoteSocket = INVALID_SOCKET Then
    ' translate the error.
    sLogDesc = GetWinsockErrorText(WSAGetLastError())

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lLocalSocket
    Exit Function
    End If

    ' who did we connect to? convert to host ordering.
    sRemoteIP = String(32, 32)
    lRet = inet_ntoa(tSockAddr.sin_addr)
    If lRet > 0 Then
    lLen = lstrlen(lRet)
    If lLen > 32 Then lLen = 32
    MemCopy ByVal sRemoteIP, ByVal lRet, lLen
    sRemoteIP = Left(sRemoteIP, lLen)
    Else
    sRemoteIP = "255.255.255.255"
    End If

    sRemotePort = Str(ntohs(tSockAddr.sin_port))

    Output "Accepted connection from " & sRemoteIP & ":" & sRemotePort & vbCrLf

    'get a handle to internal winsock event for asynch mode
    lRemoteEvent = WSACreateEvent()

    If lRemoteEvent = WSA_INVALID_EVENT Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lRemoteSocket
    Exit Function
    End If

    ' specify what events we want
    lRet = WSAEventSelect(lRemoteSocket, lRemoteEvent, FD_READ Or FD_CLOSE)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lRemoteSocket
    Exit Function
    End If

    bRemoteExit = False
    cmTerminate.Enabled = True

    ' loop and wait for the events
    Do
    DoEvents
    lRet = WSAEnumNetworkEvents(lRemoteSocket, lRemoteEvent, tWSAEvent)

    If lRet = SOCKET_ERROR Then
    ' translate the error.
    lRet = WSAGetLastError()
    sLogDesc = GetWinsockErrorText(lRet)

    'log the error
    Output sLogDesc & vbCrLf

    DisconnectSocket lRemoteSocket
    Exit Function
    Else
    Select Case tWSAEvent.lNetworkEvents
    Case FD_READ
    'incoming data
    ReadSocket lRemoteSocket

    Case FD_CLOSE
    'terminated by remote client
    DisconnectSocket lRemoteSocket
    CloseEvent lRemoteEvent
    sLogDesc = "Connection closed by remote client"
    Output sLogDesc & vbCrLf
    Exit Do

    End Select
    End If
    Loop Until bRemoteExit

    DisconnectSocket lRemoteSocket
    CloseEvent lRemoteEvent

    'and some other helpers
    Private Sub DisconnectSocket(lSocket As Long)

    On Error Resume Next

    If lSocket = INVALID_SOCKET Then Exit Sub

    Call shutdown(lSocket, SD_BOTH)
    Call closesocket(lSocket)
    lSocket = INVALID_SOCKET

    On Error GoTo 0

    End Sub

    Private Sub CloseEvent(lEvent As Long)

    On Error Resume Next

    If lEvent = WSA_INVALID_EVENT Then Exit Sub

    Call WSACloseEvent(lEvent)
    lEvent = WSA_INVALID_EVENT

    On Error GoTo 0

    End Sub

    "Chris.G" <chrisg@apdomail.sgp.hp.com> wrote:
    >
    > There,
    > This is a module in my TCP/IP class which basically
    > uses the wsock32 API to make a connection oriented
    > socket (Type SOCK_STREAM).
    >
    > All the following works in initiating a connection.
    > 1)Initialize Winsock API
    > 2)Create new socket
    > 3)Bind socket to LocalHostIP
    > 4)Bind socket to RemoteHost
    >
    >After performing all these steps when I try to use the Connect function

    to
    >connect to the remote host, the function
    >returns the value SOCKET_ERROR.
    >___________________________________________________________
    >Connect portion of the code.
    >WSAResult = connect(mlngSocket, msaRemoteAddr, Len (msaRemoteAddr))
    >
    >If (WSAResult = SOCKET_ERROR) Then
    > SetLastErrorCode "Error in OpenConnection::connect"
    >Else
    > OpenConnection = True
    >End If
    >____________________________________________________________
    >
    >When I check the return value of WSAGetLastError it is 0, I
    >I am unable to determine the cause of the failure as both
    >the server are located in the same domain.
    >
    >1)Do the same program needs to run as well as in the peer server(parallely)
    >to which the
    > connection is made?
    >2)Do I need to do anything else besides these steps?.
    >
    > Like someone told me that I need code for the listening
    > socket???. If that is the case where does the code for the
    > listening socket should exist, in the client socket program
    > or sever socket Program?.
    >
    >
    >Can anyone tell me what would be the reason my conection fails.
    >
    >Kindest regards,
    >Chris.G
    >
    >This is the function
    >--------------------
    > Public Function OpenConnection() As Boolean
    > Dim WSAResult As Long
    > OpenConnection = False
    >
    > 'Initialize Winsock API
    > WSAResult = WSAStartup(&H101, mwsaData)
    > If WSAResult <> WSANOERROR Then
    > SetLastErrorCode "Error OpenConnection::WSAStartup"
    > Exit Function
    > End If
    >
    > 'Create new socket
    > mlngSocket = socket(PF_INET, SOCK_STREAM, 0)
    > If (mlngSocket = INVALID_SOCKET) Then
    > SetLastErrorCode "Error in OpenConnection::socket"
    > Exit Function
    > End If
    >
    > 'Bind socket to LocalHostIP
    > msaLocalAddr.sin_family = PF_INET
    > msaLocalAddr.sin_port = 0
    > msaLocalAddr.sin_addr.S_addr =
    > inet_addr(mstrLocalHostIP)
    > If (msaLocalAddr.sin_addr.S_addr = INADDR_NONE) Then
    > SetLastErrorCode "Error OpenConnection::inet_addr"
    > Exit Function
    > End If
    > WSAResult = bind(mlngSocket, msaLocalAddr, Len
    >(msaLocalAddr))
    >
    > If (WSAResult = SOCKET_ERROR) Then
    > SetLastErrorCode "Error in OpenConnection::bind"
    > Exit Function
    > End If
    >
    > 'Connect with remote host
    > msaRemoteAddr.sin_family = PF_INET
    > msaRemoteAddr.sin_port = htons(mlngRemotePort)
    > msaRemoteAddr.sin_addr.S_addr = inet_addr(mstrRemoteHostIP)
    >
    > If (msaLocalAddr.sin_addr.S_addr = INADDR_NONE) Then
    > SetLastErrorCode "Error
    > OpenConnection::inet_addr=INADDR_NONE"
    > Exit Function
    > End If
    >
    > msaRemoteAddr.sin_zero(0) = 0
    > WSAResult = connect(mlngSocket, msaRemoteAddr, Len
    > (msaRemoteAddr))
    >
    > If (WSAResult = SOCKET_ERROR) Then
    > SetLastErrorCode "Error in OpenConnection::connect"
    > Else
    > OpenConnection = True
    > End If
    >End Function
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center