f



Stupid Mistake - but What?

OK, I know I'm just doing something obviously wrong, but I can't see
it.

I am trying to create a very simple HTTP server, I need to be able to
load my browser and enter a URL like

http://127.0.0.1:12345/index.html

and have my server provide that page.  It's only used on the local
machine (will never be needed on the Internet itself).

I'm running on Windows XP.  I launch a thread, and the thread (code
below) sits there, waiting for connection requests.  Problem is,
accept() never returns.  I try loading http://127.0.0.1:12345/index.html
in both FF and IE, nothing.  I try telnet 127.0.0.1 12345, nothing.  I
try telnet 192.168.1.50 12345, nothing (that's my machine's IP on my
LAN).

If I create a socket inside my server application (in its main thread,
not in the server thread) and connect to socket 12345, that works, so
it appears my code is basically correct, but my socket can't be seen
outside of the application itself?

Thanks in advance for helping me with this (undoubtedly stupid) error.
Chris

UINT CServerThread::ThreadProc(void)
{
	WSADATA wsaData;
	sockaddr_in address;

	TRACE("CServerThread::ThreadProc() in thread %d [this=%p]\n",
GetCurrentThreadId(), this);

	int iErr = WSAStartup(MAKEWORD(2, 2), &wsaData);
	// WSAStartup() returned 0 in 0 msec
	if (iErr)
	{
		TRACE("   WSAStartup() failed with error %d\n", iErr);
		return false;
	}
	m_bStarted = true;

	m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	// socket() returned 11596 in 0 msec
	if (m_socket == INVALID_SOCKET)
	{
		TRACE("   socket() failed with error %d\n", WSAGetLastError());
		return false;
	}

	memset(&address, 0, sizeof(address));
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = INADDR_ANY;		// inet_addr("127.0.0.1");
	address.sin_port = 12345;		// 0;		// bind() should pick an unused
port

	iErr = bind(m_socket, (SOCKADDR*)&address, sizeof(address));
	// bind() returned 0 in 0 msec
	if (iErr == SOCKET_ERROR)
	{
		TRACE("   bind() failed with error %d\n", WSAGetLastError());
		return false;
	}

	int nBytes = sizeof(address);
	memset(&address, 0, sizeof(address));
	iErr = getsockname(m_socket, (SOCKADDR*)&address, &nBytes);
	// getsockname() returned 0, 16/16 in 0 msec
	if (iErr == SOCKET_ERROR)
	{
		TRACE("   getsockname() failed with error %d, %d/%d\n",
WSAGetLastError(), nBytes, sizeof(address));
		return false;
	}
	TRACE("   Socket is using port %d\n", address.sin_port);

	iErr = listen(m_socket, SOMAXCONN);
	// listen() returned 0 in 31 msec
	if (iErr == SOCKET_ERROR)
	{
		TRACE("   listen() failed with error %d\n", WSAGetLastError());
		return false;
	}

	m_iPort = address.sin_port;

	for (;;)
	{
		sockaddr_in from;
		int fromlen = sizeof(from);
		DWORD lTick = GetTickCount();
		SOCKET socket = accept(m_socket, (sockaddr*)&from, &fromlen);
		TRACE("   accept() returned %d in %d msec\n", socket, GetTickCount()
- lTick);
		Sleep(500);
	}

	return 0;
}
0
Chris
2/19/2010 7:23:25 PM
comp.win.tools.winsock 400 articles. 0 followers. Post Follow

5 Replies
288 Views

Similar Articles

[PageSpeed] 21

