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

Why not closesocket?

0 views
Skip to first unread message

fantasy1215 via PocketPCJunkies.com

unread,
Jun 22, 2009, 10:38:21 PM6/22/09
to

In a dialog, click a button to create 100 threads to connect to a server,
codes are below, but in some thread it don't closesocket, can somebody
explain why? After the code, I'll paste some output of my program.
UINT __cdecl CTestsocketDlg::TestSocket(LPVOID pParam)
{
SOCKET sock = INVALID_SOCKET;
CString strShow;
sock = socket(AF_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 0;
}
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 0;
}
RETAILMSG(1, (L"~~~~~~~~closesocket[%d]\n", sock ) );
closesocket(sock);
return 1;
}

void CTestsocketDlg::OnButton1()
{
// TODO: Add your control notification handler code here
for (int i = 0; i < 100; ++i)
{
::AfxBeginThread(TestSocket, (LPVOID)0);
}
}

output:
~~~~~~~~sock[1193]
~~~~~~~~sock[1195]
~~~~~~~~sock[1197]
~~~~~~~~sock[1199]
~~~~~~~~closesocket[1199]
the socket handle 1193 1195 1197 are not closed.

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

Ation

unread,
Jun 23, 2009, 2:42:14 AM6/23/09
to
On 23 июн, 05:38, "fantasy1215 via PocketPCJunkies.com" <u50650@uwe>
wrote:

Hi. Cause some threads wait for connection. May be server can't accept
100 connections.

fantasy1215 via PocketPCJunkies.com

unread,
Jun 23, 2009, 3:11:37 AM6/23/09
to

but what happen to the unclosed socket handle? Do I waste them? If connect
block, and closesocket hasn't run, then the thread is blocked in connect(),
not exit right?

Ation wrote:
>On 23 июн, 05:38, "fantasy1215 via PocketPCJunkies.com" <u50650@uwe>
>wrote:
>> In a dialog, click a button to create 100 threads to connect to a server,
>> codes are below, but in some thread it don't closesocket, can somebody

>[quoted text clipped - 48 lines]


>> --
>> Message posted viahttp://www.pocketpcjunkies.com
>
>Hi. Cause some threads wait for connection. May be server can't accept
>100 connections.

--

Paul G. Tobey [eMVP]

unread,
Jun 23, 2009, 11:17:41 AM6/23/09
to

Well, yes, the thread can block in connect(), if there's no server present
(the connect will time-out, eventually), or if the server's socket does not
complete the connection for a while. While you have the socket, it's out of
circulation; no other thread in your application can use it. I don't recall
what the maximum number of socket handles that can open at once is, but it's
hundreds.

Paul T.

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


> but what happen to the unclosed socket handle? Do I waste them? If connect
> block, and closesocket hasn't run, then the thread is blocked in
> connect(),
> not exit right?
>
> Ation wrote:

>>On 23 ???, 05:38, "fantasy1215 via PocketPCJunkies.com" <u50650@uwe>

0 new messages