Results 1 to 9 of 9

Thread: Connect over net

  1. #1

    Thread Starter
    Lively Member DanielS1324's Avatar
    Join Date
    Apr 2005
    Posts
    79

    Connect over net

    I wrote a program to act as a chess clock. I would like to give a copy to somebody else. If your familiar with a chess clock, you know that when you make a move, you stop your time... and stopping your time starts your opponents time.

    I would like for the program to connect to his computer so that when he stops his time, the program on my machine will act accordingly. And when I stop my time, his will start. We can see each others time.

    I'm ok with programming, but no idea when it comes to connecting over the net.

    If I could learn to send maybe 1 character at at time over the internet, I could go from there.
    Last edited by DanielS1324; Feb 22nd, 2006 at 04:10 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Connect over net

    You should take a look at the Sockets example in the 2003 101 samples from Microsoft. It uses a client/server chat application as an example of network programming but the principles would be exactly the same for what you want to do. If you're using VS 2005, the 2005 101 Samples may have an equivalent example but I haven't checked.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member DanielS1324's Avatar
    Join Date
    Apr 2005
    Posts
    79

    Re: Connect over net

    Thanks for the reply. Apparently the 101 samples for VS 2005 dont have the chat example.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Connect over net

    Open the 2003 version in 2005 and it will be upgraded. There may be a few issues that need addressing but chances are it will work without need for any changes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member DanielS1324's Avatar
    Join Date
    Apr 2005
    Posts
    79

    Re: Connect over net

    Thanks! I got the connection part done. But now there's another problem... I have the connection stuff in a seperate thread and when I try to set a label's text from that thread, it throws an exception.

    Something about accessing the control from a thread other than the one it was created on.

    I tried the examples for accessing the label from seperate threads, but none of them work.

    So... How can I update the text of a label from a seperate thread?

    Thanks!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Connect over net

    To access members of a control from a different thread you need to use a delegate to marshal the call to the UI thread. Basically you create a delegate, tell it what method you want it to execute, then send it off to the UI thread to execute that method where it can safely access control members. Search the forum for "delegate" with my user name and you'll find a couple of examples, by me and others.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Connect over net

    Actually, I realised that all the examples I've given are in VB so here's a C# example. You would need to define a method to access the members of the Label, then declare a matching delegate:
    Code:
    // This delegate will be used to invoke the SetLabelText method so it must have the same signature.
    private delegate void SetLabelTextDelegate(string text);
    
    // This method sets the text of the label.
    private void SetLabelText(string text)
    {
        this.label1.Text = text;
    }
    When you wanted to set the Label's Text you would use code like this to test whether an invocation was required and, if so, perform one:
    Code:
    if (this.label1.InvokeRequired)
    {
        // We are not in the UI thread so invoke a method on the UI thread to set the label text.
        this.label1.Invoke(new SetLabelTextDelegate(SetLabelText), new string[] { "Some text" });
    }
    else
    {
        // We are in the UI thread so it is safe to access the label directly.
        this.label1.Text = "Some text";
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member DanielS1324's Avatar
    Join Date
    Apr 2005
    Posts
    79

    Re: Connect over net

    Thanks a million! That should be excatly what I need.

    The "if (invokeRequired...)" where does that code go?
    Last edited by DanielS1324; Feb 23rd, 2006 at 10:11 AM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Connect over net

    Quote Originally Posted by DanielS1324
    The "if (invokeRequired...)" where does that code go?
    Wherever you were previously accessing the Label directly. The code you are currently using would now go in the 'else' block and the cross-thread equivalent in the 'if' block.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width