On Feb 19, 12:23=A0pm, Chris Shearer Cooper
<chris.shearer.coo...@gmail.com> wrote:
> OK, I know I'm just doing something obviously wrong, but I can't see
> it.
>
> I am trying to create a very simple HTTP server, I need to be able to
> load my browser and enter a URL like
>
> http://127.0.0.1:12345/index.html
>
> and have my server provide that page. =A0It's only used on the local
> machine (will never be needed on the Internet itself).
>
> I'm running on Windows XP. =A0I launch a thread, and the thread (code
> below) sits there, waiting for connection requests. =A0Problem is,
> accept() never returns. =A0I try loadinghttp://127.0.0.1:12345/index.html
> in both FF and IE, nothing. =A0I try telnet 127.0.0.1 12345, nothing. =A0=
I
> try telnet 192.168.1.50 12345, nothing (that's my machine's IP on my
> LAN).
>
> If I create a socket inside my server application (in its main thread,
> not in the server thread) and connect to socket 12345, that works, so
> it appears my code is basically correct, but my socket can't be seen
> outside of the application itself?
>
> Thanks in advance for helping me with this (undoubtedly stupid) error.
> Chris
>
> UINT CServerThread::ThreadProc(void)
> {
> =A0 =A0 =A0 =A0 WSADATA wsaData;
> =A0 =A0 =A0 =A0 sockaddr_in address;
>
> =A0 =A0 =A0 =A0 TRACE("CServerThread::ThreadProc() in thread %d [this=3D%=
p]\n",
> GetCurrentThreadId(), this);
>
> =A0 =A0 =A0 =A0 int iErr =3D WSAStartup(MAKEWORD(2, 2), &wsaData);
> =A0 =A0 =A0 =A0 // WSAStartup() returned 0 in 0 msec
> =A0 =A0 =A0 =A0 if (iErr)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 WSAStartup() failed with erro=
r %d\n", iErr);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 m_bStarted =3D true;
>
> =A0 =A0 =A0 =A0 m_socket =3D socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
> =A0 =A0 =A0 =A0 // socket() returned 11596 in 0 msec
> =A0 =A0 =A0 =A0 if (m_socket =3D=3D INVALID_SOCKET)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 socket() failed with error %d=
\n", WSAGetLastError());
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 memset(&address, 0, sizeof(address));
> =A0 =A0 =A0 =A0 address.sin_family =3D AF_INET;
> =A0 =A0 =A0 =A0 address.sin_addr.s_addr =3D INADDR_ANY; =A0 =A0 =A0 =A0 =
=A0 // inet_addr("127.0.0.1");
> =A0 =A0 =A0 =A0 address.sin_port =3D 12345; =A0 =A0 =A0 =A0 =A0 =A0 =A0 /=
/ 0; =A0 =A0 =A0 =A0 =A0 // bind() should pick an unused
> port
>
> =A0 =A0 =A0 =A0 iErr =3D bind(m_socket, (SOCKADDR*)&address, sizeof(addre=
ss));
> =A0 =A0 =A0 =A0 // bind() returned 0 in 0 msec
> =A0 =A0 =A0 =A0 if (iErr =3D=3D SOCKET_ERROR)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 bind() failed with error %d\n=
", WSAGetLastError());
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 int nBytes =3D sizeof(address);
> =A0 =A0 =A0 =A0 memset(&address, 0, sizeof(address));
> =A0 =A0 =A0 =A0 iErr =3D getsockname(m_socket, (SOCKADDR*)&address, &nByt=
es);
> =A0 =A0 =A0 =A0 // getsockname() returned 0, 16/16 in 0 msec
> =A0 =A0 =A0 =A0 if (iErr =3D=3D SOCKET_ERROR)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 getsockname() failed with err=
or %d, %d/%d\n",
> WSAGetLastError(), nBytes, sizeof(address));
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 TRACE(" =A0 Socket is using port %d\n", address.sin_port)=
;
>
> =A0 =A0 =A0 =A0 iErr =3D listen(m_socket, SOMAXCONN);
> =A0 =A0 =A0 =A0 // listen() returned 0 in 31 msec
> =A0 =A0 =A0 =A0 if (iErr =3D=3D SOCKET_ERROR)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 listen() failed with error %d=
\n", WSAGetLastError());
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 m_iPort =3D address.sin_port;
>
> =A0 =A0 =A0 =A0 for (;;)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 sockaddr_in from;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int fromlen =3D sizeof(from);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 DWORD lTick =3D GetTickCount();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 SOCKET socket =3D accept(m_socket, (socka=
ddr*)&from, &fromlen);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 accept() returned %d in %d ms=
ec\n", socket, GetTickCount()
> - lTick);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Sleep(500);
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 return 0;
>
> }
>
>

By the way, yes this code is a little odd right now, if accept() were
to succeed, nothing would actually happen, plus I'm returning 'false'
from a function that claims to be returning a UINT.  I figured that
once accept() started returning, I would write the rest of the code,
and I keep moving the function around between being in my main thread
and being in the CServerThread, which is why the return code is off.
Please don't get hung up on those mistakes, they shouldn't materially
affect the network-ability of this code.
0
Chris
2/19/2010 7:28:25 PM
Hi,

