Server Listening

nate

Member
Joined
Dec 2, 2006
Messages
22
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 dont understand how to convert the code to VB.
 
You are looking to get a translation? Its not that difficult.
[VB]
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 dont 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
[/VB]

There is a sticky thread to help with C#->VB translation.
 
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 dont 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.
 
Where I have next to no experience with winsock, I really wouldnt know a stupid question from a good one. Looking back at my post, I hope I didnt come off as offensive, and sorry I cant help more.
 
marble_eater said:
Where I have next to no experience with winsock, I really wouldnt know a stupid question from a good one. Looking back at my post, I hope I didnt come off as offensive, and sorry I cant help more.

You werent 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.
 
Sockets in .Net

nate said:
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 dont need to add the control to the form, is this correct?

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 isnt 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.

Im sure there are examples in the forum if you search for them, otherwise I can write one quickly.

Good luck :cool:
 

Similar threads

P
Replies
0
Views
174
Policy standard local admin account with Active Di
P
Back
Top