trying to move pictureBox from remote client data but the pictureBox vanishes -how can I move the pictureBox...












0














I'm trying to move the picture box on Host(a simple server) screen through data coming from client computer. It's a simple pong game which is multiplayer. Client sends data whenever the keys are pressed. It mainly sends the Y axis location of the pictureBox but I get error when I try to change the location of picture box.
My client code which sends the Y axis location of the player1 pictureBox to server player



private void keyisdown(object sender, KeyEventArgs e)
{



        if (e.KeyCode == Keys.Up)
{
goup = true;


String s = Convert.ToString(player.Location.Y);

byte byteTime = Encoding.ASCII.GetBytes(s);

ns.Write(byteTime, 0, byteTime.Length);
}
if (e.KeyCode == Keys.Down)
{
godown = true;

String s = Convert.ToString(player.Location.Y);

byte byteTime = Encoding.ASCII.GetBytes(s);

ns.Write(byteTime, 0, byteTime.Length);
}


}


My server program which is receiving the data



public partial class Form1 : Form
{



delegate void SetTextCallback(string text);

TcpListener listener;

TcpClient client;

NetworkStream ns;

Thread t = null;


public Form1()
{
InitializeComponent();

listener = new TcpListener(IPAddress.Any, 4545);

listener.Start();

client = listener.AcceptTcpClient();

ns = client.GetStream();

t = new Thread(DoWork);

t.Start();
}

private void button1_Click(object sender, EventArgs e)
{
String s = textBox2.Text;

byte byteTime = Encoding.ASCII.GetBytes(s);

ns.Write(byteTime, 0, byteTime.Length);
}
public void DoWork()

{

byte bytes = new byte[1024];

while (true)

{

int bytesRead = ns.Read(bytes, 0, bytes.Length);
string data;
this.SetText(Encoding.ASCII.GetString(bytes, 0, bytesRead));

// data = (Encoding.ASCII.GetString(bytes, 0, bytesRead));

// int Y = Convert.ToInt32(data);

// pictureBox1.Location = new Point(769, Y);
}

}

private void SetText(string text)

{

// InvokeRequired required compares the thread ID of the

// calling thread to the thread ID of the creating thread.

// If these threads are different, it returns true.

if (this.pictureBox1.InvokeRequired)

{

SetTextCallback d = new SetTextCallback(SetText);

this.Invoke(d, new object { text });


}

else

{

this.pictureBox1.Location = new Point(769, Convert.ToInt32( text));

}

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}


}



im receiving the Y location of client players picture box and it receives it but somehow it doesnt move correctly it just vanishes when it gets Y location the size of server n client program is same and the locations are same too so it should move like mirror



This is the game I'm trying to make left is player1 and right is player2



I also need help as to how can I update the ball position to both players and how can I properly implement multiplayer, right now I'm trying to slowly change the simple chat client code and get the data I need. but how can I get a constant player update every 20 or 50ms I'm very confused. Thanks in advance.










