ConnectEx fails
-
The documentation says that when you call ConnectEx, the socket must be already bound. The connect function binds the socket for you. That must be why connect succeeds when ConnectEx fails.
tried, still not work
int main()
{
WSADATA wsa;
int res = WSAStartup(MAKEWORD(2,2), &wsa);
assert(res==0);SOCKADDR\_IN addr\_in; addr\_in.sin\_family = AF\_INET; addr\_in.sin\_addr.s\_addr = inet\_addr("127.0.0.1"); addr\_in.sin\_port = htons(5150); ZeroMemory(&addr\_in.sin\_zero, sizeof(addr\_in.sin\_zero)); SOCKET s = WSASocket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP, NULL, 0, 0); assert(s!=INVALID\_SOCKET); SOCKADDR\_IN addr\_in\_local; addr\_in\_local.sin\_family = AF\_INET; addr\_in\_local.sin\_addr.s\_addr = INADDR\_ANY; addr\_in\_local.sin\_port = 0; ZeroMemory(&addr\_in\_local.sin\_zero, sizeof(addr\_in\_local.sin\_zero)); res = bind(s, (sockaddr\*)&addr\_in\_local, sizeof(addr\_in\_local)); assert(res==0); //prepare function { DWORD dwBytes; int res = WSAIoctl(s, SIO\_GET\_EXTENSION\_FUNCTION\_POINTER, &GuidConnectEx, sizeof(GuidConnectEx), &g\_lpfnConnectEx, sizeof(g\_lpfnConnectEx), &dwBytes, NULL, NULL); int err = WSAGetLastError(); assert(res==0); } DWORD bytes\_sent; BOOL ok = (\*g\_lpfnConnectEx)(s, (sockaddr\*)&addr\_in, sizeof(addr\_in), NULL, 0, &bytes\_sent, NULL); int err = WSAGetLastError(); assert(ok); //int ok = connect(s, (sockaddr\*)&addr\_in, sizeof(addr\_in)); //assert(ok==0); for (int i=0; i<10; ++i) { char buf\[256\]; sprintf\_s(buf, sizeof(buf), "message %d", i); WSABUF wsa\_buf; wsa\_buf.buf = buf; wsa\_buf.len = strlen(buf); DWORD bytes\_sent; int res = WSASend(s, &wsa\_buf, 1, &bytes\_sent, 0, NULL, NULL); assert(res==0 && bytes\_sent==wsa\_buf.len); printf\_s("sent: %s\\n", buf); Sleep(1000); } WSACleanup(); return 0;
}
-
tried, still not work
int main()
{
WSADATA wsa;
int res = WSAStartup(MAKEWORD(2,2), &wsa);
assert(res==0);SOCKADDR\_IN addr\_in; addr\_in.sin\_family = AF\_INET; addr\_in.sin\_addr.s\_addr = inet\_addr("127.0.0.1"); addr\_in.sin\_port = htons(5150); ZeroMemory(&addr\_in.sin\_zero, sizeof(addr\_in.sin\_zero)); SOCKET s = WSASocket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP, NULL, 0, 0); assert(s!=INVALID\_SOCKET); SOCKADDR\_IN addr\_in\_local; addr\_in\_local.sin\_family = AF\_INET; addr\_in\_local.sin\_addr.s\_addr = INADDR\_ANY; addr\_in\_local.sin\_port = 0; ZeroMemory(&addr\_in\_local.sin\_zero, sizeof(addr\_in\_local.sin\_zero)); res = bind(s, (sockaddr\*)&addr\_in\_local, sizeof(addr\_in\_local)); assert(res==0); //prepare function { DWORD dwBytes; int res = WSAIoctl(s, SIO\_GET\_EXTENSION\_FUNCTION\_POINTER, &GuidConnectEx, sizeof(GuidConnectEx), &g\_lpfnConnectEx, sizeof(g\_lpfnConnectEx), &dwBytes, NULL, NULL); int err = WSAGetLastError(); assert(res==0); } DWORD bytes\_sent; BOOL ok = (\*g\_lpfnConnectEx)(s, (sockaddr\*)&addr\_in, sizeof(addr\_in), NULL, 0, &bytes\_sent, NULL); int err = WSAGetLastError(); assert(ok); //int ok = connect(s, (sockaddr\*)&addr\_in, sizeof(addr\_in)); //assert(ok==0); for (int i=0; i<10; ++i) { char buf\[256\]; sprintf\_s(buf, sizeof(buf), "message %d", i); WSABUF wsa\_buf; wsa\_buf.buf = buf; wsa\_buf.len = strlen(buf); DWORD bytes\_sent; int res = WSASend(s, &wsa\_buf, 1, &bytes\_sent, 0, NULL, NULL); assert(res==0 && bytes\_sent==wsa\_buf.len); printf\_s("sent: %s\\n", buf); Sleep(1000); } WSACleanup(); return 0;
}
followait wrote:
tried, still not work
What does this mean? Please explain which part of the program does not work, what results you receive, what messages you see, what error codes are returned. We cannot guess what happens by looking at your source code.
It's time for a new signature.
-
followait wrote:
tried, still not work
What does this mean? Please explain which part of the program does not work, what results you receive, what messages you see, what error codes are returned. We cannot guess what happens by looking at your source code.
It's time for a new signature.
-
As he suggests, bind the socket used by
ConnectEx
, but still can't connect successfully. -
followait wrote:
but still can't connect successfully.
Did you actually read my previous post? What is the error code returned when your connect call fails?
It's time for a new signature.
-
I just took a copy of your code and ran it on my Windows 7 system and it runs fine; must be something local to you. [edit]Actually no it didn't, I had to change a couple of things and misread a fail status. I will test some more tomorrow in the absence of a solution.[/edit]
It's time for a new signature.
modified on Saturday, August 7, 2010 1:05 PM
-
tried, still not work
int main()
{
WSADATA wsa;
int res = WSAStartup(MAKEWORD(2,2), &wsa);
assert(res==0);SOCKADDR\_IN addr\_in; addr\_in.sin\_family = AF\_INET; addr\_in.sin\_addr.s\_addr = inet\_addr("127.0.0.1"); addr\_in.sin\_port = htons(5150); ZeroMemory(&addr\_in.sin\_zero, sizeof(addr\_in.sin\_zero)); SOCKET s = WSASocket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP, NULL, 0, 0); assert(s!=INVALID\_SOCKET); SOCKADDR\_IN addr\_in\_local; addr\_in\_local.sin\_family = AF\_INET; addr\_in\_local.sin\_addr.s\_addr = INADDR\_ANY; addr\_in\_local.sin\_port = 0; ZeroMemory(&addr\_in\_local.sin\_zero, sizeof(addr\_in\_local.sin\_zero)); res = bind(s, (sockaddr\*)&addr\_in\_local, sizeof(addr\_in\_local)); assert(res==0); //prepare function { DWORD dwBytes; int res = WSAIoctl(s, SIO\_GET\_EXTENSION\_FUNCTION\_POINTER, &GuidConnectEx, sizeof(GuidConnectEx), &g\_lpfnConnectEx, sizeof(g\_lpfnConnectEx), &dwBytes, NULL, NULL); int err = WSAGetLastError(); assert(res==0); } DWORD bytes\_sent; BOOL ok = (\*g\_lpfnConnectEx)(s, (sockaddr\*)&addr\_in, sizeof(addr\_in), NULL, 0, &bytes\_sent, NULL); int err = WSAGetLastError(); assert(ok); //int ok = connect(s, (sockaddr\*)&addr\_in, sizeof(addr\_in)); //assert(ok==0); for (int i=0; i<10; ++i) { char buf\[256\]; sprintf\_s(buf, sizeof(buf), "message %d", i); WSABUF wsa\_buf; wsa\_buf.buf = buf; wsa\_buf.len = strlen(buf); DWORD bytes\_sent; int res = WSASend(s, &wsa\_buf, 1, &bytes\_sent, 0, NULL, NULL); assert(res==0 && bytes\_sent==wsa\_buf.len); printf\_s("sent: %s\\n", buf); Sleep(1000); } WSACleanup(); return 0;
}
followait wrote:
BOOL ok = (*g_lpfnConnectEx)(s, (sockaddr*)&addr_in, sizeof(addr_in), NULL, 0, &bytes_sent, NULL);
Now what would be the most fishy aspect of that statement? You should realize that for every cast the programmer takes the responsibility away from the compiler; you are simply passing a SOCKADDR_IN thingy and casting it to something else (a sockaddr) in order to make the compiler happy. However if they aren't compatible, that will not work out at run-time. So back to the documentation and the drawing board. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-