• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
d h k

[Winsock] Connection only works to loopback device

7 posts in this topic

Hi there,

I'm just starting out with netcoding, using Winsock 2.2 at the moment. My goal is to set up a connection! My code works fine when I run two instances of this console program, server mode on one, client on the other and then have the client connect the loopback device (127.0.0.1). It then says it's connected fine and I can exchange data using send () and recv ()!

However, when I'm trying this with an actual person (Germany to Australia), I get fairly weird results: when he's running server-mode and I'm connecting to his IP (ping [albeit 2s latency] and tracert work fine), I get "Failed to connect to server (error 10061)" which resolves to [i]WSAECONNREFUSED[/i]. When we switch roles and he connects to my or, and this is where it gets weird, any other random IP, he always gets "Connected to <insert IP>..." instantly!

[source lang="cpp"]

bool WinsockStart ( )
{
WSADATA wsaData;

if ( WSAStartup ( MAKEWORD ( 2, 2 ), &wsaData ) != 0 )
{
cout < < "Failed to initialize Winsock..." < < endl;
return false;
}

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

return true;
}

bool CreateConnectionAsClient ( )
{
SOCKADDR_IN addr;

cout < < "Running client..." < < endl;

SOCKET s = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( s == INVALID_SOCKET )
{
cout < < "Failed to create socket (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

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

cout < < "Please enter the ip address of the server you wish to connect to: ";
char *ipaddress = new char;
cin < < ipaddress;

memset ( &addr, 0, sizeof ( SOCKADDR_IN ) );
addr.sin_family = AF_INET;
addr.sin_port = htons ( 27015 );
addr.sin_addr.s_addr = inet_addr ( ipaddress );

if ( connect ( s, ( SOCKADDR* ) &addr, sizeof ( SOCKADDR ) ) == SOCKET_ERROR )
{
cout < < "Failed to connect to server " < < ipaddress < < " (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

cout < < "Connected to server " < < ipaddress < < "..." < < endl;

return true;
}

bool CreateConnectionAsServer ( )
{
SOCKADDR_IN addr;

cout < < "Running server..." < < endl;

SOCKET acceptSocket = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( acceptSocket == INVALID_SOCKET )
{
cout < < "Failed to create socket (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

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

memset ( &addr, 0, sizeof ( SOCKADDR_IN ) );
addr.sin_family = AF_INET;
addr.sin_port = htons ( 27015 );
addr.sin_addr.s_addr = ADDR_ANY;

if ( bind ( acceptSocket, ( SOCKADDR* ) &addr, sizeof ( SOCKADDR_IN ) ) == SOCKET_ERROR )
{
cout < < "Failed to bind socket (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

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

if ( listen ( acceptSocket, 10 ) == SOCKET_ERROR )
{
cout < < "Failed to listen for connections (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

cout < < "Listening..." < < endl;

SOCKET connectedSocket = accept ( acceptSocket, NULL, NULL );

if ( connectedSocket == INVALID_SOCKET )
{
cout < < "Failed to accept incoming connection (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

cout < < "New connection accepted..." < < endl;

return true;
}
[/source]


I'm simply not sure in what way I could debug or error check the code more and the results just don't make any sense to me. Since I'm new to this, I would really appreciate if some of the more experienced netcoders around here could overlook these three simple functions and give me a couple pointers!

Thanks ahead of time! If there's any other info or anything you lot need, let me know and I'll do my best to provide it! Edited by d k h
0

Share this post


Link to post
Share on other sites
If the forum eats another post, you can copy and paste the "edit" link into your browser address bar - it will still work even though clicking on it doesn't.
0

Share this post


Link to post
Share on other sites
Thanks a ton, man! Sorted out very quickly! Using the copy link trick now! Feel free to delete this post as well! Edited by d k h
0

Share this post


Link to post
Share on other sites
[quote name='d k h' timestamp='1340983956' post='4953980'][i]WSAECONNREFUSED[/i]
[/quote]

Sounds like a firewall/port forwarding issue.
0

Share this post


Link to post
Share on other sites
Okay, I've been looking up various articles on this matter (ie. Winsock firewall issue, Winsock port forwarding etc.), it all gets really technical very fast.

I am behind a DSL router that I, unfortunately, don't have access to (shared flat). I am using Windows Vista's build-in firewall. I have allowed my app to go through, of course, but just now, in the security center, I have also added the port my app uses. I will inform my test partner to do the same with whatever firewall he's using. I will be back with updates!

Ultimately, I would really like to do whatever I can with the Winsock API to be able to set up a connection without the user having to forward ports or anything the like. How do the big multiplayer games do this anyways?

Thanks for the help so far.
0

Share this post


Link to post
Share on other sites
The WinSock API won't let you re-configure your DSL firewall/router, which I bet is where the problem is.
Your only option in that case is to hope that the router supports NAT punch-through, and use an external server for NAT introduction. There are links in the FAQ about this.
0

Share this post


Link to post
Share on other sites
The problem is not in your firewall, but on his. When he is running the server his ports are closed or not correctly forwarded which means that you can not connect to his computer where he runs the server software.

tracert works because it keeps sending packets and starts raising TTL from 1 to 30 or more. This means that the first packet comes back from the first router and the second packet from the second router and the third packet from the third router and so on. Most likely will not even reach your friends PC itself. Ping tools then again send ICMP packet which most routers either block or reply to it and ICMP does not require TCP or UDP ports, because it works directly on top of IP.
0

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  
Followers 0