Jump to content
  • Advertisement
Sign in to follow this  
WWakerFAN

Can't Connect to Server

This topic is 4327 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 a managed c++ app that's the server, and it connects like this,
			listener = (gcnew System::Net::Sockets::Socket(
								System::Net::Sockets::AddressFamily::InterNetwork,
								System::Net::Sockets::SocketType::Stream,
								System::Net::Sockets::ProtocolType::Tcp
							  )
						);


			System::Net::IPHostEntry^ ipHostInfo = System::Net::Dns::GetHostEntry(System::Net::Dns::GetHostName());
			System::Net::IPAddress^ ipAddress = ipHostInfo->AddressList[0];
			System::Net::IPEndPoint^ localEndPoint = (gcnew System::Net::IPEndPoint(ipAddress, 8888));

			listener->Bind(localEndPoint);
			listener->Listen(100);
			listener->BeginAccept(gcnew System::AsyncCallback(&SOCConnectionManager::frmMain::AcceptCallback), listener);

then i have my client that connects like this,
            WORD version;
            WSADATA wsaData;
            int rVal=0;

            version = MAKEWORD(1,1);

            WSAStartup(version,(LPWSADATA)&wsaData);

            LPHOSTENT hostEntry;

            //store information about the server
            hostEntry = gethostbyname("localhost");

            if(!hostEntry) {
                WSACleanup();
                break;
            }

            //create the socket
            theSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

            if(theSocket == SOCKET_ERROR) {
                MessageBoxA(0, "COULD NOT CREATE SOCKET", "DLL Message", MB_OK | MB_ICONINFORMATION);
                break;
            }

            //Fill in the sockaddr_in struct
            SOCKADDR_IN serverInfo;

            serverInfo.sin_family = PF_INET;
            serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

            serverInfo.sin_port = htons(8888);

            rVal=connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(serverInfo));

            if(rVal==SOCKET_ERROR) {
                MessageBoxA(0, "COULD NOT CONNECT", "DLL Message", MB_OK | MB_ICONINFORMATION);
                //break;
            }
originally i just had an unmanaged console app for the server, and it managed to connect fine, but this one doesn't want to and i'm unsure as to why.

Share this post


Link to post
Share on other sites
Advertisement
GetHostName will resolve to some IP address -- or an error, in many cases.
localhost will resolve to 127.0.0.1.
Your bind explicitly does NOT bind to 127.0.0.1, so it's no surprise if 127.0.0.1 doesn't actually connect.

You want the server to bind to address "0.0.0.0" which is an alias for "all interfaces on this machine." That way, it'll be bound to 127.0.0.1, and "localhost" will find it.

Share this post


Link to post
Share on other sites
i replaced
System::Net::IPHostEntry^ ipHostInfo = System::Net::Dns::GetHostEntry(System::Net::Dns::GetHostName());
System::Net::IPAddress^ ipAddress = ipHostInfo->AddressList[0];
System::Net::IPEndPoint^ localEndPoint = (gcnew System::Net::IPEndPoint(ipAddress, 8888))

with
System::Net::IPEndPoint^ localEndPoint = (gcnew System::Net::IPEndPoint(System::Net::IPAddress::Any, 8888));

and it still won't connect :/

[Edited by - WWakerFAN on November 15, 2006 6:40:55 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!