Is the socket really listening? netstat -an should confirm that.
How do you spawn the thread. Usng beginthreadex?

UNT,
Jag


On Feb 20, 12:28=A0am, Chris Shearer Cooper
<chris.shearer.coo...@gmail.com> wrote:
> On Feb 19, 12:23=A0pm, Chris Shearer Cooper
>
>
>
>
>
> <chris.shearer.coo...@gmail.com> wrote:
> > OK, I know I'm just doing something obviously wrong, but I can't see
> > it.
>
> > I am trying to create a very simple HTTP server, I need to be able to
> > load my browser and enter a URL like
>
> >http://127.0.0.1:12345/index.html
>
> > and have my server provide that page. =A0It's only used on the local
> > machine (will never be needed on the Internet itself).
>
> > I'm running on Windows XP. =A0I launch a thread, and the thread (code
> > below) sits there, waiting for connection requests. =A0Problem is,
> > accept() never returns. =A0I try loadinghttp://127.0.0.1:12345/index.ht=
ml
> > in both FF and IE, nothing. =A0I try telnet 127.0.0.1 12345, nothing. =
=A0I
> > try telnet 192.168.1.50 12345, nothing (that's my machine's IP on my
> > LAN).
>
> > If I create a socket inside my server application (in its main thread,
> > not in the server thread) and connect to socket 12345, that works, so
> > it appears my code is basically correct, but my socket can't be seen
> > outside of the application itself?
>
> > Thanks in advance for helping me with this (undoubtedly stupid) error.
> > Chris
>
> > UINT CServerThread::ThreadProc(void)
> > {
> > =A0 =A0 =A0 =A0 WSADATA wsaData;
> > =A0 =A0 =A0 =A0 sockaddr_in address;
>
> > =A0 =A0 =A0 =A0 TRACE("CServerThread::ThreadProc() in thread %d [this=
=3D%p]\n",
> > GetCurrentThreadId(), this);
>
> > =A0 =A0 =A0 =A0 int iErr =3D WSAStartup(MAKEWORD(2, 2), &wsaData);
> > =A0 =A0 =A0 =A0 // WSAStartup() returned 0 in 0 msec
> > =A0 =A0 =A0 =A0 if (iErr)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 WSAStartup() failed with er=
ror %d\n", iErr);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 m_bStarted =3D true;
>
> > =A0 =A0 =A0 =A0 m_socket =3D socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
> > =A0 =A0 =A0 =A0 // socket() returned 11596 in 0 msec
> > =A0 =A0 =A0 =A0 if (m_socket =3D=3D INVALID_SOCKET)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 socket() failed with error =
%d\n", WSAGetLastError());
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > =A0 =A0 =A0 =A0 }
>
> > =A0 =A0 =A0 =A0 memset(&address, 0, sizeof(address));
> > =A0 =A0 =A0 =A0 address.sin_family =3D AF_INET;
> > =A0 =A0 =A0 =A0 address.sin_addr.s_addr =3D INADDR_ANY; =A0 =A0 =A0 =A0=
 =A0 // inet_addr("127.0.0.1");
