Hi 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 to which the connection is made?.
2)Do I need to do anything else besides these steps?.
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
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 to which the connection is made?.
2)Do I need to do anything else besides these steps?.
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