Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualMr C

Posted 14 March 2013 - 07:00 PM

Hi all. I didn't want to make a post about this but google has proven rather unhelpful.
 
I am in school for Game Programming and I am working on an XNA based tech demo for a class. I am trying to teach myself UDP socket programming with the goal of eventually making multiplayer game. Right now my grasp on socket programming is rudimentary at best though through example I have written a simple chat server/client. I would love to be able to use Lidgren for this but as it is a tech demo I need to write all the code myself, which I am fine with.
 
The end goal of my project is to have two sprites moving around the screen, one separate clients controlled by a server. Right now, I just want my server to tell me which key I am sending it. As of now, all I get is:
 
Received a broadcast from 192.168.x.xxx:59145
Message: NONE
 
On any key press, which at least means it is getting to my server.
 

KeyboardState keyboardState = new KeyboardState();
 
            if (Keyboard.GetState().GetPressedKeys().Length > 0)
            {
                // Default movement is none
                if (!keyboardState.IsKeyDown(Keys.W) || !keyboardState.IsKeyDown(Keys.A) || !keyboardState.IsKeyDown(Keys.S) || !keyboardState.IsKeyDown(Keys.D))
                {
                    MoveDir = MoveDirection.NONE;
                }
                if (keyboardState.IsKeyDown(Keys.W))
                {
                    MoveDir = MoveDirection.UP;
                }
                if (keyboardState.IsKeyDown(Keys.A))
                {
                    MoveDir = MoveDirection.LEFT;
                }
                if (keyboardState.IsKeyDown(Keys.S))
                {
                    MoveDir = MoveDirection.DOWN;
                }
                if (keyboardState.IsKeyDown(Keys.D))
                {
                    MoveDir = MoveDirection.RIGHT;
                }
 
                byte[] send_buffer = Encoding.ASCII.GetBytes(MoveDir.ToString()); 
 
                try
                {
                    sending_socket.SendTo(send_buffer, sending_end_point);
 
                }
                catch (Exception send_exception)
                {
                    exception_thrown = true;
                    Console.WriteLine(" Exception {0}", send_exception.Message);
                }
                if (exception_thrown == false)
                {
                    Console.WriteLine("Message has been sent to the broadcast address");
                }
                else
                {
                    exception_thrown = false;
                    Console.WriteLine("The exception indicates the message was not sent.");
                }
            }
 
That is the chunk of relevant code inside my XNA update function. MoveDir is declared at the start of my file as an instance of Movedirection. 
 

  // Move direction enumerator
    enum MoveDirection
    {
        UP,
        DOWN,
        LEFT,
        RIGHT,
        NONE
    }
is my enum
 
I don't think it matters but my server code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
namespace Server
{
    public class UDPListener
    {
        private const int listenPort = 11000;
 
 
        static int Main(string[] args)
        {
            bool done = false;
 
            UdpClient listener = new UdpClient(listenPort);
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
            string recieved_userName;
            string received_data;
            byte[] receive_name_array;
            byte[] receive_byte_array;
            receive_name_array = listener.Receive(ref groupEP);
           
 
            try
            {
                while (!done)
                {
                    
                    receive_byte_array = listener.Receive(ref groupEP);
                    Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
                    received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
                    Console.Write("Message: {0}\n", received_data);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            listener.Close();
            return 0;
        }
 
    }
}
 
Anyways, like I said I am new to this and trying to figure it all out. My level of experience is a college level data structures course. XNA does not support "true keyboard input" (http://dream-forever.net/Blog/2011/08/29/getting-true-keyboard-input-into-your-xna-games/) and I know there HAS to be a simpler way to get this done besides what is suggested in that post. I don't need it to be pretty, just to work. 
 
If this is a dumb question, I apologize for wasting peoples time. Thanks for any help.
 
Edit: I was unsure if I should post this here or in the networking section, since it has XNA specific stuff I chose here, please move if it is out of place.

#1Mr C

Posted 14 March 2013 - 06:58 PM

Hi all. I didn't want to make a post about this but google has proven rather unhelpful.
 
I am in school for Game Programming and I am working on an XNA based tech demo for a class. I am trying to teach myself UDP socket programming with the goal of eventually making multiplayer game. Right now my grasp on socket programming is rudimentary at best though through example I have written a simple chat server/client. I would love to be able to use Lidgren for this but as it is a tech demo I need to write all the code myself, which I am fine with.
 
The end goal of my project is to have two sprites moving around the screen, one separate clients controlled by a server. Right now, I just want my server to tell me which key I am sending it. As of now, all I get is:
 
Received a broadcast from 192.168.x.xxx:59145
Message: NONE
 
On any key press, which at least means it is getting to my server.
 
KeyboardState keyboardState = new KeyboardState();
 
            if (Keyboard.GetState().GetPressedKeys().Length > 0)
            {
                // Default movement is none
                if (!keyboardState.IsKeyDown(Keys.W) || !keyboardState.IsKeyDown(Keys.A) || !keyboardState.IsKeyDown(Keys.S) || !keyboardState.IsKeyDown(Keys.D))
                {
                    MoveDir = MoveDirection.NONE;
                }
                if (keyboardState.IsKeyDown(Keys.W))
                {
                    MoveDir = MoveDirection.UP;
                }
                if (keyboardState.IsKeyDown(Keys.A))
                {
                    MoveDir = MoveDirection.LEFT;
                }
                if (keyboardState.IsKeyDown(Keys.S))
                {
                    MoveDir = MoveDirection.DOWN;
                }
                if (keyboardState.IsKeyDown(Keys.D))
                {
                    MoveDir = MoveDirection.RIGHT;
                }
 
                byte[] send_buffer = Encoding.ASCII.GetBytes(MoveDir.ToString()); 
 
                try
                {
                    sending_socket.SendTo(send_buffer, sending_end_point);
 
                }
                catch (Exception send_exception)
                {
                    exception_thrown = true;
                    Console.WriteLine(" Exception {0}", send_exception.Message);
                }
                if (exception_thrown == false)
                {
                    Console.WriteLine("Message has been sent to the broadcast address");
                }
                else
                {
                    exception_thrown = false;
                    Console.WriteLine("The exception indicates the message was not sent.");
                }
            }
 
That is the chunk of relevant code inside my XNA update function. MoveDir is declared at the start of my file as an instance of Movedirection. 
 
  // Move direction enumerator
    enum MoveDirection
    {
        UP,
        DOWN,
        LEFT,
        RIGHT,
        NONE
    }
is my enum
 
I don't think it matters but my server code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
namespace Server
{
    public class UDPListener
    {
        private const int listenPort = 11000;
 
 
        static int Main(string[] args)
        {
            bool done = false;
 
            UdpClient listener = new UdpClient(listenPort);
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
            string recieved_userName;
            string received_data;
            byte[] receive_name_array;
            byte[] receive_byte_array;
            receive_name_array = listener.Receive(ref groupEP);
           
 
            try
            {
                while (!done)
                {
                    
                    receive_byte_array = listener.Receive(ref groupEP);
                    Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
                    received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
                    Console.Write("Message: {0}\n", received_data);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            listener.Close();
            return 0;
        }
 
    }
}
 
Anyways, like I said I am new to this and trying to figure it all out. My level of experience is a college level data structures course. XNA does not support "true keyboard input" (http://dream-forever.net/Blog/2011/08/29/getting-true-keyboard-input-into-your-xna-games/) and I know there HAS to be a simpler way to get this done besides what is suggested in that post. I don't need it to be pretty, just to work. 
 
If this is a dumb question, I apologize for wasting peoples time. Thanks for any help.

PARTNERS