PC Review


Reply
Thread Tools Rate Thread

client/server

 
 
Henk
Guest
Posts: n/a
 
      24th Feb 2004
Hi there,

How can I modify this code
http://www.eggheadcafe.com/articles/20020323.asp to make it possible
that multiple clients can send data to the server (and get data from
the server)?

Or does somebody know another example? :roll:


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
 
Reply With Quote
 
 
 
 
Anand Balasubramanian
Guest
Posts: n/a
 
      25th Feb 2004
Hi,
The way you would handle multple clients is by creating multiple threads
on the server side. Basically you would run your server in a infinite while
loop and as soon as it accepts a connection, it will crearte a new thread
and pass the Tcpclient object as a paramenter to this thread which will
then communicate with the client, while the main thread in the server will
continue to listen incoming connections.

While (true)
{
Wait for Connection
Create thread to handle the client
}

This is generally how you would handle multple clients. I shall try and see
if i can put a sample together.

Thanks
Anand



Anand Balasubramanian
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks

 
Reply With Quote
 
Anand Balasubramanian
Guest
Posts: n/a
 
      25th Feb 2004
Hi,
Here is how the server should be

Main Module
===========
Imports System.Net.Sockets
Imports System.Text
Class TCPSrv
Shared Sub Main()
' Must listen on correct port- must be same as port client wants to
connect on.
Const portNumber As Integer = 8000
Dim tcpListener As New TcpListener(portNumber)
Dim ct As Integer

tcpListener.Start()
Console.WriteLine("Waiting for connection...")
Try
'Accept the pending client connection and return 'a
TcpClient initialized for communication.

ct = 0
While (True)

Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection accepted.")

Dim tmp As New WorkerThread(tcpClient, ct)
ct += 1

Dim thrd As New System.Threading.Thread(AddressOf
tmp.workerMethod)
thrd.Start()

End While

Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()
End Try
End Sub
End Class

The WorkerThread Class
======================
Imports System.Net.Sockets
Imports System.Text


Public Class WorkerThread
Private tcpClient As tcpClient
Private clientnumber As Integer

Sub New(ByRef tcpCli As tcpClient, ByVal ct As Integer)
tcpClient = tcpCli
clientnumber = ct
End Sub

Sub workerMethod()
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("Client sent: " + clientdata))
Dim responseString As String = "Connected to server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(("Message Sent /> : " + responseString + "Client
number" + clientnumber.ToString))
'Any communication with the remote client using the TcpClient can
go here.
'Close TcpListener and TcpClient.
tcpClient.Close()
Console.WriteLine("exit")
End Sub
End Class

I hope this helps.
Thanks



Anand Balasubramanian
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with client computer in the client/server network windows server 2003 gogi100 Windows XP Networking 1 1st Sep 2007 05:23 PM
Browse client Directory to a specific folder and ftp the file from server to client bindu_conacle@yahoo.com Microsoft ASP .NET 1 26th Feb 2006 11:34 AM
Browse client Directory to a specific folder and ftp the file from server to client bindu_conacle@yahoo.com Microsoft VB .NET 0 26th Feb 2006 10:24 AM
Socket Server to Socket Client..Client function not working when called from Server... trint Microsoft C# .NET 2 13th Apr 2005 09:43 PM
Explain to a client the workings of Terminal Server & Client Connections (Remote Desktop) Barry McConomy Microsoft Windows 2000 Terminal Server Clients 4 13th Sep 2004 12:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:26 PM.