473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

client/server

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: :D :D :D :D
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #1
2 1399
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

Nov 20 '05 #2
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

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Matt | last post by:
I think this is the basic concept in ASP server-side development. My boss told me web application is NOT client-server application. I argued with him because browser is the client, and the server...
2
by: Microsoft | last post by:
I'm about to start converting my application from a old-style monolith exe (with flat files and limited database support for sharing some of the data) to a layered .NET SQL server version. I have...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
18
by: cjl | last post by:
Hey all: I know that it is silly in the age of Google to 'lose' something on the internet, but I recently checked out a project that had implemented a database with a subset of SQL in pure...
6
by: Ken Allen | last post by:
I am relatively new to .Net and C#, but I hav ebeen programing in other languages and done some COM work for a number of years. I am attempting to understand how to map an older program...
0
by: Harley | last post by:
Hello, I am just learning the tcp/ip functions etc under vb.net so please look over me if this is obviouse. I have been all over looking into any functions that I didn't totaly understand and...
4
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
10
by: Ben | last post by:
Hi, I made an application in classic asp (reservation of books and video stuffs for students) and want to migrate to asp.net. The user has to chose a date, then pushung on a submit button. The...
2
by: Frank Swarbrick | last post by:
I am trying to understand "client authentication" works. My environment is DB2/UDB LUW 8.2 on zSeries SLES9 as the database server and DB2 for VSE 7.4 as the client. We currently have DB2/LUW set...
2
by: nsaffary | last post by:
hi I hava a client/server program that run correctly when i run it in one computer(local) but when I run client on a one computer and run server run on another, connection does not stablish.(I set...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.