Sign in to follow this  

yet another winsock problem

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been using winsock for awhile now, mainly connecting to webservers. I recently coded a program to connect to my other computer on the network. I'm behind a router, and the port is forwarded. my program worked fine untill I added extra code (another thread and connection) whichh I then commented out entirely. when the server program reaches the accept line, it waits (no errors) when my other program tries to connect it fails with errir 10060 "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. " windows firewall is off on both and have tried turning my firewall off on both. No luck. The odd thing is, if the server + client both run on either computer and connect via 127.0.0.1 or 192.168.1.3 it works fine, no problems at all. heres my c++ code server
	WSADATA info;
	if (WSAStartup(0x101,&info) != 0)
	{
		MessageBox(NULL,"ERROR 1","ERROR",MB_OK);
		return false;
	}
	
	soc = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	
	if (soc == INVALID_SOCKET)
	{
		MessageBox(NULL,"ERROR 2","ERROR",MB_OK);
		return false;
	}

	struct sockaddr_in sa;
	sa.sin_addr.s_addr = INADDR_ANY;
	sa.sin_family = AF_INET;
	sa.sin_port = htons(1447);

	if (bind(soc,(SOCKADDR*)&sa,sizeof(sa)) == SOCKET_ERROR)
	{
		MessageBox(NULL,"ERROR 3","ERROR",MB_OK);
		closesocket(soc);
		return false;
	}

	MessageBox(NULL,"binded","ERROR",MB_OK);

	if (listen(soc,1) == SOCKET_ERROR)
	{
		MessageBox(NULL,"ERROR 4","ERROR",MB_OK);
		closesocket(soc);
		return false;
	}

	MessageBox(NULL,"listening","ERROR",MB_OK);

	do
	{
		clientSoc = accept(soc,NULL,NULL);
		if (clientSoc == INVALID_SOCKET)
		{
			int i = WSAGetLastError();
		}
	}
	while(clientSoc == INVALID_SOCKET);
and client

    WSADATA info;
	if (WSAStartup(0x101,&info) != 0)
	{
		MessageBox(NULL,"Error setting up winsock","ERROR",MB_OK|MB_ICONSTOP);
		return false;
	}
	
	soc = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	if (soc == INVALID_SOCKET)
	{
		MessageBox(NULL,"Error creating socket","ERROR",MB_OK|MB_ICONSTOP);
		return false;
	}

    struct hostent *hp;
	struct sockaddr_in sa;

	unsigned int addr = inet_addr(IP);
	hp = gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);

	if (hp == NULL)
	{
		MessageBox(NULL,"Error getting host","ERROR",MB_OK|MB_ICONSTOP);
		closesocket(soc);
		return false;
	}

	sa.sin_addr.s_addr = *((unsigned long*)hp->h_addr);
	sa.sin_family = AF_INET;
	sa.sin_port = htons(1447);

	if (connect(soc,(struct sockaddr*)&sa,sizeof(sa)))
	{
		int i = WSAGetLastError();
		MessageBox(NULL,"Error connecting to server","ERROR",MB_OK|MB_ICONSTOP);
		closesocket(soc);
		return false;
	}
IP is a 15 element char array memset to 0 first any help would be appreciated.

Share this post


Link to post
Share on other sites
What is the network topology? Is the server behind the router and the client not? In that case, make sure your port is forwarded.

Try to isolate if the problem is the client or the server. For instance, if a known good client like telnet can connect to your server on the service port, then your client code has a problem. If not, it's a server problem.


It may just be that I've misunderstood what the address code is meant to be doing, but your client connect code looks fishy (or at the very least more complicated than it could be). Try reducing it to just this:

sa.sin_addr.s_addr = inet_addr("server.ip.goes.here");

Share this post


Link to post
Share on other sites
I suggest you use a sniffer while debugging network problems. You can sniff on both the client and the server and that way you can tell exactly where the packets stop flowing through properly. So if you see the syn packets getting to the server and the server isn't responding, you know its an error on the server (Firewall or Application, perhaps some other wierd problem). If the server does respond with Syn-Acks and the client doesn't get them, the router is blocking. Likewise, if you see the Syns leaving the client yet never reaching the server, its also a problem with the router not forwarding properly.

If you don't have a sniffer I suggest using Ethereal. Should help you later on too in other networking problems...

Share this post


Link to post
Share on other sites
ok, nothing is going through, so im guessing its client side. so I've looked through it, replaced it with code from a working program etc. nothing. I have no choice but to assume its my firewall, even though it says its not blocking it

Share this post


Link to post
Share on other sites
Are you sure the WSA version is correct?
This is just a long shot but usually I would type MAKEWORD(2, 0) as the WSA version

EDIT: I have been helping a guy earlier with the exact same problem as yours, the arguments to accept can NOT be NULL!

Share this post


Link to post
Share on other sites

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this