Results 1 to 6 of 6

Thread: Tcp networking

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Tcp networking

    I've just spent the whole day reading, writing code around TCP connects to allow users to communicate via a server application.

    So far i'm further behind than when I woke up today.

    What I want to be able to do is pass game data and comments via a central server.

    Is the best option moving forward with technology using TCP?

    Where do I start? I'm very very lost with it all.

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

    Re: Tcp networking

    Follow the CodeBank link in my signature and check out my Asynchronous TCP thread for an example of a server that can communicate asynchronously with multiple clients over TCP. It's designed to send text data but all data transmitted is sent as binary data, i.e. a Byte array, so you can easily adapt it to send whatever you want. You could also use synchronous methods rather than asynchronous at one or both ends. Synchronous methods are easier but they will freeze up a UI so for GUI apps you should go asynchronous.

    As a basic explanation, at the server end you use a TcpListener to accept incoming connections and create a TcpClient for each one. You then receive and send data using the NetworkStream wrapped by that TcpClient. At the client end you create a TcpClient and connect it to the server. You then send and receive using its NetworkStream. If you need finer control than the TcpClient provides then you can use the Socket class, which works at a lower level.
    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
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Tcp networking

    http://msdn.microsoft.com/en-us/libr...plistener.aspx
    http://msdn.microsoft.com/en-us/libr...tcpclient.aspx

    Thanks for the reply, I've used this code and it simply sents a message string to the server (listener) and returns the same string in caps, questions I have

    1. How can I send a message to another user?
    2. How can I send a one way message to the server?

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

    Re: Tcp networking

    There are only two things you can do:

    1. Send a message from a client to the server.
    2. Send a message from the server to a client.

    That's it, that's all. You simply have to do those two things in whatever combination(s) suit your app.
    How can I send a message to another user?
    You would send a message to the server and it would then send that message on to the other client. Your original message would have to have a header indicating which client the message is bound for. The server would then strip off that header, read it and send the payload on accordingly.
    How can I send a one way message to the server?
    What's a "one way message"? A message is a message. They all go one way. To send a message to the server you send a message to the server, i.e. write data to the stream associated with the TcpClient at the client end. You then read that data from the stream associated with the TcpClient at the server end.
    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
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Tcp networking

    Number 2 was a dumb question. Agreed now.

    Question 1, how does I add a header which is then forwarded to to another user? I assume the header is the IP address?


    Also can I added a text box that allows input then on a button click it sends that text via the network stream?

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

    Re: Tcp networking

    Quote Originally Posted by AirlineSim View Post
    Question 1, how does I add a header which is then forwarded to to another user? I assume the header is the IP address?
    That's completely up to you. It's your application so it works how you want it to. The .NET framework is dumb in that it simply transports the data between client and server with no reagrd for what the data actually represents. It's completely up to you to give the data meaning by how you design your app.
    Quote Originally Posted by AirlineSim View Post
    Also can I added a text box that allows input then on a button click it sends that text via the network stream?
    Again, that's completely up to you. TextBoxes have got absolutely to do with TCP communication. As I said, the .NET Framework will transport the data without caring what the data represents. That also means that it doesn't care where the data came from in the first place. A Byte array is a Byte array. If you want to get a String from a TextBox and convert that to a Byte array to transmit then that is completely up to you. Again, it's your app so it works how you want it to.

    The fact that you're asking that last question pretty much guarantees that you haven't looked at my CodeBank thread yet. People post in the CodeBank so everyone can go there to get information instead of the same answers having to be given over and over in thread after thread. I spent quite a bit of time on that code so I'm not going to answer any more questions here that are already answered there. As I always say, do what you can for yourself first, which includes using information that people have already directed you to. Download the project, run it, read the code and watch it in action. If there's still something you don't understand after that, then I'll be happy to help.
    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