> > =A0 =A0 =A0 =A0 address.sin_port =3D 12345; =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 // 0; =A0 =A0 =A0 =A0 =A0 // bind() should pick an unused
> > port
>
> > =A0 =A0 =A0 =A0 iErr =3D bind(m_socket, (SOCKADDR*)&address, sizeof(add=
ress));
> > =A0 =A0 =A0 =A0 // bind() returned 0 in 0 msec
> > =A0 =A0 =A0 =A0 if (iErr =3D=3D SOCKET_ERROR)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 bind() failed with error %d=
\n", WSAGetLastError());
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > =A0 =A0 =A0 =A0 }
>
> > =A0 =A0 =A0 =A0 int nBytes =3D sizeof(address);
> > =A0 =A0 =A0 =A0 memset(&address, 0, sizeof(address));
> > =A0 =A0 =A0 =A0 iErr =3D getsockname(m_socket, (SOCKADDR*)&address, &nB=
ytes);
> > =A0 =A0 =A0 =A0 // getsockname() returned 0, 16/16 in 0 msec
> > =A0 =A0 =A0 =A0 if (iErr =3D=3D SOCKET_ERROR)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 getsockname() failed with e=
rror %d, %d/%d\n",
> > WSAGetLastError(), nBytes, sizeof(address));
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 TRACE(" =A0 Socket is using port %d\n", address.sin_por=
t);
>
> > =A0 =A0 =A0 =A0 iErr =3D listen(m_socket, SOMAXCONN);
> > =A0 =A0 =A0 =A0 // listen() returned 0 in 31 msec
> > =A0 =A0 =A0 =A0 if (iErr =3D=3D SOCKET_ERROR)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 listen() failed with error =
%d\n", WSAGetLastError());
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > =A0 =A0 =A0 =A0 }
>
> > =A0 =A0 =A0 =A0 m_iPort =3D address.sin_port;
>
> > =A0 =A0 =A0 =A0 for (;;)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 sockaddr_in from;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int fromlen =3D sizeof(from);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 DWORD lTick =3D GetTickCount();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 SOCKET socket =3D accept(m_socket, (soc=
kaddr*)&from, &fromlen);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 accept() returned %d in %d =
msec\n", socket, GetTickCount()
> > - lTick);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Sleep(500);
> > =A0 =A0 =A0 =A0 }
>
> > =A0 =A0 =A0 =A0 return 0;
>
> > }
>
> By the way, yes this code is a little odd right now, if accept() were
> to succeed, nothing would actually happen, plus I'm returning 'false'
> from a function that claims to be returning a UINT. =A0I figured that
> once accept() started returning, I would write the rest of the code,
> and I keep moving the function around between being in my main thread
> and being in the CServerThread, which is why the return code is off.
> Please don't get hung up on those mistakes, they shouldn't materially
> affect the network-ability of this code.- Hide quoted text -
>
> - Show quoted text -

0
Jag
2/20/2010 9:39:58 AM
On Feb 20, 2:39=A0am, Jag <jagathi...@gmail.com> wrote:
> Hi,
>
> Is the socket really listening? netstat -an should confirm that.
> How do you spawn the thread. Usng beginthreadex?
>
> UNT,
> Jag
>
> On Feb 20, 12:28=A0am, Chris Shearer Cooper
>
> <chris.shearer.coo...@gmail.com> wrote:
> > On Feb 19, 12:23=A0pm, Chris Shearer Cooper
>
> > <chris.shearer.coo...@gmail.com> wrote:
> > > OK, I know I'm just doing something obviously wrong, but I can't see
> > > it.
>
> > > I am trying to create a very simple HTTP server, I need to be able to
> > > load my browser and enter a URL like
>
> > >http://127.0.0.1:12345/index.html
>
> > > and have my server provide that page. =A0It's only used on the local
> > > machine (will never be needed on the Internet itself).
>
> > > I'm running on Windows XP. =A0I launch a thread, and the thread (code
> > > below) sits there, waiting for connection requests. =A0Problem is,
> > > accept() never returns. =A0I try loadinghttp://127.0.0.1:12345/index.=
html
> > > in both FF and IE, nothing. =A0I try telnet 127.0.0.1 12345, nothing.=
 =A0I
