Results 1 to 4 of 4

Thread: TCP Connection

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    TCP Connection

    I have the following in my current app...

    VB Code:
    1. Try
    2.             ' Set the TcpListener on port 13000.
    3.             Dim port As Int32 = 13000
    4.             Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
    5.  
    6.             Server = New TcpListener(localAddr, port)
    7.  
    8.             ' Start listening for client requests.
    9.             Server.Start()
    10.  
    11.             ' Buffer for reading data
    12.             Dim bytes(1024) As [Byte]
    13.             Dim data As [String] = Nothing
    14.  
    15.             ' Enter the listening loop.
    16.             While True
    17.                 Debug.Write("Waiting for a connection... ")
    18.  
    19.                 ' Perform a blocking call to accept requests.
    20.                 ' You could also user server.AcceptSocket() here.
    21.                 Dim client As TcpClient = Server.AcceptTcpClient()
    22.                 Debug.WriteLine("Connected!")
    23.  
    24.                 data = Nothing
    25.  
    26.                 ' Get a stream object for reading and writing
    27.                 Dim stream As NetworkStream = client.GetStream()
    28.  
    29.                 Dim i As Int32
    30.  
    31.                 ' Loop to receive all the data sent by the client.
    32.                 i = stream.Read(bytes, 0, bytes.Length)
    33.                 While (i <> 0)
    34.                     ' Translate data bytes to a ASCII string.
    35.                     data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    36.                     Debug.WriteLine([String].Format("Received: {0}", data))
    37.  
    38.                     ' Process the data sent by the client.
    39.                     data = CheckValidity(data.ToUpper)
    40.  
    41.                     Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
    42.  
    43.                     ' Send back a response.
    44.                     stream.Write(msg, 0, msg.Length)
    45.                     Console.WriteLine([String].Format("Sent: {0}", data))
    46.  
    47.                     i = stream.Read(bytes, 0, bytes.Length)
    48.  
    49.                 End While
    50.  
    51.                 ' Shutdown and end connection
    52.                 client.Close()
    53.             End While

    This works ok the first time you connect and stuff, but it does not cause the connection to start listening again. It is in a sub that is used to create a thread to listen for incoming connection requests.

    The ideal thing to happen would be for a request to come in and be pushed off to another port or something like that so that the main port (the one that is hardcoded into my client) will remain open for more requests...

    Any Ideas?

    SQ1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    New Member
    Join Date
    Mar 2004
    Posts
    6
    instead of:
    Dim client As TcpClient = Server.AcceptTcpClient()
    try something to the effect of:
    redim preserve client(client.length + 1) as tcpclient = server.accepttcpclient()

    then you will need to create a loop simliar to the following in a seperate sub that checks for new data:
    for I as integer = 0 to client.length - 1
    if client(I).datawaiting? then (cannot remembeer the exact syntax)
    'recieve the data into a string and raise an event with the client index and the data, ie:
    raiseevent newdata(I, receiveddata)
    next

    basically, you want an array, or dictionary, or hashtable or whatever to store all the tcpclients from each new connection

    I used to have the code that I wrote awhile back, but lost it in a hard drive crash, I just remember how I did it. I actually used a hashtable and for each new connection I created a GUID, and added the client to the hashtable using the GUID as the key and the tcpclient as the value.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    I might research the hashtable thing... but i do have one question...

    doesn't the table, array or whatever get like really huge?

    sq1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    squirrelly1, it sounds like you're trying to create something that I have. That is, I struggled through it and finally got it to work. Not sure if it's bug free or not, but it's in production at several clients and they don't call me to say it's not working Plus, every time I dial in to check it out, it's always up and running.

    Anyway, what I have is a Windows service. This service accepts multiple connects from x number of clients. Also, it makes one connection to a server - so I have one thread that handles the clients, and another that handles the connection to the server.

    When a client connects, I add him to my hashtable, which contains his socket information. Now - and this part I'm not really sure about, but it seems to be the case - if you receive 0 bytes from your client, it means he shut down - so when that happens, I remove him from the hashtable.

    Mike

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