Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

socket() return problem

0 views
Skip to first unread message

fantasy1215 via PocketPCJunkies.com

unread,
Jun 22, 2009, 6:05:16 AM6/22/09
to

I write client code to connect to my server. socket then connect.
When first time socket return, I print to see the SOCKET handle returned is 1.
connect is fine. After connect I'm sure I closesocket to close it. But second
time, the SOCKET handle returned by socket is 1 also, so connect complain,
WSAGetLastError is 10038(work on an invalid socket handle.) So please give me
some advice, why did socket() returned the invalid SOCKET handle?


my code:
SOCKET sock = INVALID_SOCKET;
sock = socket(PF_INET, SOCK_STREAM, 0);
RETAILMSG(1, (L"~~~~~~~~sock[%d]\n", sock ) );
if (INVALID_SOCKET == sock)
{
strShow.Format(TEXT("socket err[%d]"), WSAGetLastError());
AfxMessageBox(strShow);
return;
}
SOCKADDR_IN addr;
ZeroMemory(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(25);
addr.sin_addr.s_addr = inet_addr("192.168.1.2");
if (connect(sock, (PSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
{
strShow.Format(TEXT("connect error[%d]"), WSAGetLastError());
closesocket(sock);
return;
}
RETAILMSG(1, (L"~~~~~~~~closesocket[%d]\n", sock ) );
closesocket(sock);

--
Message posted via PocketPCJunkies.com
http://www.pocketpcjunkies.com/Uwe/Forums.aspx/wince-vc/200906/1

Paul G. Tobey [eMVP]

unread,
Jun 22, 2009, 12:40:09 PM6/22/09
to

closesocket() is not an instantaneous operation. Negotiation with the other
end of the socket occurs in the normal case. If you're actually telling us
everything, my guess would be that you're calling socket() again before the
other socket is actually broken, but after the socket number, 1, is marked
as unused in the socket table. By the time you get to connect(), the socket
is truly broken with the server and so the connect operation is invalid.

On the other hand, I've never actually seen this happen in any version of
Windows CE that I've used, so I'd think seriously first that there was
something wrong with my code. Although I don't think this is related, the
first parameter to socket() in your case should be AF_INET, not PF_INET.

Paul T.

"fantasy1215 via PocketPCJunkies.com" <u50650@uwe> wrote in message
news:97f9b23744181@uwe...

fantasy1215 via PocketPCJunkies.com

unread,
Jun 22, 2009, 9:30:48 PM6/22/09
to

How can I fix it? I use AF_INET as socket()'s first parameter, invalid handle
returned also.

Paul G. Tobey [eMVP] wrote:
>closesocket() is not an instantaneous operation. Negotiation with the other
>end of the socket occurs in the normal case. If you're actually telling us
>everything, my guess would be that you're calling socket() again before the
>other socket is actually broken, but after the socket number, 1, is marked
>as unused in the socket table. By the time you get to connect(), the socket
>is truly broken with the server and so the connect operation is invalid.
>
>On the other hand, I've never actually seen this happen in any version of
>Windows CE that I've used, so I'd think seriously first that there was
>something wrong with my code. Although I don't think this is related, the
>first parameter to socket() in your case should be AF_INET, not PF_INET.
>
>Paul T.
>

>>I write client code to connect to my server. socket then connect.
>> When first time socket return, I print to see the SOCKET handle returned

>[quoted text clipped - 29 lines]


>> RETAILMSG(1, (L"~~~~~~~~closesocket[%d]\n", sock ) );
>> closesocket(sock);

--
Message posted via http://www.pocketpcjunkies.com

Paul G. Tobey [eMVP]

unread,
Jun 23, 2009, 11:20:14 AM6/23/09
to

It's the rest of your code, probably, that is responsible for any problem
that belongs to you. You've not told us anything about how the code is
repeated, so we can hardly advise you. All we have is a function that, by
itself, I would bet is reliable. Since you're having a problem with it, how
it's called is almost certainly responsible (unless there's actually a race
condition concerning closesocket, as I mentioned; if that's the problem,
you're stuck).

Paul T.

"fantasy1215 via PocketPCJunkies.com" <u50650@uwe> wrote in message

news:9801c6eed7b49@uwe...

0 new messages