|
-
Mar 13th, 2004, 07:09 PM
#1
Thread Starter
Frenzied Member
TCP Connection
I have the following in my current app...
VB Code:
Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
Server = New TcpListener(localAddr, port)
' Start listening for client requests.
Server.Start()
' Buffer for reading data
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
' Enter the listening loop.
While True
Debug.Write("Waiting for a connection... ")
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = Server.AcceptTcpClient()
Debug.WriteLine("Connected!")
data = Nothing
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Debug.WriteLine([String].Format("Received: {0}", data))
' Process the data sent by the client.
data = CheckValidity(data.ToUpper)
Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
Console.WriteLine([String].Format("Sent: {0}", data))
i = stream.Read(bytes, 0, bytes.Length)
End While
' Shutdown and end connection
client.Close()
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?
-
Mar 14th, 2004, 12:18 AM
#2
New Member
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.
-
Mar 14th, 2004, 11:12 AM
#3
Thread Starter
Frenzied Member
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?
-
Mar 14th, 2004, 02:10 PM
#4
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|