> > > try telnet 192.168.1.50 12345, nothing (that's my machine's IP on my
> > > LAN).
>
> > > If I create a socket inside my server application (in its main thread=
,
> > > not in the server thread) and connect to socket 12345, that works, so
> > > it appears my code is basically correct, but my socket can't be seen
> > > outside of the application itself?
>
> > > Thanks in advance for helping me with this (undoubtedly stupid) error=
..
> > > Chris
>
> > > UINT CServerThread::ThreadProc(void)
> > > {
> > > =A0 =A0 =A0 =A0 WSADATA wsaData;
> > > =A0 =A0 =A0 =A0 sockaddr_in address;
>
> > > =A0 =A0 =A0 =A0 TRACE("CServerThread::ThreadProc() in thread %d [this=
=3D%p]\n",
> > > GetCurrentThreadId(), this);
>
> > > =A0 =A0 =A0 =A0 int iErr =3D WSAStartup(MAKEWORD(2, 2), &wsaData);
> > > =A0 =A0 =A0 =A0 // WSAStartup() returned 0 in 0 msec
> > > =A0 =A0 =A0 =A0 if (iErr)
> > > =A0 =A0 =A0 =A0 {
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 WSAStartup() failed with =
error %d\n", iErr);
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > > =A0 =A0 =A0 =A0 }
> > > =A0 =A0 =A0 =A0 m_bStarted =3D true;
>
> > > =A0 =A0 =A0 =A0 m_socket =3D socket(AF_INET, SOCK_STREAM, IPPROTO_TCP=
);
> > > =A0 =A0 =A0 =A0 // socket() returned 11596 in 0 msec
> > > =A0 =A0 =A0 =A0 if (m_socket =3D=3D INVALID_SOCKET)
> > > =A0 =A0 =A0 =A0 {
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 socket() failed with erro=
r %d\n", WSAGetLastError());
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > > =A0 =A0 =A0 =A0 }
>
> > > =A0 =A0 =A0 =A0 memset(&address, 0, sizeof(address));
> > > =A0 =A0 =A0 =A0 address.sin_family =3D AF_INET;
> > > =A0 =A0 =A0 =A0 address.sin_addr.s_addr =3D INADDR_ANY; =A0 =A0 =A0 =
=A0 =A0 // inet_addr("127.0.0.1");
> > > =A0 =A0 =A0 =A0 address.sin_port =3D 12345; =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 // 0; =A0 =A0 =A0 =A0 =A0 // bind() should pick an unused
> > > port
>
> > > =A0 =A0 =A0 =A0 iErr =3D bind(m_socket, (SOCKADDR*)&address, sizeof(a=
ddress));
> > > =A0 =A0 =A0 =A0 // bind() returned 0 in 0 msec
> > > =A0 =A0 =A0 =A0 if (iErr =3D=3D SOCKET_ERROR)
> > > =A0 =A0 =A0 =A0 {
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 bind() failed with error =
%d\n", WSAGetLastError());
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > > =A0 =A0 =A0 =A0 }
>
> > > =A0 =A0 =A0 =A0 int nBytes =3D sizeof(address);
> > > =A0 =A0 =A0 =A0 memset(&address, 0, sizeof(address));
> > > =A0 =A0 =A0 =A0 iErr =3D getsockname(m_socket, (SOCKADDR*)&address, &=
nBytes);
> > > =A0 =A0 =A0 =A0 // getsockname() returned 0, 16/16 in 0 msec
> > > =A0 =A0 =A0 =A0 if (iErr =3D=3D SOCKET_ERROR)
> > > =A0 =A0 =A0 =A0 {
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 getsockname() failed with=
 error %d, %d/%d\n",
> > > WSAGetLastError(), nBytes, sizeof(address));
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > > =A0 =A0 =A0 =A0 }
> > > =A0 =A0 =A0 =A0 TRACE(" =A0 Socket is using port %d\n", address.sin_p=
ort);
>
> > > =A0 =A0 =A0 =A0 iErr =3D listen(m_socket, SOMAXCONN);
> > > =A0 =A0 =A0 =A0 // listen() returned 0 in 31 msec
> > > =A0 =A0 =A0 =A0 if (iErr =3D=3D SOCKET_ERROR)
> > > =A0 =A0 =A0 =A0 {
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 listen() failed with erro=
r %d\n", WSAGetLastError());
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return false;
> > > =A0 =A0 =A0 =A0 }
>
> > > =A0 =A0 =A0 =A0 m_iPort =3D address.sin_port;
>
> > > =A0 =A0 =A0 =A0 for (;;)
> > > =A0 =A0 =A0 =A0 {
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 sockaddr_in from;
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int fromlen =3D sizeof(from);
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 DWORD lTick =3D GetTickCount();
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 SOCKET socket =3D accept(m_socket, (s=
ockaddr*)&from, &fromlen);
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 TRACE(" =A0 accept() returned %d in %=
d msec\n", socket, GetTickCount()
> > > - lTick);
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Sleep(500);
> > > =A0 =A0 =A0 =A0 }
>
> > > =A0 =A0 =A0 =A0 return 0;
>
> > > }
>
> > By the way, yes this code is a little odd right now, if accept() were
> > to succeed, nothing would actually happen, plus I'm returning 'false'
> > from a function that claims to be returning a UINT. =A0I figured that
> > once accept() started returning, I would write the rest of the code,
> > and I keep moving the function around between being in my main thread
> > and being in the CServerThread, which is why the return code is off.
> > Please don't get hung up on those mistakes, they shouldn't materially
> > affect the network-ability of this code.- Hide quoted text -
>
> > - Show quoted text -
>
>

