Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2008

Tip: Looking for answers? Try searching our database.

Multiple socket clients

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Markgoldin - 30 Jul 2008 14:39 GMT
Here is code I am using to create a socket listener:
public void Run()

{

go = true;

Random rand = new Random();

// Buffer for reading data

Byte[] bytes = new Byte[1024];

String data = null;

while (go)

{

TcpClient tcpc = FromAnotherClass.tcpl.AcceptTcpClient(); //accept
connection

data = null;

// Get a stream object for reading and writing

NetworkStream stream = tcpc.GetStream();

// Loop to receive all the data sent by the client.

int i;

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)

{

// Translate data bytes to a ASCII string.

data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

// Process the data sent by the client.

if (data != "")

{

System.Console.WriteLine(data);

}

}

Thread.Sleep(1000 + rand.Next(2000));

}

}

My question is can this code handle connections from multiple clients?

Or  should I have a separate listener for each client?

Thanks for help.
Ignacio Machin ( .NET/ C# MVP ) - 30 Jul 2008 21:04 GMT
> My question is can this code handle connections from multiple clients?
>
> Or  should I have a separate listener for each client?

You need to have one thread listening for connection, and when a
connection is received a new thread is spawned to handle it.

I have posted similar code in the past, it's very easily done with a
Sync Queue.

Look in the archive or post back if you do not find my old posts
Ignacio Machin ( .NET/ C# MVP ) - 30 Jul 2008 21:07 GMT
found the post:

while(true)
{
Socket s = listener1.AcceptSocket();
syncedQueue.Enqueue( s );
new Thread( new ThreadStart( workerMethod) ).Start();
}

workerMethod()
{
Socket s = syncedQueue.Dequeue();
}

You have to use a synced queue though:

syncedqueue = Queue.Synchonize( new Queue() );
Markgoldin - 30 Jul 2008 21:48 GMT
This is a bit above my head. How do I pull data from it?

> found the post:
>
[quoted text clipped - 13 lines]
>
> syncedqueue = Queue.Synchonize( new Queue() );

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.