Author Topic: WinSock in SGDK2, or any way or Sending info across the internet with SGDK2  (Read 5676 times)

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Works like a charm! Thanks! Do I need to forward my ports to a certain port to host?

http://i55.tinypic.com/2wq7a6c.png

One of my clients sent me a picture with this error.

I changed the port in the code to 2976, and forwarded my Computers ports to 2976.

Does SGDK2 Lock ports? Because when I host and  use a Port Checker to see if its open, It says port 2976 is wide open on both TCP and UDP.

Forwarding my ports did help however, Because connecting to anyone without Forwarded Ports or a Firewall gives me a message saying the host ABSOLUTELY REFUSED connection

Seems like a Firewall issue? My firewall is set to allow my Executable to run
« Last Edit: 2011-01-01, 04:59:43 PM by Pizzaman »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Well, SGDK2 itself doesn't do anything with port numbers, of course, but the Net.cs class does.  And it is just using the most straightforward socket access I could find.  Under the covers, however, it might be doing something more complicated.  Generally when you have a TCP listener on a particular port, it hears a connection request and then communicates another port number that can be used for that TCP channel so that it can continue to listen for other connections on that port.  I'm not sure if that's what's happening here, but if so, it's possible that your port for listening is routed properly, but the port that the listener assigned for the actual TCP channel after connected was not.

It looks like some details are described here: http://stackoverflow.com/questions/1415682/tcplistener-accepttcpclient-and-firewall.  And others here: http://bytes.com/topic/c-sharp/answers/544315-tcplistener-tcpclient-not-working-over-nat

It's all pure .NET framework so I don't have much more info about it that than you.  Networking can get complicated.  There's the router and the computer to deal with and there are NAT and Firewall rules to deal with too.  And there's probably more.  You might want to try some simpler networking programs or diagnostic tools to see if they help identify what kind of connections are possible between a particular pair of computers rather than trying to figure it out via running the game directly.

Edit: Maybe you need to enable the AllowNatTraversal property on the TcpListener before it starts listening: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.allownattraversal.aspx. ???

Edit 2: One more thing.  The client should not be including a port number, only an IP address in the server name field (when the user types an address in).  I don't know if that's an issue or not. But the port number for both server and client is set by the code and should not be included in the address.

Edit 3: Unfortunately AllowNatTraversal is not available on .NET 1.1... it's not available until .NET 4.  Not sure what to suggest exploring first... upgrading to .NET 4 or determining the actual cause of the problem and finding an alternative or more correct solution.

Edit 4: Also see http://blogs.msdn.com/b/ncl/archive/2009/07/27/end-to-end-connectivity-with-nat-traversal-.aspx
« Last Edit: 2011-01-02, 11:16:07 AM by bluemonkmn »

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Thanks for the info :(. This may be an odd question, but could a program like Hamachi allow connectivity?

Unrelated: I was also going to modify the net.cs to send chatroom messages between the host and the client server, like an IRC in game. :D Which would be the wisest way of showing the text, the Message code? Or a Message Box simular to the net.cs ip prompt? Its windowed.
« Last Edit: 2011-01-02, 08:33:56 PM by Pizzaman »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
I haven't heard of Hamachi, but it looks like something worth trying.  I would find some other simple test program(s) before Hamachi, though, to see if the problem can be replicated in any other software.

I would suggest something similar to LogDebugLabel for drawing messages from players, but of course it would operate even in non-debug mode.  Most of the code you need is actually in GameForm in the OutputDebugInfo.  You could copy that into custom code, rename it and remove the "Conditional("DEBUG")" line so that you can use it in any mode.  No time to explain in detail at the moment.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Does this issue for the netgame even seem fixable? Or should I abandon all hope and try making a Game like this thats browserbased with JS and PHP etc...

Funny enough, a netgame works if the IP is 127.0.0.1.. On the same machine :/

But if the host window is paused the client gets kicked. :O I couldnt find the line that you said you commented out in GameForm.cs
« Last Edit: 2011-01-03, 12:52:49 PM by Pizzaman »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Does this issue for the netgame even seem fixable? Or should I abandon all hope and try making a Game like this thats browserbased with JS and PHP etc...

I can't tell yet.  It could be fixable, but you need to run some other diagnostics or at least other network games on the pair of machines to see if others have made it work.  If others can make it work, we should be able to make it work.  Did you look at Hamachi yet?  If you can't find existing software and utilities to test with, maybe you want to get C# Express (if you don't already have it; it's free) and try going through this tutorial.  It would help in understanding the code better anyway:
C# Tutorial - Simple Threaded TCP Server

You need something simpler than the game to test the connection with.  I could write something if you can't find or write something.

But if the host window is paused the client gets kicked. :O I couldnt find the line that you said you commented out in GameForm.cs

I changed this line:

Code: [Select]
         isActive = (System.Windows.Forms.Form.ActiveForm == this);

to this:
Code: [Select]
         isActive = true; //(System.Windows.Forms.Form.ActiveForm == this);

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Did you give up on making a network game with SGDK2, or did you get it working?

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Im starting from scratch now. Learning Java and eventually going to learn C# :D Last thing I want to be is a Help Vampire

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Ive discovered the possible issue, its that the firewall refuses all external connections on the port. Happily I finally got it to work with my external Address.
I added this on line 146
Code: [Select]
host.AllowNatTraversal(true);Changed the Address for listening to my Network Address
(192.168.1.103)
Then I added a firewall exception for my EXE.

Now, the only issue is the Framerate. I have to further investigate this, but as long as It can connect, I at least have a base for what I can use for my game.
Ill have to prompt the use for their Network IP or use some other means of locating it, as well as code that Uses FirewallAPI.dll to automatically add an exception (If It can do that, but I read it in the article you sent :) )

Thanks for the code!

EDIT: Im using SGDK 2.1.9 for this, with the NET Framework 4.0 even though it might already be obvious :)

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Wow -- look at that self-sufficiency... way to go!

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
My only concern now is really making code that downloads a SaveGame from the host. To do this, I figured everytime the host gets a join, It saves the current save so that joiners can download. Ive never worked with savegames. Do they save the position of the sprites and the Current Tiles of the Map (as for counters)? Can they be altered in a compiled close source game? It seems simpler than the Dynamic Map Loading thing durund was talking about. The game is simular to Minecraft/Terreria, so the Tiles in the Map Are very important, as well as the counters processing daytime/nighttime and inventories etc, so they desperately need to be sync'ed. But then again there might be difficulty downloading a save file from one PC ingame, and storing it to a file the client loads before the host syncs inputs. If I created a map called Network, upon savegame load would the Map generate the tiles from the downloaded Savegame (If the size for the map remained constant for client and host- after being defined by the game already)?

Concerns aside, My only cry for help now is challenging the possibility of said idea. Does it even seem possible? Am I making this too complicated?

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Saved game functions should be a good way to synchronize the maps except that you will somehow have to keep track of the separate players since you don't want everyone's map to be *exactly* the same.  You want player 2 to be controlling a different sprite than player 1.  But yes, all the tiles and sprites are saved if you don't explicitly exclude/include a limited subset of info in the save unit with IncludeInSaveUnit.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Speaking of 2D games like minecraft, have you heard of 2dcraft?  I just answered a question for it on stackoverflow.com and it looks like an interesting game.  But I haven't investigated much yet.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Whew, thats a relief, and yes, Ive actually seen something EXACTLY like that 2Dcraft

http://www.youtube.com/watch?v=_fI68a_hGGg&feature=related

Made in GameMaker LOL. But the feel Im going for...well I cant explain it until I release an alpha of my game. Its similar in some sense but different, combining 2D elements from Terarria, Minecraft, with Crafting based on the Android Game: Alchemy. Multiplayer will be the last thing Ill add after a SinglePlayer, Coop and Sandbox Mode, but its good to plan ahead.

After all, adding multiplayer increases re-playability, an issue I have with most games Ive played or made.

Ill try to post what I have so far in the Projects category soon so you can see.