The program this lives inside is MFC/C++, so I use the MFC function
AfxBeginThread() which does eventually call _beginthreadex().

Regarding netstat, that's a great idea, unfortunately the results make
me even more confused.

I'm opening on a hard-coded port # of 26386 (that's a port that was
reported unused during some of my testing), and that port # doesn't
appear in the netstat listing at all.

However, if I connect to that port from inside the same program (I'll
paste the code below), it succeeds.  The connect() is happy, and
accept() returns me a socket, so life is good.  And _still_ the
connection doesn't appear in netstat.

Am I somehow not in TCP/IP?  Or how am I doing this, such that this
connection works, but doesn't show up?  I'm so confused.

Thanks,
Chris

sockaddr_in address;
SOCKET sock =3D socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&address, 0, sizeof(address));
address.sin_family =3D AF_INET;
address.sin_addr.s_addr =3D inet_addr("127.0.0.1");
address.sin_port =3D m_thread.GetPort();
int iErr =3D connect(sock, (sockaddr*)&address, sizeof(address));
// connect() returns zero (success)!
0
Chris
2/20/2010 2:17:02 PM
Chris Shearer Cooper wrote:
> [...]
> 	memset(&address, 0, sizeof(address));
> 	address.sin_family = AF_INET;
> 	address.sin_addr.s_addr = INADDR_ANY;
> 	address.sin_port = 12345;

The port the above binds to is actually 14640.  The port number (and 
other elements except "sin_family") in the SOCKADDR_IN structure is in 
network order, not host order.

You are likely making the same mistake in both your server and client 
code within your program, so they still wind up using the same port and 
the client can connect to the server.  But any other code (e.g. a telnet 
program) that you think should be using 12345 as the port won't connect, 
because you're actually using a different port.

Try this:

	memset(&address, 0, sizeof(address));
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = INADDR_ANY;
	address.sin_port = htons(12345);

Pete
0
Peter
2/20/2010 6:01:16 PM
On Feb 20, 11:01=A0am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
> Chris Shearer Cooper wrote:
> > [...]
> > =A0 =A0memset(&address, 0, sizeof(address));
> > =A0 =A0address.sin_family =3D AF_INET;
> > =A0 =A0address.sin_addr.s_addr =3D INADDR_ANY;
> > =A0 =A0address.sin_port =3D 12345;
>
> The port the above binds to is actually 14640. =A0The port number (and
> other elements except "sin_family") in the SOCKADDR_IN structure is in
> network order, not host order.
>
> You are likely making the same mistake in both your server and client
> code within your program, so they still wind up using the same port and
> the client can connect to the server. =A0But any other code (e.g. a telne=
t
> program) that you think should be using 12345 as the port won't connect,
> because you're actually using a different port.
>
> Try this:
>
> =A0 =A0 =A0 =A0 memset(&address, 0, sizeof(address));
> =A0 =A0 =A0 =A0 address.sin_family =3D AF_INET;
> =A0 =A0 =A0 =A0 address.sin_addr.s_addr =3D INADDR_ANY;
> =A0 =A0 =A0 =A0 address.sin_port =3D htons(12345);
>
> Pete

Woot!  You are the man.  I know it had to be something simple and
obvious.  Go have a beer (or drink of your choice) and send me the
bill.

Chris
0
Chris
2/20/2010 6:18:03 PM
Reply:

Similar Artilces:

Win Win Win Win......
Win Win Win Win...... Win cash Rs.10,000. SMS BLUFF to 57333 (Only For Indian) Free Registration! - Register here Free:- http://offr.biz/HLDDT337130BVGMJPF (....Free Offer is Valid Only For First 250 members who Registered...) ------------------------------------------------------------------------------- ...

Stupid, Stupid, Stupid
Me, not you. So about two weeks ago, I returned my newly bought Airport Express and purchased the new 802.11n model. I plugged it into an AC outlet near the computer and set it up. I then unplugged it, and plugged it near the DirecTV DVR. After going back to the computer, I used Airport Utility to ensure that it was connected. Then two days ago, I tried to browse the On Demand listings on DirecTV, and discovered that there was no internet connection. I did everything I could think of at the time, but regardless of configuration changes via Airport Utility, nothing worked. Last nig...

