Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411509 Posts in 69375 Topics- by 58430 Members - Latest Member: Jesse Webb

April 26, 2024, 11:37:27 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Lidgren - Can't run the examples
Pages: [1]
Print
Author Topic: Lidgren - Can't run the examples  (Read 1515 times)
thekill473
Level 1
*


View Profile
« on: March 25, 2015, 08:55:25 PM »

Hey everybody. At the moment I'm experimenting with Lidgren and XNA.
Whenever I try to call NetServer.Start() I get a SocketException.

Here's my code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using Lidgren.Network;

namespace Gravel
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        NetClient client;
        Texture2D playerTexture;
        Dictionary<long, NetworkedPlayer> players = new Dictionary<long, NetworkedPlayer>();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            NetPeerConfiguration config = new NetPeerConfiguration("Gravel");
            config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);

            client = new NetClient(config);
            client.Start();
        }

        protected override void Initialize()
        {
            client.DiscoverKnownPeer("10.118.0.1", 9999);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            playerTexture = Content.Load<Texture2D>("Player");
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            client.Shutdown("bye");
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // Movement Code
            int xInput = 0;
            int yInput = 0;

            if (Keyboard.GetState().IsKeyDown(Keys.W))
                yInput -= 1;
            else if (Keyboard.GetState().IsKeyDown(Keys.S))
                yInput += 1;

            if (xInput != 0 || yInput != 0)
            {
                NetOutgoingMessage message = client.CreateMessage();
                message.Write(xInput);
                message.Write(yInput);
                client.SendMessage(message, NetDeliveryMethod.Unreliable);
            }

            // Read Messages
            NetIncomingMessage incomingMessage;

            while ((incomingMessage = client.ReadMessage()) != null)
            {
                if (incomingMessage.MessageType == NetIncomingMessageType.DiscoveryResponse)
                {
                    client.Connect(incomingMessage.SenderEndPoint);
                }
                else if (incomingMessage.MessageType == NetIncomingMessageType.Data)
                {
                    long who = incomingMessage.ReadInt64();
                    int x = incomingMessage.ReadInt32();
                    int y = incomingMessage.ReadInt32();

                    if (!players.Keys.Contains(who))
                    {
                        players.Add(who, new NetworkedPlayer(playerTexture));
                    }

                    players[who].position = new Vector2(x, y);
                }
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            // Draw All Players
            foreach (NetworkedPlayer player in players.Values)
            {
                player.Draw(spriteBatch);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

The error message:
Code:
System.Net.Sockets.SocketException was unhandled
  HResult=-2147467259
  Message=An invalid argument was supplied
  Source=System
  ErrorCode=10022
  NativeErrorCode=10022
  StackTrace:
       at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
       at Lidgren.Network.NetPeer.InitializeNetwork() in l:\Lidgren Stuff\lidgren-network-gen3-2013-05-07\Lidgren-network-gen3\Lidgren.Network\NetPeer.Internal.cs:line 117
       at Lidgren.Network.NetPeer.Start() in l:\Lidgren Stuff\lidgren-network-gen3-2013-05-07\Lidgren-network-gen3\Lidgren.Network\NetPeer.cs:line 139
       at Gravel.Game1..ctor() in l:\C++\Projects\Tutorial\RPG\Gravel\Gravel\Gravel\Game1.cs:line 34
       at Gravel.Program.Main(String[] args) in l:\C++\Projects\Tutorial\RPG\Gravel\Gravel\Gravel\Program.cs:line 13
  InnerException:

I can't find any help on Google and I'm at a dead end. Any help would be appreciated.

Thanks for reading.
Logged
riksteri
Level 0
**



View Profile
« Reply #1 on: March 26, 2015, 07:15:32 AM »

I can't help much, but maybe this provides a starting point for someone else to continue.

I would update the Lidgren version, as it seems you're using one from almost two years ago.

Are you referencing a Lidgren assembly that is built with the same configuration (Debug/Release) as your game code? Have you made modifications to the Lidgren source code?

I assume you have not and am looking at the source code from the trunk of the Lidgren revision you apparently are using. The stack trace tells us the exception gets thrown by a line of code that looks like this:

Code:
m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

MSDN documentation states that the constructor throws a SocketException when "the combination of addressFamily, socketType, and protocolType results in an invalid socket." InterNetwork, Dgram and Udp together should be a valid combination, not to mention the unlikelyhood such an error would not have been detected before a release. In other words, the docs are not much help here.

HResult -2147467259 is just "Unspecified error." so that doesn't tell us anything either.

Have you tried running the same code on another machine? Have you tried running it with administrator rights? I'm guessing this is something local to your machine, a firewall preventing the creation of sockets or some such. That line of code has no variables in it, the same constant values are used for everyone using it, and yet it's working fine for many users of Lidgren.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic