Sign in to follow this  

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

This topic is 4747 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
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
Call me crazy but isnt the syntax for the listen function:


listen(name_of_socket, #)
where the number is the number of people joining, so wouldnt it be correct to have it set to one because I only want one other player to join? I think im going to rewrite this whole thing, so many things are wrong already. All I want is a peer-to-peer client that lets you play a game similar to super mario war against each other.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Thrust
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.


It's constructive criticism, and you'll have to learn to deal with it if you plan to get anywhere in this field. He/She is right, you know. If you're not familiar with the API (there's a Winsock api doc out there) and when told that it takes a string, you throw a std::string at it and can't figure out why it's asking for a 'char *'.. then you're jumping into deeper waters than you can handle. A 'char *' is one of the most simple of types in C.

However, some people learn by blindly fumbling their way through, asking question upon question about the most simple of things.

Share this post


Link to post
Share on other sites
Finially! With a few tutorials and a good nights sleep I finially fixed my code properly. Thank you everybody that helped me in through this small but complicated process, I guess im a little to rusty to figure this stuff out on my own, heh. Anyways sorry for being so negative, I know you were trying to help, I was just really frustrated at the time because of a few coding errors. Anyways heres the current, fixed 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, IPPROTO_TCP);

//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(struct sockaddr)) == 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);

//Socket for connection
SOCKET connectPlayer;

connectPlayer = accept(connectSocket, NULL, NULL);
cout << "Client accepted... SUCCESS!" << endl;

closesocket(connectPlayer);
}


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


cout << "Please enter in the hosts IP address: ";
cin >> 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;

//Stores information about the hosting player
LPHOSTENT hostEntry;
in_addr iaHost;
//Hosts IP address
iaHost.s_addr = inet_addr(ipAddress.c_str());
//Gathers information using the IP address
hostEntry = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);
//error handling
if (!hostEntry)
{
cout << "Error gathering data..." << endl;
WSACleanup ();
return;
}



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

//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 serverInfo;

// Address family Internet
serverInfo.sin_family = AF_INET;

serverInfo.sin_port = htons (port); // Assign port to this socket

serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list); // Hosts IP address


//Connects to the host
error = connect(connectSocket,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));

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

cout << "Connected" << endl;

}



Now for that feeling of accomplishment... the best part of being a coder :)

Share this post


Link to post
Share on other sites

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