Click to See Complete Forum and Search --> : Winsock2 connect error
minlial
June 5th, 2006, 01:05 PM
I am using winsock2 to create a tcp stream. My listen function works correctly but every time i try to connect it fails. Can someone tell me why the code below always fails?
sockaddr_in target;
target.sin_family = AF_INET; // address family Internet
target.sin_port = htons (5001); // set server’s port number
target.sin_addr.s_addr = inet_addr ("127.0.0.1"); // set server’s IP
philkr
June 5th, 2006, 01:18 PM
There is nothing wrong with this bit of code.
Please post the complete winsock related code, so we can take a look at it!
What error do you get when you connect?
humptydumpty
June 6th, 2006, 01:15 AM
SOCKET m_Sock;
SOCKADDR_IN sock_addr; //socket Address Variable
WSACleanup(); // Clears The Socket
m_Sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Create Socket, Conenect using Sock Stream
sock_addr.sin_port = htons(p_iPortNo); // Set Port No for Socket
sock_addr.sin_addr.s_addr = inet_addr(p_strIPAdd);// Set IP Address for Socket
sock_addr.sin_family = AF_INET;
memset(sock_addr.sin_zero, 0, sizeof(sock_addr.sin_zero));
int result = connect(m_Sock, (sockaddr*)&sock_addr, sizeof(sock_addr));// Connect socket to server
//check for result value if it is SOCKET_ERROR means Connection failed else you can proced
Thanx
philkr
June 6th, 2006, 06:20 AM
You can't call any socket functions after you called WSACleanup().
It should be the last thing you call in your program, because it means "I don't need winsock any longer".
humptydumpty
June 6th, 2006, 06:55 AM
You can't call any socket functions after you called WSACleanup().
It should be the last thing you call in your program, because it means "I don't need winsock any longer".
Just Check it out Once Again First Cleanup Then i create a Socket.There is nothing Wrong with the Code.if you want you can check this one with your self
Thanx
Krishnaa
June 6th, 2006, 07:40 AM
WSACleanup must not be called before any other winsock2 call, MSDN says
The WSACleanup function terminates use of the WS2_32.DLL.
You need closesocket call in case if you want to close any open socket.
humptydumpty
June 6th, 2006, 08:15 AM
Actuallu it Should Like in Following Manner
int retVal = 0; // Varable that stores return value type
SOCKADDR_IN sock_addr;//socket Address Variable
SOCKET m_Sock;
WSACleanup();// Clears The Socket
WORD wVersionRequested = 0x202;
WSADATA m_data;
if(0 == ::WSAStartup(wVersionRequested, &m_data))
{
m_Sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create Socket, Conenect using Sock Stream
// Run if Socket Creation Failed
if(m_Sock == INVALID_SOCKET)
{
retVal = -1;
return retVal;
}
// Run if Socket Creation Successfull
else
{
sock_addr.sin_port = htons(p_iPortNo); // Set Port No for Socket
sock_addr.sin_addr.s_addr = inet_addr(p_strIPAdd); // Set IP Address for Socket
sock_addr.sin_family = AF_INET;
memset(sock_addr.sin_zero, 0, sizeof(sock_addr.sin_zero));
int result = connect(m_Sock, (sockaddr*)&sock_addr, sizeof(sock_addr)); // Connect socket to server
// Run If Unable to connect to server
if(result == SOCKET_ERROR)
{
int i = WSAGetLastError();
retVal = -1;
return retVal;
}
}
}
logan
June 6th, 2006, 10:21 AM
Originally WSACleanup probably had some use; currently this is just for backward compatibility but it is always safer to call it as some implementations use this to terminate use of WS2_32.DLL
MikeAThon
June 8th, 2006, 06:23 PM
....My listen function works correctly but every time i try to connect it fails. ...
The code you posted does not show that listen() was called, naturally, since listen() would typically be called in a server and connect() would typically be called in a client.
If you are still having problems, post your real code, and explain clearly the problems you are seeing. To say that "listen works correctly" but that "it" fails does not give us any hint as to what is really failing.
Mike
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.