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

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
This may be odd, but can anyone tell me what the code for using WinSock in SGDK2 would look like? I would like to implement some netgame feature into my game. Even just the sourcecode for a message chat or the code for passing any kind of value from one computer to another via the Internet would work. I would like to build off on there. If anyone could make the SGDK2 Code to send data from Computers and recieve it Via the Internet (Whether it's just text or numbers or something full end like the Netgame Sample from SGDK1) That would help. Thank you. (I couldnt get the netgame thing from SGDK1 to help so Im hoping sgdk2 hes something up my alley

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
See:

Let me know how far those get you and I can investigate further from there.
« Last Edit: 2010-12-28, 06:02:27 AM by bluemonkmn »

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Well I started my code like this.

namespace System.Net.Sockets
{
        public class TcpClient : IDisposable

It gave me errors about implementing IDisposable.

So This became my code For Hosting a Server:

using System.ComponentModel;
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace NetGames
{
class MyTcpListener
{
[Description("Host a Netgame")]
   public static void Host()
      { 
         TcpListener server=null;
         try
         {
         Int32 port = 5096;
         IPAddress localAddr = IPAddress.Parse("127.0.0.1");

server = new TcpListener(localAddr, port);

server.Start();

Byte[] bytes = new Byte[256];
String data = null;

while(true)
{
      Console.Write("Waiting for a connection... ");

TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

data = null;

NetworkStream stream = client.GetStream();

int i;

while ((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Recieved:
  • ", data);


data = data.ToUpper();

byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent; {0}", data);
}

client.Close();
}
}
catch (SocketException e)
{
   Console.WriteLine("SocketException: {0}", e);
}
finally
{
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
}

Mostly Derived from the Msdn, It works with no Errors in sgdk2. I call it in one of the Plans. Its a connection test. :D However it freezes when using it. Where Did I go wrong? How would the code for sending and recieving player Inputs across the internet look?



« Last Edit: 2010-12-28, 05:29:45 PM by Pizzaman »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
It freezes because you have a while(true) loop that never exits.  That function, the way you have written it, will be 100% focused on only reading from the network and will never return to the caller, especially if nothing on the network even tries to connect to it.  Generally that kind of stuff would be done on a separate thread, I guess.  I suppose I will have to work out a sample.  Hopefully Friday if not before.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Thank you so much Bluemonkmn. It really means alot to me. And thanks for the diagnosis. :D

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Attached is an SGDK2 project that networks up to 3 players by simply replacing the inputs for the players that are not the local player with an object that gets inputs from the network.  Once the Net.cs file is in the project, all you have to do is have a plan that calls NetworkPlayers once per frame, and it will map the players from the network into the inputs for the players in this game.  Then you map the inputs of all the players to sprites like normal and viola. Inputs from the network are controlling sprites.

I tested it for 2 and 3 players all running on a local machine, but I noticed that player 3 could see the other two players, but not actually play for some reason.  I don't have time to investigate further right now, but the simplicity of this solution is pretty cool.  It worked pretty well to have players be generic objects because I could just replace them with network players and you don't need to change any SGDK2 code.  Although I did change one line of SGDK2 code for testing: I made it so that the game will continue to run when the window is inactive, otherwise I can't have two windows running on the same machine playing with each other.  One is always inactive causing the whole game to freeze.  So I commended out the line that pauses the game with the window is inactive in GameForm.cs.

Edit:  Attachment updated.  All 3 players now work correctly.

Edit 2: Attachment updated.  I hope Net.cs is compatible with .NET 1.1 now
« Last Edit: 2010-12-31, 03:07:17 PM by bluemonkmn »

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Thank you bluemonkmn. This might sound like kind of a drag, but how would you make this work in sgdk 2.0? My display drivers don't support the GL_ARB texture rectangle so I'm using version 2.0, Sadly this project doesn't run in 2.0. What sinple changes to the code do I need to make it run? Importing it in the earlier version doesnt help either...

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
I'd rather figure out how to make 2.1 work on your system than figure out how to get back to 2.0.  DirectX must be doing something that we could do with OpenGL if we knew what to do... unless the OpenGL drivers for your card are just that much stupider than the DirectX drivers.  So two things to try:
1. Check for updated drivers for your card again to see if there's anything more recent available now.
2. Try removing the GL_ARB reliance from the code... not sure how to do this yet, but if you can't find drivers that work, we can investigate this.  Do you have and OpenGL utility that shows what all the openGL features your card *does* support.  There must be one available somewhere.  I want SGDK2.1 to work on as wide a variety of systems as possible.  So with your help I hope we can figure out what needs to be done to make it work as well as it does with DirectX.  Maybe the only choice is to make sure the graphic sheet is a power of 2 pixels in size, and then avoid using GL_ARB.  I thought GL_ARB was to allow arbitrary texture sizes, but I'm not so sure any more.  Perhaps we should just remove the requirement and see what happens. What happens if you ignore the warning message (it doesn't force you to quit does it?  is still tried to function?).  (Note: http://www.opengl.org/wiki/NPOT_Texture, http://www.opengl.org/registry/specs/ARB/texture_rectangle.txt)

Ooh, also, it's theoretically possible to use DirectX in SGDK 2.1... that might be another option to look into.
« Last Edit: 2010-12-30, 06:18:35 AM by bluemonkmn »

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
The error I get in the NetSample is :

 The GL_ARB Texture Rectangle  that happens in the Map editor usually,

Ive tried this on two computers.

X60 Lenovo Thinkpad, Windows 7, the utility I used "OpenGL Extensions Viewer 3.0" Says all the drivers are up to date and passes all GL tests up to OpenGL 1.4
Compaq Prescario.. something. I just bricked it accidentally yesterday and have yet to use Windows Recovery... This was my old Computer for running SGDK1 and SGDK2
It had a VIA/S3G Unichrome IGP Display driver Up to date, and supported OpenGL...



Both Ran 2.0 flawlessly, but give the same errors on 2.1

It gives me the option to Continue or Quit the application, but continuing just leaves me with... Well... Imagine the Ugly Hall of Mirrors Effect that occurs when a Map has no background..
I cant see any sprites, No music is run. Just a glitched Application window or a Glitched Map editor.

After 2 weeks of trying to get 2.1 to work, I just went back to 2.0. If theres any way for me to edit the code to remove the error, that would help, I don't want to "break" anything else I thnk my PC is already breaking.

The portability of my game on a large amounts of computers running Windows, (I find my potential players run Windows Based OS's) is essential, as this will be a Mainly Online Based Game, Sort of like an MMORPG. Even If I fix this problem for me, Having to get 20 or so more people (At least I hope so for the release :P) to have to do similar processes may discourage people from playing. I could include a Support HTML file... but I dont know if the solution to this would be a simple 10 step process, more of a guess and check solution.

I find many of my players can run the DirectX projects, rather the OpenGL.
The only computer Ive been able to get 2.1 to run on is my Technical School  Teachers Computer. He has high end graphics cards that the school decided to splurge on, and it is a CAD class...
He uses Nvidia I think...

I'm going to try to play with things -_- But I doubt it'll work after my past 2 weeks.
I would love to help you run 2.1 on a vast amount of computers.

*I forgot* The error I get runing the Net.cs in 2.0 was:

 error CS0116: A namespace does not directly contain members such as fields or methods Line 119, character 4
« Last Edit: 2010-12-30, 07:48:04 PM by Pizzaman »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
It may be a few days before I have time to investigate DirectX again.  But I do hope to look into that.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
*I forgot* The error I get runing the Net.cs in 2.0 was:

 error CS0116: A namespace does not directly contain members such as fields or methods Line 119, character 4

Try removing the word "static" from line 119 (Ctrl-G to go to line 119).  It's the line that says "static class Net".

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
d:\C#\Sewnwerars\Game\fmodbase.cs(89,10) : warning CS0168: The variable 'ex' is declared but never used
d:\C#\Sewnwerars\Game\Snow.cs(169,18) : warning CS0168: The variable 'ex' is declared but never used
d:\C#\Sewnwerars\Game\Net.cs(33,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
d:\C#\Sewnwerars\Game\Net.cs(39,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
d:\C#\Sewnwerars\Game\Net.cs(52,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
d:\C#\Sewnwerars\Game\Net.cs(63,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
d:\C#\Sewnwerars\Game\Net.cs(75,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
d:\C#\Sewnwerars\Game\Net.cs(196,13) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
d:\C#\Sewnwerars\Game\Net.cs(245,23) : error CS0117: 'string' does not contain a definition for 'IsNullOrEmpty'
d:\C#\Sewnwerars\Game\Net.cs(305,10) : error CS0117: 'System.Windows.Forms.Button' does not contain a definition for 'UseVisualStyleBackColor'
d:\C#\Sewnwerars\Game\Net.cs(329,10) : error CS0117: 'CustomObjects.NetParams' does not contain a definition for 'AutoScaleDimensions'
d:\C#\Sewnwerars\Game\Net.cs(330,10) : error CS0117: 'CustomObjects.NetParams' does not contain a definition for 'AutoScaleMode'

(The Errors I get when it runs) ↑ ^^^                 

c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.3.cs(89,10) : warning CS0168: The variable 'ex' is declared but never used
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.30.cs(169,18) : warning CS0168: The variable 'ex' is declared but never used
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(33,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(39,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(52,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(63,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(75,10) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(196,13) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(245,23) : error CS0117: 'string' does not contain a definition for 'IsNullOrEmpty'
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(305,10) : error CS0117: 'System.Windows.Forms.Button' does not contain a definition for 'UseVisualStyleBackColor'
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(329,10) : error CS0117: 'CustomObjects.NetParams' does not contain a definition for 'AutoScaleDimensions'
c:\Users\David Thomas\AppData\Local\Temp\ampxscrj.33.cs(330,10) : error CS0117: 'CustomObjects.NetParams' does not contain a definition for 'AutoScaleMode'

The errors I get when trying to look at the rule list ^^^ ↑

Seems that removing 'static' presents more errors...

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
I think you're getting these errors because SGDK 2.1 was upgraded to use .NET 2.0, but SGDK 2.0 and you are still back on .NET 1.1.  I updated the attachment above with a version of Net.cs that takes care of all the errors you listed.  Hopefully that's all of them.  Here are the changes I made:
1. Add a declaration for "NetworkStream stream;" after "TcpClient client;".
2. Add "stream = client.GetStream();" after "this.client = client;".
3. Change client.Client.Receive calls to stream.Read:
  a. Change the one in ReceivePlayerInputs to "stream.Read(inputBuffer, 0, inputBuffer.Length);"
  b. Change the one in ReadOtherPlayerInputs to "stream.Read(playerInputBuffer, 0, playerInputBuffer.Length);".
  c.
4. Change client.Client.Send calls stream.Write:
  a. Change the one in SendPlayerInputs to "stream.Write(playerInputBuffer, 0, playerInputBuffer.Length);".
  b. Change the one in SendMyInputs to "stream.Write(inputBuffer, 0, inputBuffer.Length);".
  c. Change the one in SendPlayerNumber to "stream.Write(playerInputBuffer, 0, playerInputBuffer.Length);".
5. Change the line that contained IsNullOrEmpty to "if ((frmNp.HostName == null) || (frmNp.HostName.Length == 0))"
6. Delete the other lines that were referenced:
  a. Delete the line containing "UseVisualStyleBackColor"
  b. Delete the line containing "AutoScaleDimensions"
  c. Delete the line containing "AutoScaleMode".

I think that was everything.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
If I knew more about C# I would love to stop bothering you.. and because of every post and attachment you've made for the sake of my game I thank you. You have done a lot for me. Thank you very much for devoting time out of your day for me, bluemonkmn.

I still get 2 errors.

c:\Users\David Thomas\AppData\Local\Temp\1woygti1.3.cs(89,10) : warning CS0168: The variable 'ex' is declared but never used
c:\Users\David Thomas\AppData\Local\Temp\1woygti1.27.cs(198,13) : error CS0122: 'System.Net.Sockets.TcpClient.Client' is inaccessible due to its protection level

You sir, are a genius. Creating with ease code that would take me months, in only a day or two.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Go to line 198 (with Ctrl-G and type 198) and replace these lines:

Code: [Select]
            byte[] playerInfo = new byte[5];
            connector.Client.Receive(playerInfo);

with this:

Code: [Select]
            byte[] playerInfo = client.ReadOtherPlayerInputs();