473,327 Members | 2,025 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,327 software developers and data experts.

Multiple socket clients

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.
Jul 30 '08 #1
3 1451
>
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
Jul 30 '08 #2
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() );
Jul 30 '08 #3
This is a bit above my head. How do I pull data from it?
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:64**********************************@l42g2000 hsc.googlegroups.com...
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() );

Jul 30 '08 #4

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

Similar topics

9
by: User | last post by:
Hi, I tried to find the information over the internet but didn't find any answers. I'm looking for a server side code example of winsock accepting many clients. I know that in VB.NET it is...
7
by: rdh | last post by:
Hi all, I am in process of developing a Server in C++ supporting multiple protocols. The server will be exposing various functionalities, and the clients can communicate over any of the...
11
by: Mike | last post by:
Looking to find any information on how to properly configure multiple instances of DB2. This is on Win2k db2 ver 7.2. I am basically looking for information on how the multiple instance settings...
5
by: Rotzooi | last post by:
Hi, In the past I created a VB6 application that was capable of accepting multiple client connections for status logging over the internet using simple non-Windows clients (GSM/GPRS). There is...
7
by: Sharon | last post by:
Hi all, I've implemented a TCP server using the Socket async methods. When connecting to the server from 3 instances of hyper terminal, i've noticed that each of the newly created server sockets,...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
1
by: Riddle | last post by:
Hi, I'm relatively new to socket programming and I'm having trouble understanding them! I've (using help from tinternet and a useful tutorial) made a client and server that are able to talk to one...
1
by: kurrachaitanya | last post by:
Lecture Saying that: I am generally expecting most of you to work n java.If you prefer to use c/c++ version of the programs,you may,but the assistance in the lab sessions will be very much geared...
1
by: kurrachaitanya | last post by:
YOUR TASK IS AS FOLLOWS : The server is written in such a way that only one client can connect to it at any time.modify the server sothat several clients can be connected to it(and later disconnect...
4
by: Veeraraghavan | last post by:
Hi All, I am developing a client server communication using system.net.socket and I am finding it very difficult to get a solution for this. I started with single port communication with single...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.