socket connection: several client connect to a single socket server. And i cant be sure how many client, so i hope accept method dont block execution. I think the timeout is the best solution machanism.
a connection will create a thread to handle the message come from the same socket port(every client connection will send message by the same port). The connection will continue to receive data (dont have to send data) until a special word occur.
if u have a relative code example, please post it.
i am not american and my english is not good...>_< . hope that u can understand what i mean.^_^
I personally tend to go the async route and stop the listener at whatever point I need to. The synchronous Accept method is traditionally used to listen on a worker thread that specifically handles that job. Termination of the listener is then simply a matter of aborting the blocked thread.
If either of these options is not appropriate, can you please give a little more information on why you need a timeout and perhaps we can look to an alternative approach that meets your needs.
Maybe something along the following lines is what you are looking for .....
Lets create a new class that will abstract all the logic away for us ...... we need a module level variable for the listening socket.
Private ReadOnly mListener As Net.Sockets.Socket
Now, in our ctor we need to set up the listening side of our server.
mListener =
New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
mListener.Bind(
New Net.IPEndPoint(Net.Dns.GetHostEntry(localHost).AddressList(0), YourPortNumberGoesHere))
mListener.Listen(10)
mListener.BeginAccept(
New AsyncCallback(AddressOf Me.OnConnectRequest), mListener)
We now have a listening socket and we have supplied a delegate that will be invoked when a connection request is made. We need to code this method now.
Private Sub OnConnectRequest(ByVal async As IAsyncResult)
Dim socket As Net.Sockets.Socket = TryCast(async.AsyncState, Net.Sockets.Socket)
If socket Is Nothing Then Return This will be the same reference as mListener but I prefer to work with this variable is case the application changes
Dim newSocket As Net.Sockets.Socket = socket.EndAccept(async) : socket.Accept()
If newSocket Is Nothing Then Return
This is where you will need to add your custom logic to set up the reading of the socket held in newSocket. Use its BeginAccept method to pass in a delegate that can be called when there is data to be read. You will also need to pass in a buffer that it can write to, so it is probably best to use a wrapper class for the callback and buffer.
socket.BeginAccept(
New AsyncCallback(AddressOf Me.OnConnectRequest), socket) Start Listening for more incoming connection requests
End Sub
Probably the last thing to consider is how to convert the read bytes back into your message string ...
Text.Encoding.ASCII.GetString(
mBuffer)
It's by no means a complete working example, but hopefully this will set you off along the right path and you can tweak it to meet your actual requirements.
Well I guess that depends on what exactly you are trying to do. I believe 'select' can be used to determine socket 'state' and appropiate action can then be taken (not sure if you are asking because it has a timeout argument, but this only controls the amount of time it takes to determine state)
Have you considered asynchronously processing the sockets It's quite simple ... call BeginAccept passing in a callback delegate and that method will be invoked when the socket has information for you. You can use EndAccept to terminate the socket listening when you need to/recover the data.
Give me a shout if you need a code example or some such thing.
about socket's accept method in vb 2003?
Jo_
http://msdn2.microsoft.com/en-us/library/system.net.sockets.socket.select.aspx
method 'select' can work
nojetlag
AjayB
Hi,
the requirement what i need is:
socket connection: several client connect to a single socket server. And i cant be sure how many client, so i hope accept method dont block execution. I think the timeout is the best solution machanism.
a connection will create a thread to handle the message come from the same socket port(every client connection will send message by the same port). The connection will continue to receive data (dont have to send data) until a special word occur.
if u have a relative code example, please post it.
i am not american and my english is not good...>_< . hope that u can understand what i mean.^_^
thx very much.....
Biceps
Hi
I don't believe you can.
I personally tend to go the async route and stop the listener at whatever point I need to. The synchronous Accept method is traditionally used to listen on a worker thread that specifically handles that job. Termination of the listener is then simply a matter of aborting the blocked thread.
If either of these options is not appropriate, can you please give a little more information on why you need a timeout and perhaps we can look to an alternative approach that meets your needs.
Richard
jflowers
Hi
I'm not american either and your english is fine.
Maybe something along the following lines is what you are looking for .....
Lets create a new class that will abstract all the logic away for us ...... we need a module level variable for the listening socket.
Private ReadOnly mListener As Net.Sockets.Socket
Now, in our ctor we need to set up the listening side of our server.
mListener =
New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)mListener.Bind(
New Net.IPEndPoint(Net.Dns.GetHostEntry(localHost).AddressList(0), YourPortNumberGoesHere))mListener.Listen(10)
mListener.BeginAccept(
New AsyncCallback(AddressOf Me.OnConnectRequest), mListener)We now have a listening socket and we have supplied a delegate that will be invoked when a connection request is made. We need to code this method now.
Private Sub OnConnectRequest(ByVal async As IAsyncResult) Dim socket As Net.Sockets.Socket = TryCast(async.AsyncState, Net.Sockets.Socket)If socket Is Nothing Then Return This will be the same reference as mListener but I prefer to work with this variable is case the application changes
Dim newSocket As Net.Sockets.Socket = socket.EndAccept(async) : socket.Accept() If newSocket Is Nothing Then ReturnThis is where you will need to add your custom logic to set up the reading of the socket held in newSocket. Use its BeginAccept method to pass in a delegate that can be called when there is data to be read. You will also need to pass in a buffer that it can write to, so it is probably best to use a wrapper class for the callback and buffer.
socket.BeginAccept(
New AsyncCallback(AddressOf Me.OnConnectRequest), socket) Start Listening for more incoming connection requests End SubProbably the last thing to consider is how to convert the read bytes back into your message string ...
Text.Encoding.ASCII.GetString(
mBuffer)It's by no means a complete working example, but hopefully this will set you off along the right path and you can tweak it to meet your actual requirements.
Good luck.
Richard
laserbeam
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1155607&SiteID=1
maybe it is easy for u, please......
Mr_White
Hi
Well I guess that depends on what exactly you are trying to do. I believe 'select' can be used to determine socket 'state' and appropiate action can then be taken (not sure if you are asking because it has a timeout argument, but this only controls the amount of time it takes to determine state)
Have you considered asynchronously processing the sockets It's quite simple ... call BeginAccept passing in a callback delegate and that method will be invoked when the socket has information for you. You can use EndAccept to terminate the socket listening when you need to/recover the data.
Give me a shout if you need a code example or some such thing.
Richard