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

Winsock32 API, conection failes to the peer server

Status
Not open for further replies.

Gomahan

IS-IT--Management
Nov 11, 2000
4
SG
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 &quot;Error OpenConnection::WSAStartup&quot;
Exit Function
End If

'Create new socket
mlngSocket = socket(PF_INET, SOCK_STREAM, 0)
If (mlngSocket = INVALID_SOCKET) Then
SetLastErrorCode &quot;Error in OpenConnection::socket&quot;
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 &quot;Error OpenConnection::inet_addr&quot;
Exit Function
End If
WSAResult = bind(mlngSocket, msaLocalAddr, Len
(msaLocalAddr))

If (WSAResult = SOCKET_ERROR) Then
SetLastErrorCode &quot;Error in OpenConnection::bind&quot;
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 &quot;Error
OpenConnection::inet_addr=INADDR_NONE&quot;
Exit Function
End If

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

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


 
The above looks ok...From the onset. Where is your code for the listening socket??? Have you checked that? The most common error some do is Use the Listening Socket to Connect..and then Turn it back to a Listening socket again. You might check that as well...


John Stephens

P.S. For all my TCP/IP needs I usually use Catalyst Socketwrench..
 
Hi John,
Thanks for your help, I really appreciate that.
But you see I don't have much knowledge about the deployment of a TCP/IP WinSock32.

Is the code for the listening socket be included in client
side socket interface or the server side. Because my current
interface just trying to connect to a peer using the above code.

So should I have a similar Winsock32 interface running on
the server side to listen for the client side request?.

How should I go about it?. Please help me.

Kindest regards,
Chris.G
 
Chris,

There are some good examples using the Winsock control in the VB help which got me started OK.
Mike
michael.j.lacey@ntlworld.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top