Jump to content
  • Advertisement
Sign in to follow this  
Guest

My client/server can now talk to eah other over the same computer/Lan.Now how do i...

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

Guest
get them to talk over the net?
My servers code doesnt really matter but pretty much i need them to talk from different computers via the external ip....

//Client
WSADATA wsaData;
WORD version;
int error;
version=MAKEWORD(2,0);
error = WSAStartup( version, &wsaData );

if(error!=0)
{
MessageBox(NULL,"Winsock Couldnt Initiate","Error",MB_ICONEXCLAMATION);
return false;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 0 )
{
WSACleanup();
MessageBox(NULL,"Incorrect Winsock Version","Error",MB_ICONEXCLAMATION);
return false;
}
SOCKET client;
client=socket(AF_INET,SOCK_STREAM,0);

SOCKADDR_IN sin;
memset(&sin, 0, sizeof sin );
sin.sin_family=AF_INET;
sin.sin_port=htons(5447);
sin.sin_addr.s_addr = inet_addr("118.93.199.75");

if (connect(client,(SOCKADDR *)&sin,sizeof(sin))==SOCKET_ERROR)
{
MessageBox(NULL,"Error Connecting to Server","Error",MB_ICONEXCLAMATION);
WSACleanup();
closesocket(client);
return false;
};
char buffer[1024];
int len = recv(client,(char*)buffer,sizeof(buffer),0);
if(len > 0)
{
buffer[len] = 0;
}
MessageBox(NULL,buffer,"Sent???",MB_OK);

closesocket(client);
WSACleanup();
return true;



//Server



WSADATA wsaData;
WORD version;
int error;

version = MAKEWORD( 2, 0 );

error = WSAStartup( version, &wsaData );

if ( error != 0 )
{
Console::WriteLine("Error");
return FALSE;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 0 )
{
Console::WriteLine("Error");
WSACleanup();
return FALSE;
}
SOCKET server;

server = socket( AF_INET, SOCK_STREAM, 0 );

SOCKADDR_IN sin;
Console::WriteLine("Creating Socket Address");
memset( &sin, 0, sizeof sin );

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(5447);

if ( bind( server, (sockaddr*)&sin, sizeof sin ) == SOCKET_ERROR )
{
Console::WriteLine("Error Binding Socket");
return FALSE;
}
Console::WriteLine("Starting to listen");
while ( listen( server, 20) == SOCKET_ERROR );
SOCKET client;
int length;

length = sizeof sin;
client = accept( server,(sockaddr *)&sin, &length );
if (client!=INVALID_SOCKET)
{
Console::WriteLine("Accepted!!");
}
char buffer[1024]="stuff";
if(send(client,buffer,strlen(buffer),0))
{
Console::WriteLine("Sent Message!!");
}
closesocket( server );
closesocket(client);
WSACleanup();






Show me what i must do! Thank you! XOXOXOX!!!:D:D:D:D

Share this post


Link to post
Share on other sites
Advertisement
Guest
Nope i looked at number 21 didnt help me at all

Share this post


Link to post
Share on other sites
Guest
Wheres evil steve when you need him:S

Share this post


Link to post
Share on other sites
Quote:
Original post by harminal
Nope i looked at number 21 didnt help me at all


So, in specific, what part of the answer did you not understand, or what part of applying the answer to your code did you not understand?
Please be specific: "when you say to do X, I don't understand how to pass in argument Y."

Share this post


Link to post
Share on other sites
Basically to connect using the external IP you don't have to change your code at all. you need to configure your router to forward any connections on port 5447 to your servers LAN ip though, exactly how this is done depends on your router, check its manual.

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603
Giving him the answer won't help. Right now, he needs to learn how to ask the question.


Hmmm....

The FAQ number in question is 21.

Could it be: What is 2 times 21?

Share this post


Link to post
Share on other sites
Guest
Quote:
Original post by SimonForsman
Basically to connect using the external IP you don't have to change your code at all. you need to configure your router to forward any connections on port 5447 to your servers LAN ip though, exactly how this is done depends on your router, check its manual.


Thanks you actually answered my question in good detail! Thanks alot. Hplus fails

Share this post


Link to post
Share on other sites
Guest
So simon in this line of my code

sin.sin_addr.s_addr = inet_addr("127.0.0.1");

would i change the ip to my external one?

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!