Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Tic Tac Toe with Local Multiplayer function added link problem

multiplayer online tic tac toe

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 googooli86

googooli86

    CC Lurker

  • New Member
  • Pip
  • 9 posts

Posted 20 March 2015 - 11:49 AM

Hi there,

I've made the Tic Tac Toe game.
Now I also added the "Multiplayer" function (LAN - TCP - UDP).
My problem is after I created the Multiplayer menutoolstrip and the dialog for the connection, I have some problems to link it up to the game.

Here is my code:
 

namespace TicTacToe
{
    public partial class Multiplayer : Form
    {

        Socket sck;
        EndPoint epLocal, epRemote;
        public Multiplayer()
        {
            InitializeComponent();

            sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            textLocalIp.Text = GetLocalIP();
            textFriendsIp.Text = GetLocalIP();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            try
            {
                epLocal = new IPEndPoint(IPAddress.Parse(textLocalIp.Text), Convert.ToInt32(textLocalPort.Text));
                sck.Bind(epLocal);

                epRemote = new IPEndPoint(IPAddress.Parse(textFriendsIp.Text), Convert.ToInt32(textFriendsPort.Text));
                sck.Connect(epRemote);

                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);

                buttonStart.Text = "Connected";
                buttonStart.Enabled = false;
          

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private string GetLocalIP()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());

            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }

            return "127.0.0.1";

        }

        private void MessageCallBack(IAsyncResult aResult)
        {
            try
            {
                int size = sck.EndReceiveFrom(aResult, ref epRemote);
                if (size > 0)
                {
                    byte[] receivedData = new byte[1464];

                    receivedData = (byte[])aResult.AsyncState;

                    
                }

                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }

        }

    }
}

Stuck at combining the Multiplayer.cs to the Mainform.cs (The main game)..

Any tip or ideas for letting me continue my work will be appreciated.

Thanks.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download