Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. windows forms TCP connection problem

windows forms TCP connection problem

Scheduled Pinned Locked Moved C#
helptutorialcsharpwinformsgame-dev
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bar3000
    wrote on last edited by
    #1

    Hi, I built a checkers game in C# in which players sitting next to each other can play. I am now trying to upgrade it, to become internet based. I want to generate a simple P2P connection. I wrote this code to see if a connection is established but for some reason it freezes when pressing the connection button. Please help me sort this out. Or if you think this code is unsuitable, than please help me out with this.. here is the code

        string clientOrServer;
        Stream \_stream;
        string read;
            Stream getTcpStream()
            {
            TcpClient client;
            IPEndPoint meetingPoint = new IPEndPoint(IPAddress.Parse("79.178.57.135"), 8080);
            if(clientOrServer.ToLower().StartsWith("c"))
                {
                    client = new TcpClient();
                    client.Connect(meetingPoint);
                }
                else
                {
                    TcpListener listener = new TcpListener(meetingPoint.Port);
                    listener.Start();
                    client=listener.AcceptTcpClient();
                    listener.Stop();
                }
                return client.GetStream();
            }
      
        void clientServerclick(System.Windows.Forms.PictureBox picturebox)
        {
           StreamWriter writer = new StreamWriter(\_stream);
           writer.AutoFlush = true;
          if (picturebox == pictureBox89)
          {
              writer.WriteLine("{0}", pictureBox89);
          }
          if (picturebox == pictureBox51)
          {
              writer.WriteLine("{0}", pictureBox51);
          }
        }
        void readclick()
        {
            StreamReader reader = new StreamReader(\_stream);
            string a = reader.ReadLine();
            Console.WriteLine(a);
        }
    

    this is the code for client generating connection
    clientOrServer = "c";
    _stream = getTcpStream();
    Console.WriteLine("Connected");
    server
    clientOrServer = "s";
    _stream = getTcpStream();
    Console.WriteLine("Connected");
    this runs when a click on a soldier occurs
    clientServerclick(pictureBox);

    If this code is off, please guide me on how to accomplish this sort of thing.THANKS :-D :)

    C 1 Reply Last reply
    0
    • B bar3000

      Hi, I built a checkers game in C# in which players sitting next to each other can play. I am now trying to upgrade it, to become internet based. I want to generate a simple P2P connection. I wrote this code to see if a connection is established but for some reason it freezes when pressing the connection button. Please help me sort this out. Or if you think this code is unsuitable, than please help me out with this.. here is the code

          string clientOrServer;
          Stream \_stream;
          string read;
              Stream getTcpStream()
              {
              TcpClient client;
              IPEndPoint meetingPoint = new IPEndPoint(IPAddress.Parse("79.178.57.135"), 8080);
              if(clientOrServer.ToLower().StartsWith("c"))
                  {
                      client = new TcpClient();
                      client.Connect(meetingPoint);
                  }
                  else
                  {
                      TcpListener listener = new TcpListener(meetingPoint.Port);
                      listener.Start();
                      client=listener.AcceptTcpClient();
                      listener.Stop();
                  }
                  return client.GetStream();
              }
        
          void clientServerclick(System.Windows.Forms.PictureBox picturebox)
          {
             StreamWriter writer = new StreamWriter(\_stream);
             writer.AutoFlush = true;
            if (picturebox == pictureBox89)
            {
                writer.WriteLine("{0}", pictureBox89);
            }
            if (picturebox == pictureBox51)
            {
                writer.WriteLine("{0}", pictureBox51);
            }
          }
          void readclick()
          {
              StreamReader reader = new StreamReader(\_stream);
              string a = reader.ReadLine();
              Console.WriteLine(a);
          }
      

      this is the code for client generating connection
      clientOrServer = "c";
      _stream = getTcpStream();
      Console.WriteLine("Connected");
      server
      clientOrServer = "s";
      _stream = getTcpStream();
      Console.WriteLine("Connected");
      this runs when a click on a soldier occurs
      clientServerclick(pictureBox);

      If this code is off, please guide me on how to accomplish this sort of thing.THANKS :-D :)

      C Offline
      C Offline
      Calin Tatar
      wrote on last edited by
      #2

      try to create a worker Thread to call readclick() Calin

      B 1 Reply Last reply
      0
      • C Calin Tatar

        try to create a worker Thread to call readclick() Calin

        B Offline
        B Offline
        bar3000
        wrote on last edited by
        #3

        do I need a reader thread too then?

        C 1 Reply Last reply
        0
        • B bar3000

          do I need a reader thread too then?

          C Offline
          C Offline
          Calin Tatar
          wrote on last edited by
          #4

          or add the thread only for getTcpStream(); Calin

          B 1 Reply Last reply
          0
          • C Calin Tatar

            or add the thread only for getTcpStream(); Calin

            B Offline
            B Offline
            bar3000
            wrote on last edited by
            #5

            can u give the line for this please.. because when i try Thread tcp = new Thread(new ThreadStart(getTcpStream)); it gives me this error Error 1 'System.IO.Stream WindowsFormsApplication1.Form1.TCPChatApplication.getTcpStream()' has the wrong return type

            R 1 Reply Last reply
            0
            • B bar3000

              can u give the line for this please.. because when i try Thread tcp = new Thread(new ThreadStart(getTcpStream)); it gives me this error Error 1 'System.IO.Stream WindowsFormsApplication1.Form1.TCPChatApplication.getTcpStream()' has the wrong return type

              R Offline
              R Offline
              riced
              wrote on last edited by
              #6

              ThreadStart() takes the name of a method that has no return type (i.e. is void) and takes no parameters. Your getTcpStream method returns a stream.

              Regards David R

              B 1 Reply Last reply
              0
              • R riced

                ThreadStart() takes the name of a method that has no return type (i.e. is void) and takes no parameters. Your getTcpStream method returns a stream.

                Regards David R

                B Offline
                B Offline
                bar3000
                wrote on last edited by
                #7

                Thank you very much! finaly worked it out...

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups