tcplistener and threads

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joedeene
    Contributor
    • Jul 2008
    • 579

    tcplistener and threads

    hey, kinda confused, like when i use a thread and set its address of property to a sub that has a tcplistener starting in it or something with tcplistener, i can access other windows controls, like by editing a textbox's string value with no problem, without getting a cross-thread operation, but for my client app, i get cross-thread exception, and i really dont know how to handle those properly.

    so i got two questions:

    1) how to access a textbox and change its text value for my client app without getting a cross thread exception?

    2) can multiple sockets and/or listeners be set to a single port on one computer, say for a chat application, like if you have 5 clients(socket or tcpclient) connecting to the one tcplistener ?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    1) MSDN has a nice tutorial that is linked from the error message you get when you access gui components from other threads (just look up the error message and it will take you nice explanations of ways around it)

    2) The tcplistener only listens on the one port, when it receives a connection request, and that request is accepted, a new connection is established with a different port number (either as a Socket or TcpClient, depending on how you accepted the connection). So your TcpListener can continue to accept more and more connections, it will just pipe them off to you as Sockets(TcpClie nts)

    Comment

    • joedeene
      Contributor
      • Jul 2008
      • 579

      #3
      well, i've looked all over MSDN and they speak so expertly, and its hard for me to break it down, and i've almost got a thread to work for the client, but really i took all that out and i dont need it, because it doesnt really use all or alot of my cpu or anything, but it times out and i dont want it to, isnt there a way i can use

      something like -


      Dim socket As New Socket(AddressF amily.InterNetw ork, SocketType.Stre am, ProtocolType.Tc p)
      Dim connected As Boolean = False




      Private Sub btnConnect_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnConnect.Clic k


      Do Until connected

      socket.Connect( "***.***.*.***" , *****)
      connected = True


      Loop
      End Sub



      but it says the machine actively refused it, because i havent start the server, just because i want to see if it loops, but the error pops up. i want to skip the error and it keep looping, but i dont want to use 'On Error Resume Next' because it would make the connected value true, wouldnt it?
      Last edited by joedeene; Jul 21 '08, 03:53 PM. Reason: personal/privacy endangerment

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        What you could have is roughly something like this:

        A TcpListener (that has been started), that will check for peding connections
        If one is found, Accept (as either a tcpClient or a socket) and then pipe off a new thread using that NEW socket.
        That new thread will be in charge of that socket connection to the connecting client. When the client disconnects, kill off the thread.

        Repeat in a loop-ish manor. You could have any number of active connections.

        Comment

        • joedeene
          Contributor
          • Jul 2008
          • 579

          #5
          Originally posted by Plater
          What you could have is roughly something like this:

          A TcpListener (that has been started), that will check for peding connections
          If one is found, Accept (as either a tcpClient or a socket) and then pipe off a new thread using that NEW socket.
          That new thread will be in charge of that socket connection to the connecting client. When the client disconnects, kill off the thread.

          Repeat in a loop-ish manor. You could have any number of active connections.
          wow, umm ya, that sounds kinda simple, but do u have any kind of code sample i could use to start the new thread for each connection?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Msdn has some decent enough examples.
            I've just decided that my own implementation was not good and am reworking it.
            If I come up with anything good, I'll let you know.

            Comment

            • joedeene
              Contributor
              • Jul 2008
              • 579

              #7
              alrighty, thanks bud. i just want to get this running but it seems so complicated. i found this link on a forum http://www.codeproject.com/KB/IP/dotnettcp.aspx
              but its written in C# and im not experienced with that language, its hard to convert. but i'm gonna keep look at it, but ya if you come up with anything, plz let me kno.

              Comment

              Working...