BackgroundWorker and Sockets

本文关键字:Sockets and BackgroundWorker | 更新日期: 2023-09-27 18:28:14

描述:

计划在周末制作一个简单的多人乒乓球游戏。它花了4个小时来编写/设计游戏物理&UI。花了一整天的时间研究这个代码,但无法检测/修复它的问题。


问题:

找不到它不工作的原因,它卡在Reveicva bgWorker中的int bytesRead = accepted.Receive(Buffer1);上,而Senda bgWorker没有运行。

这是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace LePong
{
    public partial class Form1 : Form
    {
        int speedTop = 3, speedLeft = 5;
        int playerRight = 0, playerLeft = 0;
        bool leRuler = true;
        static byte[] Buffer1 { get; set; }
        public int rckt2Pos;
        public Form1()
        {
            InitializeComponent();
            //Sets Cursor at Top of Racket
            Point p = new Point(racket1Main.Left / 2, racket1Top.Bottom);
            Cursor.Position = p;
            Cursor.Hide();
                Receivea.RunWorkerAsync();
                    Senda.RunWorkerAsync();
        }
        private void Receivea_DoWork(object sender, DoWorkEventArgs e)
        {
            Socket sckt1;
            sckt1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sckt1.Bind(new IPEndPoint(0, 1234));
            Socket accepted;
            while (true)
            {
                if (leRuler == true)
                {
                    sckt1.Listen(100);
                    accepted = sckt1.Accept();
                    Buffer1 = new byte[accepted.SendBufferSize];
                    int bytesRead = accepted.Receive(Buffer1);
                    string strData;
                    byte[] formatted = new byte[bytesRead];
                    for (int i = 0; i < bytesRead; i++)
                    {
                        formatted[i] = Buffer1[i];
                    }
                    strData = Encoding.ASCII.GetString(formatted);
                    try
                    {
                        racket2Main.Top = int.Parse(strData);
                        leRuler = false;
                    }
                    catch { }
                }
            }
        }
        private void Senda_DoWork(object sender, DoWorkEventArgs e)
        {
            Socket sckt2;
            string text;
            sckt2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
            try
            {
                sckt2.Connect("127.0.0.1", 1234);
            }
            catch { }
            while (true)
            {
                if (leRuler == false)
                {
                    text = Cursor.Position.Y + "";
                    byte[] data = Encoding.ASCII.GetBytes(text);
                    sckt2.Send(data);
                    leRuler = true;
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Receivea.IsBusy == false)
                Receivea.RunWorkerAsync();
            if (Senda.IsBusy == false)
                Senda.RunWorkerAsync();
            //Sets Racket1 Postion
            racket1Main.Top = Cursor.Position.Y;
            racket1Back.Top = racket1Top.Top;
            racket1Top.Top = racket1Main.Top - racket1Top.Height;
            racket1Bottom.Top = racket1Main.Bottom;
            //Sets Racket2 Postion On Screen
            racket2Top.Left = playground.Right - racket1Top.Width - 12;
            racket2Main.Left = playground.Right - racket1Main.Width - 12;
            racket2Bottom.Left = playground.Right - racket1Main.Width - 12;
            racket2Back.Left = playground.Right - racket1Back.Width - 12;
            //Sets Racket2 Postion
            //racket2Main.Top = Cursor.Position.Y;
            racket2Back.Top = racket2Top.Top;
            racket2Top.Top = racket2Main.Top - racket2Top.Height;
            racket2Bottom.Top = racket2Main.Bottom;
            playerLeftScore.Left = racket2Main.Left + 3;
            //Sets Scores On Rackets
            if (playerLeft > 0)
                playerLeftScore.Text = "" + playerLeft;
            if (playerRight > 0)
                playerRightScore.Text = "" + playerRight;
            playerLeftScore.Top = racket2Main.Top + 25;
            playerRightScore.Top = racket1Main.Top + 25;
            //Sets Ball Speed/Postion
            Ball.Top += speedTop;
            Ball.Left += speedLeft;
            //Checks If Ball Hits Screen Bounds
            if (Ball.Top <= playground.Top) { speedTop = -speedTop; }
            if (Ball.Bottom >= playground.Bottom) { speedTop = -speedTop; }
            if (Ball.Right >= playground.Right) { playerRight++; speedLeft = -speedLeft; speedTop = -speedTop; Ball.Top = playground.Height / 2 - Ball.Height / 2; Ball.Left = playground.Width / 2 - Ball.Width / 2; }
            if (Ball.Left <= playground.Left) { playerLeft++; speedLeft = -speedLeft; speedTop = -speedTop; Ball.Top = playground.Height / 2 - Ball.Height; Ball.Left = playground.Width / 2 - Ball.Width / 2; }
            //Checks If Ball Hits Racket1
            if (Ball.Bounds.IntersectsWith(racket1Top.Bounds)) { speedTop = -3; speedLeft = 5; }
            if (Ball.Bounds.IntersectsWith(racket1Main.Bounds)) { speedTop = speedTop - speedTop / 2; speedLeft = 5; }
            if (Ball.Bounds.IntersectsWith(racket1Bottom.Bounds)) { speedTop = 3; speedLeft = 5; }
            //Checks If Ball Hits Racket2
            if (Ball.Bounds.IntersectsWith(racket2Top.Bounds)) { speedTop = -3; speedLeft = -5; }
            if (Ball.Bounds.IntersectsWith(racket2Main.Bounds)) { speedTop = speedTop - speedTop / 2; speedLeft = -5; }
            if (Ball.Bounds.IntersectsWith(racket2Bottom.Bounds)) { speedTop = 3; speedLeft = -5; }


        }

    }
}

BackgroundWorker and Sockets

只需使用Socket的异步方法。–nbsp;Servy

非常感谢,这就是我想要的Varand-Vartanian