share|improve this question





























    0














    I'm trying to move the picture box on Host(a simple server) screen through data coming from client computer. It's a simple pong game which is multiplayer. Client sends data whenever the keys are pressed. It mainly sends the Y axis location of the pictureBox but I get error when I try to change the location of picture box.
    My client code which sends the Y axis location of the player1 pictureBox to server player



    private void keyisdown(object sender, KeyEventArgs e)
    {



            if (e.KeyCode == Keys.Up)
    {
    goup = true;


    String s = Convert.ToString(player.Location.Y);

    byte byteTime = Encoding.ASCII.GetBytes(s);

    ns.Write(byteTime, 0, byteTime.Length);
    }
    if (e.KeyCode == Keys.Down)
    {
    godown = true;

    String s = Convert.ToString(player.Location.Y);

    byte byteTime = Encoding.ASCII.GetBytes(s);

    ns.Write(byteTime, 0, byteTime.Length);
    }


    }


    My server program which is receiving the data



    public partial class Form1 : Form
    {



    delegate void SetTextCallback(string text);

    TcpListener listener;

    TcpClient client;

    NetworkStream ns;

    Thread t = null;


    public Form1()
    {
    InitializeComponent();

    listener = new TcpListener(IPAddress.Any, 4545);

    listener.Start();

    client = listener.AcceptTcpClient();

    ns = client.GetStream();

    t = new Thread(DoWork);

    t.Start();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    String s = textBox2.Text;

    byte byteTime = Encoding.ASCII.GetBytes(s);

    ns.Write(byteTime, 0, byteTime.Length);
    }
    public void DoWork()

    {

    byte bytes = new byte[1024];

    while (true)

    {

    int bytesRead = ns.Read(bytes, 0, bytes.Length);
    string data;
    this.SetText(Encoding.ASCII.GetString(bytes, 0, bytesRead));

    // data = (Encoding.ASCII.GetString(bytes, 0, bytesRead));

    // int Y = Convert.ToInt32(data);

    // pictureBox1.Location = new Point(769, Y);
    }

    }

    private void SetText(string text)

    {

    // InvokeRequired required compares the thread ID of the

    // calling thread to the thread ID of the creating thread.

    // If these threads are different, it returns true.

    if (this.pictureBox1.InvokeRequired)

    {

    SetTextCallback d = new SetTextCallback(SetText);

    this.Invoke(d, new object { text });


    }

    else

    {

    this.pictureBox1.Location = new Point(769, Convert.ToInt32( text));

    }

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }


    }



    im receiving the Y location of client players picture box and it receives it but somehow it doesnt move correctly it just vanishes when it gets Y location the size of server n client program is same and the locations are same too so it should move like mirror



    This is the game I'm trying to make left is player1 and right is player2



    I also need help as to how can I update the ball position to both players and how can I properly implement multiplayer, right now I'm trying to slowly change the simple chat client code and get the data I need. but how can I get a constant player update every 20 or 50ms I'm very confused. Thanks in advance.










    share|improve this question



























      0












      0








      0







      I'm trying to move the picture box on Host(a simple server) screen through data coming from client computer. It's a simple pong game which is multiplayer. Client sends data whenever the keys are pressed. It mainly sends the Y axis location of the pictureBox but I get error when I try to change the location of picture box.
      My client code which sends the Y axis location of the player1 pictureBox to server player



      private void keyisdown(object sender, KeyEventArgs e)
      {



              if (e.KeyCode == Keys.Up)
      {
      goup = true;


      String s = Convert.ToString(player.Location.Y);

      byte byteTime = Encoding.ASCII.GetBytes(s);

      ns.Write(byteTime, 0, byteTime.Length);
      }
      if (e.KeyCode == Keys.Down)
      {
      godown = true;

      String s = Convert.ToString(player.Location.Y);

      byte byteTime = Encoding.ASCII.GetBytes(s);

      ns.Write(byteTime, 0, byteTime.Length);
      }


      }


      My server program which is receiving the data



      public partial class Form1 : Form
      {



      delegate void SetTextCallback(string text);

      TcpListener listener;

      TcpClient client;

      NetworkStream ns;

      Thread t = null;


      public Form1()
      {
      InitializeComponent();

      listener = new TcpListener(IPAddress.Any, 4545);

      listener.Start();

      client = listener.AcceptTcpClient();

      ns = client.GetStream();

      t = new Thread(DoWork);

      t.Start();
      }

      private void button1_Click(object sender, EventArgs e)
      {
      String s = textBox2.Text;

      byte byteTime = Encoding.ASCII.GetBytes(s);

      ns.Write(byteTime, 0, byteTime.Length);
      }
      public void DoWork()

      {

      byte bytes = new byte[1024];

      while (true)

      {

      int bytesRead = ns.Read(bytes, 0, bytes.Length);
      string data;
      this.SetText(Encoding.ASCII.GetString(bytes, 0, bytesRead));

      // data = (Encoding.ASCII.GetString(bytes, 0, bytesRead));

      // int Y = Convert.ToInt32(data);

      // pictureBox1.Location = new Point(769, Y);
      }

      }

      private void SetText(string text)

      {

      // InvokeRequired required compares the thread ID of the

      // calling thread to the thread ID of the creating thread.

      // If these threads are different, it returns true.

      if (this.pictureBox1.InvokeRequired)

      {

      SetTextCallback d = new SetTextCallback(SetText);

      this.Invoke(d, new object { text });


      }

      else

      {

      this.pictureBox1.Location = new Point(769, Convert.ToInt32( text));

      }

      }

      private void textBox1_TextChanged(object sender, EventArgs e)
      {

      }

      private void Form1_Load(object sender, EventArgs e)
      {

      }


      }



      im receiving the Y location of client players picture box and it receives it but somehow it doesnt move correctly it just vanishes when it gets Y location the size of server n client program is same and the locations are same too so it should move like mirror



      This is the game I'm trying to make left is player1 and right is player2



      I also need help as to how can I update the ball position to both players and how can I properly implement multiplayer, right now I'm trying to slowly change the simple chat client code and get the data I need. but how can I get a constant player update every 20 or 50ms I'm very confused. Thanks in advance.










      share|improve this question















      I'm trying to move the picture box on Host(a simple server) screen through data coming from client computer. It's a simple pong game which is multiplayer. Client sends data whenever the keys are pressed. It mainly sends the Y axis location of the pictureBox but I get error when I try to change the location of picture box.
      My client code which sends the Y axis location of the player1 pictureBox to server player



      private void keyisdown(object sender, KeyEventArgs e)
      {



              if (e.KeyCode == Keys.Up)
      {
      goup = true;


      String s = Convert.ToString(player.Location.Y);

      byte byteTime = Encoding.ASCII.GetBytes(s);

      ns.Write(byteTime, 0, byteTime.Length);
      }
      if (e.KeyCode == Keys.Down)
      {
      godown = true;

      String s = Convert.ToString(player.Location.Y);

      byte byteTime = Encoding.ASCII.GetBytes(s);

      ns.Write(byteTime, 0, byteTime.Length);
      }


      }


      My server program which is receiving the data



      public partial class Form1 : Form
      {



      delegate void SetTextCallback(string text);

      TcpListener listener;

      TcpClient client;

      NetworkStream ns;

      Thread t = null;


      public Form1()
      {
      InitializeComponent();

      listener = new TcpListener(IPAddress.Any, 4545);

      listener.Start();

      client = listener.AcceptTcpClient();

      ns = client.GetStream();

      t = new Thread(DoWork);

      t.Start();
      }

      private void button1_Click(object sender, EventArgs e)
      {
      String s = textBox2.Text;

      byte byteTime = Encoding.ASCII.GetBytes(s);

      ns.Write(byteTime, 0, byteTime.Length);
      }
      public void DoWork()

      {

      byte bytes = new byte[1024];

      while (true)

      {

      int bytesRead = ns.Read(bytes, 0, bytes.Length);
      string data;
      this.SetText(Encoding.ASCII.GetString(bytes, 0, bytesRead));

      // data = (Encoding.ASCII.GetString(bytes, 0, bytesRead));

      // int Y = Convert.ToInt32(data);

      // pictureBox1.Location = new Point(769, Y);
      }

      }

      private void SetText(string text)

      {

      // InvokeRequired required compares the thread ID of the

      // calling thread to the thread ID of the creating thread.

      // If these threads are different, it returns true.

      if (this.pictureBox1.InvokeRequired)

      {

      SetTextCallback d = new SetTextCallback(SetText);

      this.Invoke(d, new object { text });


      }

      else

      {

      this.pictureBox1.Location = new Point(769, Convert.ToInt32( text));

      }

      }

      private void textBox1_TextChanged(object sender, EventArgs e)
      {

      }

      private void Form1_Load(object sender, EventArgs e)
      {

      }


      }



      im receiving the Y location of client players picture box and it receives it but somehow it doesnt move correctly it just vanishes when it gets Y location the size of server n client program is same and the locations are same too so it should move like mirror



      This is the game I'm trying to make left is player1 and right is player2



      I also need help as to how can I update the ball position to both players and how can I properly implement multiplayer, right now I'm trying to slowly change the simple chat client code and get the data I need. but how can I get a constant player update every 20 or 50ms I'm very confused. Thanks in advance.







      c# visual-studio sockets network-programming






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 15:43

























      asked Nov 19 '18 at 13:13









      Sam Munir

      13




      13





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375416%2ftrying-to-move-picturebox-from-remote-client-data-but-the-picturebox-vanishes-h%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375416%2ftrying-to-move-picturebox-from-remote-client-data-but-the-picturebox-vanishes-h%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith