|
-
June 5th, 2006, 01:05 PM
#1
Winsock2 connect error
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
-
June 5th, 2006, 01:18 PM
#2
Re: Winsock2 connect error
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?
Please don't forget to rate users who helped you!
-
June 6th, 2006, 01:15 AM
#3
Re: Winsock2 connect error
Code:
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
Last edited by humptydumpty; June 6th, 2006 at 01:17 AM.
-
June 6th, 2006, 06:20 AM
#4
Re: Winsock2 connect error
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".
Please don't forget to rate users who helped you!
-
June 6th, 2006, 06:55 AM
#5
Re: Winsock2 connect error
 Originally Posted by philkr
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
-
June 6th, 2006, 07:40 AM
#6
Re: Winsock2 connect error
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.
Regards,
Ramkrishna Pawar
-
June 6th, 2006, 08:15 AM
#7
Re: Winsock2 connect error
Actuallu it Should Like in Following Manner
Code:
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;
}
}
}
-
June 6th, 2006, 10:21 AM
#8
Re: Winsock2 connect error
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
"I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
FORZA MILAN!!!
-
June 8th, 2006, 06:23 PM
#9
Re: Winsock2 connect error
 Originally Posted by minlial
....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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|