Stupid, stupid, stupid...
Hi All, <hangs head in shame> I've downloaded the latest patchpro from sun, and like a stupid idiot didn't read the instructions and installed the packages using pkgadd -d /export/home/luser/pproSunOSx865.9re2.2 this listed 11 packages, and I blindly installed them all. Now when I installed patchpro using the setup script it install but doesn't run properly. Is there a way roll back these packages that I have installed, and if I take a note of them, will it put the system back the way it was before, or has this overwritten some previous versions files. I have uninstalle...

Re: Stupid Americans! -- Stupid... Stupid... STUPID!!!
John Carlson wrote: > > On 8 Nov 2004 08:27:13 -0800, malc_p@btinternet.com (Malc P) wrote: > > >"Madison Pruet" <mpruet@comcast.net> wrote in message news:<sStjd.9547$5K2.9063@attbi_s03>... > >> Try updating your statistics. That generally makes things run better. > >> > >Or maybe ROLLBACK WORK may help.... > > > > Might take a restore . . . sounds like a bit of data corruption . . . > what's the RAID level? > > JWC What about restoring backup level 0 taked on July 4, 1776? -- In...

Tool Kits for Service Pros
20% OFF All Tools & Tool Kits For Master Network Pro's =-=-=-=-=-=-=-=-=-=-=-=-= NEW PC/Network Maintenance Kit The ultimate tool kit for maintaining network, LAN and PC environments. The 500-020 PC and Master Network Installation Maintenance Tool Kit combines the best elements of our Network Tool Kit with the key tools from our Computer Repair Tool Kit. We then added a Cat 5 Cable Tester to create our most complete Datacom Tool Kit ever. It is the ideal tool kit for any IT department! Details: http://www.forpctechs.com/store/p292.htm For Electronic Pro's =-=-=-=-=-=-=-=-=-=-...

Tool Kits for Service Pros
20% OFF All Tools & Tool Kits For Master Network Pro's =-=-=-=-=-=-=-=-=-=-=-=-= NEW PC/Network Maintenance Kit The ultimate tool kit for maintaining network, LAN and PC environments. The 500-020 PC and Master Network Installation Maintenance Tool Kit combines the best elements of our Network Tool Kit with the key tools from our Computer Repair Tool Kit. We then added a Cat 5 Cable Tester to create our most complete Datacom Tool Kit ever. It is the ideal tool kit for any IT department! Details: http://www.forpctechs.com/store/p292.htm For Electronic Pro's =-...

Tool Kits for Service Pros
20% OFF All Tools & Tool Kits For Master Network Pro's =-=-=-=-=-=-=-=-=-=-=-=-= NEW PC/Network Maintenance Kit The ultimate tool kit for maintaining network, LAN and PC environments. The 500-020 PC and Master Network Installation Maintenance Tool Kit combines the best elements of our Network Tool Kit with the key tools from our Computer Repair Tool Kit. We then added a Cat 5 Cable Tester to create our most complete Datacom Tool Kit ever. It is the ideal tool kit for any IT department! Details: http://www.forpctechs.com/store/p292.htm For Electronic Pro's =-...

CivIII: Stupid stupid stupid . . .
Hint: never send a transport, carrying an army, across a narrow seaway to an enemy landmass without first sending something offensive, or in the presence of an AEGIS cruiser or sub. 'ARGH! NO, DON'T ATTACK THE BLOODY SUB!!' -- Richard P. Grant 0x5F9559B1 MRC Lab of Mol Biol rpg 'at' mrc-lmb.cam.ac.uk http://www.rg-d.com/BioLOG/ "If it's not on fire, it's a software problem" - Matt Burch in article bl8p55$g9u$5@pegasus.csx.cam.ac.uk, Richard P. Grant at rpg14@yahoo.co.uk.invalid wrote on 9/29/...

Stupid Mistakes
Apparently I am making a stupid mistake, but I can't see it. I have a text file with one 6 digit number per line which corresponds to a file name on a network directory. I want to find each file, then copy it somewhere. I am trying to do it in steps to make the find a little faster. If it finds the file in the first step, it should not go through to the others, but it does. Testing a file, I found it in the fist step. The length of $f was 38. Can't figure out why it is not picking up on the length of $f in the second step. What am I doing wrong? Please help! use File::Find ;...

