Jump to content
  • Advertisement
Sign in to follow this  
Thrust

Entering in a hosts IP address... Please help...

This topic is 5057 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

Hello everyone. Im trying to make the client to client interface for a game im going to create. I havent done this Winsock thing for awhile so im a little rusty. Anywhay here I go. If you chose to host, it loads properly and waits for the other user to connect, now if you chose to join for some reason theres a problem with the fact I enter in the IP address manually. I thought this was the right way to do it but I guess not. Anyways heres the full code
#include <iostream>
#include <stdlib.h>
#include <windows.h> 
#include <winsock2.h> 

//Starts up WinSock
void host();
void join();

using namespace std;
  
int main(int argc, char *argv[])
{
  int choice;
  
  cout <<"Loading the game..." << endl;
  cout << "would you like to host?" << endl;
  cout << "1.Yes" << endl;
  cout << "2.No" << endl;
  cin >> choice;
  
  if(choice == 1)
  {
    host();
  }
  else if (choice == 2)
  {
    join();
  }
  
  
  cout << "WELCOME 2 DA GAME!!! Well its actuially not here yet..." << endl;
  cout << "This just tests the connection :)" << endl;
  
  system("PAUSE");	
  return 0;
}

//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//Hosts the game
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
void host()
{
  int error;
  WSADATA wsaData;
  error = WSAStartup(MAKEWORD(2, 0), &wsaData);
  
  if (error != 0)
  {
    cout << "Couldn't Initialize Winsock: 1" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  
  cout << "Winsock initialized..." << endl;
  
  SOCKET connectSocket;
  connectSocket = socket(AF_INET, SOCK_STREAM,0);
  
  if (connectSocket == INVALID_SOCKET)
  {
    cout << "Couldnt create socket: 2" << endl;
  // unload WinSock
  WSACleanup ();
  return; 
  }
  
  cout << "Socket created..." << endl;
  
  //This is the port it connects to, we are using 12521
  const int port = 12521; 
  // the address structure for a TCP socket
  sockaddr_in addr;

  addr.sin_family = AF_INET;      // Address family Internet
  addr.sin_port = htons (port);   // Assign port to this socket
  addr.sin_addr.s_addr = htonl (INADDR_ANY);   // No destination
  
  if (bind(connectSocket, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
  { 
  cout << "Couldn't bind socket: 3" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  
  cout << "Socket binded..." << endl;
  
  //Waits for the connection 
  cout << "Waiting for connection from other player..." << endl;
  listen(connectSocket, 1);
  
  //accepts the connection
  accept(connectSocket, NULL, NULL);
  cout << "Client accepted... SUCCESS!" << endl;
  
}
  

//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//Joins the host
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
void join()
{
  int error;
  int ipAddress;
  WSADATA wsaData;
  error = WSAStartup(MAKEWORD(2, 0), &wsaData);
  
  if (error != 0)
  {
    cout << "Couldn't Initialize Winsock: 1" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  
  cout << "Winsock initialized..." << endl;
  
  //Takes the hosts IP address for the connecton 
  cout << "Please enter the hosts IP address: " << endl;
  cin >> ipAddress;
  //Creates socket
  SOCKET connectSocket;
  connectSocket = socket(AF_INET, SOCK_STREAM,0);
  
  if (connectSocket == INVALID_SOCKET)
  {
    cout << "Couldnt create socket: 2" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  
  cout << "Socket created..." << endl;
  
  //This is the port it connects to, we are using 12521
  const int port = 12521; 
  // the address structure for a TCP socket
  sockaddr_in addr;

  addr.sin_family = AF_INET;      // Address family Internet
  addr.sin_port = htons (port);   // Assign port to this socket
  addr.sin_addr.s_addr = htonl (ipAddress);   // Hosts IP address
  //Connects to the host
  error = connect(connectSocket,(LPSOCKADDR)&addr,sizeof(struct sockaddr));
  if (error != 0)
  { 
  cout << "Couldn't connect to host: 3" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  cout << "Connected" << endl;
  
}

The problem seems to be here
cout << "Please enter the hosts IP address: " << endl;
  cin >> ipAddress;
  //Creates socket
  SOCKET connectSocket;
  connectSocket = socket(AF_INET, SOCK_STREAM,0);
  
  if (connectSocket == INVALID_SOCKET)
  {
    cout << "Couldnt create socket: 2" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  
  cout << "Socket created..." << endl;
  
  //This is the port it connects to, we are using 12521
  const int port = 12521; 
  // the address structure for a TCP socket
  sockaddr_in addr;

  addr.sin_family = AF_INET;      // Address family Internet
  addr.sin_port = htons (port);   // Assign port to this socket
  addr.sin_addr.s_addr = htonl (ipAddress);   // Hosts IP address
  //Connects to the host
  error = connect(connectSocket,(LPSOCKADDR)&addr,sizeof(struct sockaddr));
  if (error != 0)
  { 
  cout << "Couldn't connect to host: 3" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  cout << "Connected" << endl;
  
}

Know a way to fix this, im stumped... Once I crack this, its on to the game itself, but this is a MAJOR setback. Two hours of web surfing and tutorials and I still can't crack this. Anyways, I get no errors, it just doesent correct. I assume its because im using an int to store the IP address which includes periods, but yet if I change it to a string, it wont work. I cant use an array of characers because you have to define a size and IP Address sizes change. I just dont get it.

Share this post


Link to post
Share on other sites
Advertisement
sockaddr_in clientService;

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 27015 );

Out of msdn, it uses inet_addr() where you use htonl()...

Share this post


Link to post
Share on other sites

void join()
{
int error;
char ipAddress;
WSADATA wsaData;
error = WSAStartup(MAKEWORD(2, 0), &wsaData);

if (error != 0)
{
cout << "Couldn't Initialize Winsock: 1" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Winsock initialized..." << endl;

//Takes the hosts IP address for the connecton
cout << "Please enter the hosts IP address: " << endl;
cin >> ipAddress;
const char ip = ipAddress;
//Creates socket
SOCKET connectSocket;
connectSocket = socket(AF_INET, SOCK_STREAM,0);

if (connectSocket == INVALID_SOCKET)
{
cout << "Couldnt create socket: 2" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Socket created..." << endl;

//This is the port it connects to, we are using 12521
const int port = 12521;
// the address structure for a TCP socket
sockaddr_in addr;

addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (port); // Assign port to this socket
addr.sin_addr.s_addr = inet_addr (ip); // Hosts IP address



Now i get the error message:

server.cpp: In function `void join()':
server.cpp:154: invalid conversion from `const char' to `const char*'

make.exe: *** [server.o] Error 1

Share this post


Link to post
Share on other sites
Ironically it still doesent work. Now I get the error:
server.cpp:168: cannot convert `std::string' to `const char*' for argument `1'
to `long unsigned int inet_addr(const char*)'

I just dont get it. It wont freaking work. All I want is this damn connection so I can start working on something a little more complex.


//Mark St. Jean
//Brandon Walton
//WASTEDink 2004
//First online game alpha 0.1

#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <winsock2.h>

//Starts up WinSock
void host();
void join();

using namespace std;

int main(int argc, char *argv[])
{
int choice;

cout <<"Loading the game..." << endl;
cout << "would you like to host?" << endl;
cout << "1.Yes" << endl;
cout << "2.No" << endl;
cin >> choice;

//Eventually change to switch
if(choice == 1)
{
host();
}
else if (choice == 2)
{
join();
}


cout << "WELCOME 2 DA GAME!!! Well its actuially not here yet..." << endl;
cout << "This just tests the connection :)" << endl;

system("PAUSE");
return 0;
}

//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//Hosts the game
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
void host()
{
int error;
//loads winsock
WSADATA wsaData;
error = WSAStartup(MAKEWORD(2, 0), &wsaData);

//error handler
if (error != 0)
{
cout << "Couldn't Initialize Winsock: 1" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Winsock initialized..." << endl;

//creates a socket
SOCKET connectSocket;
connectSocket = socket(AF_INET, SOCK_STREAM,0);

//error handler
if (connectSocket == INVALID_SOCKET)
{
cout << "Couldnt create socket: 2" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Socket created..." << endl;

//This is the port it connects to, we are using 12521
const int port = 12521;
// the address structure for a TCP socket
sockaddr_in addr;

addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (port); // Assign port to this socket
addr.sin_addr.s_addr = htonl (INADDR_ANY); // No destination

if (bind(connectSocket, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
{
cout << "Couldn't bind socket: 3" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Socket binded..." << endl;

//Waits for the connection
cout << "Waiting for connection from other player..." << endl;
listen(connectSocket, 1);

//accepts the connection
accept(connectSocket, NULL, NULL);
cout << "Client accepted... SUCCESS!" << endl;

}


//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//Joins the host
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
void join()
{
int error;
string ipAddress;

//loads winsock
WSADATA wsaData;
error = WSAStartup(MAKEWORD(2, 0), &wsaData);

//error handler
if (error != 0)
{
cout << "Couldn't Initialize Winsock: 1" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Winsock initialized..." << endl;

//Takes the hosts IP address for the connecton
cout << "Please enter the hosts IP address: " << endl;
cin >> ipAddress;

//Creates socket
SOCKET connectSocket;
connectSocket = socket(AF_INET, SOCK_STREAM,0);

//error handler
if (connectSocket == INVALID_SOCKET)
{
cout << "Couldnt create socket: 2" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Socket created..." << endl;

//This is the port it connects to, we are using 12521
const int port = 12521;
// the address structure for a TCP socket
sockaddr_in addr;

addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (port); // Assign port to this socket
addr.sin_addr.s_addr = inet_addr(ipAddress); // Hosts IP address
//Connects to the host
error = connect(connectSocket,(LPSOCKADDR)&addr,sizeof(struct sockaddr));

//error handler
if (error != 0)
{
cout << "Couldn't connect to host: 3" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Connected" << endl;

}


Share this post


Link to post
Share on other sites
that error indicates that the receiving function expects a const *char, but your trying to send it a std::string. remember a std::string is NOT a const *char.

the simple solution is call .c_str() method of the string object when sending. e.g.

function(some_string.c_str())

c_str() returns a const *char which represents the string.

Share this post


Link to post
Share on other sites
Thanks Graveyard, it finially works, well kind of. Here is the current, up to date code:


//Mark St. Jean
//Brandon Walton
//WASTEDink 2004
//First online game alpha 0.1

#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <winsock2.h>

//Starts up WinSock
void host();
void join();

using namespace std;

int main(int argc, char *argv[])
{
int choice;

cout <<"Loading the game..." << endl;
cout << "would you like to host?" << endl;
cout << "1.Yes" << endl;
cout << "2.No" << endl;
cin >> choice;

//Eventually change to switch
if(choice == 1)
{
host();
}
else if (choice == 2)
{
join();
}


cout << "WELCOME 2 DA GAME!!! Well its actuially not here yet..." << endl;
cout << "This just tests the connection :)" << endl;

system("PAUSE");
return 0;
}

//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//Hosts the game
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
void host()
{
int error;
//loads winsock
WSADATA wsaData;
error = WSAStartup(MAKEWORD(2, 0), &wsaData);

//error handler
if (error != 0)
{
cout << "Couldn't Initialize Winsock: 1" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Winsock initialized..." << endl;

//creates a socket
SOCKET connectSocket;
connectSocket = socket(AF_INET, SOCK_STREAM,0);

//error handler
if (connectSocket == INVALID_SOCKET)
{
cout << "Couldnt create socket: 2" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Socket created..." << endl;

//This is the port it connects to, we are using 12521
const int port = 12521;
// the address structure for a TCP socket
sockaddr_in addr;

addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (port); // Assign port to this socket
addr.sin_addr.s_addr = htonl (INADDR_ANY); // No destination

if (bind(connectSocket, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
{
cout << "Couldn't bind socket: 3" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Socket binded..." << endl;

//Waits for the connection
cout << "Waiting for connection from other player..." << endl;
listen(connectSocket, 1);

cout << "Woot!" << endl;

//accepts the connection
accept(connectSocket, NULL, NULL);
cout << "Client accepted... SUCCESS!" << endl;

}


//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//Joins the host
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
void join()
{
int error;
string ipAddress;

//loads winsock
WSADATA wsaData;
error = WSAStartup(MAKEWORD(2, 0), &wsaData);

//error handler
if (error != 0)
{
cout << "Couldn't Initialize Winsock: 1" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Winsock initialized..." << endl;

//Takes the hosts IP address for the connecton
cout << "Please enter the hosts IP address: " << endl;
cin >> ipAddress;

//Creates socket
SOCKET connectSocket;
connectSocket = socket(AF_INET, SOCK_STREAM,0);

//error handler
if (connectSocket == INVALID_SOCKET)
{
cout << "Couldnt create socket: 2" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Socket created..." << endl;

//This is the port it connects to, we are using 12521
const int port = 12521;
// the address structure for a TCP socket
sockaddr_in addr;

addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (port); // Assign port to this socket
addr.sin_addr.s_addr = inet_addr(ipAddress.c_str()); // Hosts IP address
//Connects to the host
error = connect(connectSocket,(LPSOCKADDR)&addr,sizeof(struct sockaddr));

//error handler
if (error != 0)
{
cout << "Couldn't connect to host: 3" << endl;
// unload WinSock
WSACleanup ();
return;
}

cout << "Connected" << endl;

}




Alright, when you try connecting, the message goes through for the person joining, but yet even when you start trying to host, it doesent stop and listen, it skips right over it and sends the "woot!" message. I looked over my code for a while now and I can't figure out why the heck it just skips over it, even if your not online it skips over it and displays the message. One last part and im finially there, for now... Thanks for your help already, you all have helped alot. Im going to go now, check back in the mourning. Thanks everyone. Please just help me figure out this last problem, im tired of staring at code... the worst part of being a coder.

Share this post


Link to post
Share on other sites
You're not a coder, you're a very lost person in beyond your depth.
I suggest you take a step back for a while & concentrate on all these little bits that you are skipping over so you can start "the game."

Sorry. I didn't mean that rudely I'm not trying to get @ you.
I've done the same thing myself before - tried to just rush in. But you need to slow down abit & tackle all those little bits & actually learn what they are & how they work (for instance prompting the user for something which is clearly a string "216.239.57.99" & not an int) & then using an std string & not realising what a char* is or how to convert to it.
You're headed for disaster - calm down.
Quote:
im tired of staring at code... the worst part of being a coder

GET OUT! Run! Run for the hills! You'll be eaten alive.
That statement isn't a very promising sign. & btw many coders spend much more than 2 hours looking up & trying to get their heads around things, & can spend upto weeks staring @ code that produces runtime errors FOR NO DAMN APPARENT REASON.
Seriously if you don't like code I predict some kind of gruesome self-inflected death.
...Or prehaps I've over-exagerating & you tryed rushing ahead abit too much. Tommorow you will probally look @ it, then realise the kind of mess you did & set off of another path.
I remember being very optimisatic years ago & rushing ahead, only to find that I was eventually lost & having to throw it all away again after. But each time you do this you do get further, so it's important to keep encouraged. So erm keep with it. But still I'll yell the words that I wished someone had upon myself during those times: "Stop you're going too fast, try & get an understanding before you are totally lost in the woods".
Probally won't do any good though right ;] Mistakes make a man.

Share this post


Link to post
Share on other sites
Believe it or not this is my second winsock application. I am currently re-learning c++ after a few months off and I dont really appreciate the very long post you just left. Please dont make assumptions of me, I just wanted help on the code. Now back to figuring out whats wrong... Thanks for the advice though, but let me do things my way. If everyone listend to people when they were told there doing things wrong, this world would be a horriable place.

Share this post


Link to post
Share on other sites
Speaking of which, I found my error:




accept(connectSocket, NULL, NULL);



I believe those should contain the correct values, ha. But yet that doesent explain why it skips the listen function, shouldn't it wait there till the other client trys to connect to it?

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!