nate Posted December 4, 2006 Posted December 4, 2006 I am trying to make my server listen continually. My server currently listens for a client and when the client connects the exchenge info. My problem is I want to keep serving other clients. Do I need to use multiple sck controls or does this handle it in .net? I remmember doing this in vb6 using 2 controls one to listen and one to accept the request and transfer info. Please help I know this has to be easier now. Thanks in advance. I found this post and replied. I just don't understand how to convert the code to VB. Quote Ignorance begins with I.
Leaders snarfblam Posted December 4, 2006 Leaders Posted December 4, 2006 You are looking to get a translation? It's not that difficult. Private Sub StartServer() Dim server As New TcpListener(IPAddress.Any, 1234) server.Start() Dim socket As Socket Dim chatSession As ChatSession While True ' blocks until a connection is established socket = server.AcceptSocket() ' when this fires the socket will be connected and the connection ' will actually be setup on a port other than 1234. this allows ' us to keep servicing future requests on port 1234. ' for long running processes, the key is to somehow pass the socket ' off to another thread. if you don't do this the server will ' not be able to establish new connections while the process is still ' running. chatSession = New ChatSession(socket) chatSessions.Add(chatSession) End While End Sub There is a sticky thread to help with C#->VB translation. Quote [sIGPIC]e[/sIGPIC]
nate Posted December 5, 2006 Author Posted December 5, 2006 marble_eater, Duh, the translation was simple sorry for the stupid question. I guess my question is more in the understanding here normally I add a winsock control to my form set it to listen then use it to transmit data from one program to the other. This code looks like I don't need to add the control to the form, is this correct? Ive never used TcpListener and where would the data arrival come in here? Sorry if these are stupid questions, just a little confused. Thanks again. Quote Ignorance begins with I.
Leaders snarfblam Posted December 5, 2006 Leaders Posted December 5, 2006 Where I have next to no experience with winsock, I really wouldn't know a stupid question from a good one. Looking back at my post, I hope I didn't come off as offensive, and sorry I can't help more. Quote [sIGPIC]e[/sIGPIC]
nate Posted December 5, 2006 Author Posted December 5, 2006 Where I have next to no experience with winsock' date=' I really wouldn't know a stupid question from a good one. Looking back at my post, I hope I didn't come off as offensive, and sorry I can't help more.[/quote'] You weren't rude I did have an actual stupid question. Thanks for the help I think I can figure it out I just need to figure out how to pass it on. Thanks again you got me much further. Quote Ignorance begins with I.
MrPaul Posted December 9, 2006 Posted December 9, 2006 Sockets in .Net normally I add a winsock control to my form set it to listen then use it to transmit data from one program to the other. This code looks like I don't need to add the control to the form' date=' is this correct?[/quote'] A socket is not generally something that is part of, or supports, a user interface. It is therefore counterintuitive to have a UI socket control, and in .Net there isn't one. The System.Net and System.Net.Sockets namespaces contain classes for implementing sockets, such as the TcpListener mentioned above. Socket event handling differs much from VB6 and below. The above code starts a listening socket and then serves off each incoming request to a new Socket object. There is also an AcceptTcpClient method which returns the new connection as a TcpClient. The TcpClient class is a simplified wrapper for a Socket, and exposes a NetworkStream which can be used to send and receive via the synchronous (blocking) Read and Write methods or the asynchronous (non-blocking) BeginRead/EndRead and BeginWrite/EndWrite methods. The Socket class also contains these methods if you prefer to work with Sockets (as I do). When a Socket or TcpClient is returned from AcceptSocket or AcceptTcpClient you probably want to begin receiving data asycnhronously. To do this you need to call BeginRead, specifying the callback method (delegate) to be invoked when the receive operation has completed - ie when there is data to process. In the delegate method you invoke EndReceive and process the data accordingly, before calling BeginReceive again to continue receiving. I'm sure there are examples in the forum if you search for them, otherwise I can write one quickly. Good luck :cool: Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.