Stupid Mistake!
There I was trying to "multi-task" like an idiot and flashed a D845PT with the wrong BIOS. Now I cannot even boot to a floppy. Is there any way out of this one? Thanks, JohnS "Homer Simpson" <nada@nowhere.com> wrote in message news:Xns96B0D2E13562Esorrynothanksorg@140.99.99.138... > There I was trying to "multi-task" like an idiot and flashed a D845PT with > the wrong BIOS. Now I cannot even boot to a floppy. Is there any way out > of this one? ftp://download.intel.com/support/motherboards/desktop/d845pt/a7585201.pdf Page 76, Section 2.9...

Stupid is as stupid does
I was diddling about, searching for A82497-66 cache controllers. I'd like to pump up a P66 or so. I have found 497s at a price of roughly $3 EACH, mounted in computer boards. Screw that $75 per quote... ...

Stupid Mistake
What I Was Trying to Do-- Delete a lot of duplicate and otherwise unneeded files from a 500 GB USB external drive. What I Did-- Opened the HD window in List View. Used two shortcuts alternately several times to expand all folders and sub-folders. In other words, I used 'All Files' (Command-a) and 'Expand Folder' (Option-Command-Right Arrow) several times. What I Learned-- (1) There are several hundred thousand files on the drive. (2) The Collapse Folder shortcut (Command-Left Arrow) does not work so I cannot restore the window to its prior state. What I am Faced With-- After...

stupid mistake?
So, I want to do what should be a pretty straightforward. Figure captions in my document can have footnote or link elements as well as straight text. I would think that the following simple template would produce the desired outcome but, instead, the original link and footnote markup is included. What am I doing wrong?!? TIA, David ============== XSL code: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> <xsl:template match="caption"> <p class="caption"> Figure <xsl:value-of select=&quo...

when is winning not winning?
when apple loses. just ask out twerp apple shills. On 2014-11-01 20:57:34 +0000, awoolmeyer@gmail.com said: > when apple loses. just ask out twerp apple shills. And where is Apple losing, Michael? On Saturday, November 1, 2014 6:31:07 PM UTC-4, Alan Baker wrote: > On 2014-11-01 20:57:34 +0000, awoolmeyer@gmail.com said: > > > when apple loses. just ask out twerp apple shills. > > And where is Apple losing, Michael? interesting that you know you're a twerp troll. i like that you are the head of my fan club. it's twerps like you that ke...

Web resources about - Stupid Mistake - but What? - comp.win.tools.winsock

Mistakes Were Made (But Not by Me) - Wikipedia, the free encyclopedia
Mistakes Were Made (But Not by Me) is a non-fiction book by social psychologists Carol Tavris and Elliot Aronson , first published in 2007. It ...

Nasdaq To Pay Record $10M Fine For Facebook IPO Mistakes
The Securities and Exchange Commission announced Wednesday the punishment for Nasdaq’s mishandling of Facebook’s initial public offering: a record ...

The 10 most common firing mistakes
... There was nothing surprising about her kind remarks until the end, when she declared that the course The 10 most common firing mistakes March ...

Acting : Do people ever make mistakes in live performances? What's that like?
Answer (1 of 39): The biggest mistake I ever made during a live performance was when I literally fell asleep on stage. It was the summer of 2001 ...

Drunk Dial NO! Block Mistakes! & Chat! on the App Store on iTunes
Get Drunk Dial NO! Block Mistakes! & Chat! on the App Store. See screenshots and ratings, and read customer reviews.

Was that a mistake? Project 365(2) Day 160 - Flickr - Photo Sharing!
When I decided I needed to upgrade my netbook to a Windows based Samsung, the deal was that Pamela would have the Acer. It would be perfect for ...

Makeup MISTAKES to AVOID! +13 Tips for a Flawless Face - YouTube
THUMBS UP if you toooo made some of these mistakes! ;P For 13 TIPS on how to achieve a flawless face with makeup, check out my blog: http:// ...

The truth behind the avocado shortage: growers made 'innocent mistake'
International Avocado Society says growers tried to boost pre-Christmas sales.

Beyond Biglaw: Mistake Tolerance
The likelihood that clients (or colleagues) will forgive a mistake depends on a few key factors, as columnist Gaston Kroub explains.

Lancashire 'terrorist house' row 'not a spelling mistake'
Officials deny claims a spelling error led to a 10-year-old Muslim boy, who wrote he lived in a "terrorist house", being spoken to by police ...

Resources last updated: 1/28/2016 6:36:48 AM