Sign in to follow this  
crazy_andy

error getting host

Recommended Posts

I have written two programs, the client and the server. when I run both on one pc and have IP being "127.0.0.1" it connect fine, and all the other stuff in my programs work. However I want to run the server on another computer connected to my router. local IP being "192.168.1.2" when I try using the same code, to connect to the other computer using both the local IP and the actual IP I get hp being NULL. and my MessageBox saying "error getting host". I have sygate firewall on both PCs, and my router firewall. I have forwarded the port to the server computer. The software firewalls are not bllocking the program afaik. The code below is part of my SetUpConnection function. Anyone got any idea what I'm doing wrong.
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;

    if (inet_addr(IP) == INADDR_NONE)
	{
		hp = gethostbyname(IP);
	}
	else
	{
		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;
	}

Share this post


Link to post
Share on other sites
Try the telnet command from the console prompt:
Start->run->cmd
telnet 192.168.1.2 port

If it says it can't connect, it means your firewalls or router are not configured properly. If it connects, it means your program has a problem.

Share this post


Link to post
Share on other sites
it connects, after I forwarded port 23 on my router. its not the connect thats the problem though, its the gethostbyaddt / gethostbyname functions.

edit: problem solved. the ip address passed to the function was being truncated to 10 characters doh :)

[Edited by - crazy_andy on November 4, 2005 7:52:32 PM]

Share this post


Link to post
Share on other sites

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