473,397 Members | 1,972 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,397 software developers and data experts.

socket -client

i am doing here some some socket-client work in C# windows service
it is working fine for multiple clients

now i want to limit these multiple clients to 25 for example
i want that when service starts objects for all these 25 clients
are created and when client connects it should be accepted and will not
allow more than 25 clients to connect
and when client diconnects that object can be allocated to another client
who requests
i am not able to make up logic how i will first create 25 objects and store
them in array
and how i will be checking wether object is free or not to be allocated to
another client
code:

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
listen obj;
Thread threadlisten=null;
obj=new listen();
threadlisten =new Thread(new ThreadStart(obj.startlisten));
threadlisten.Start();
}

in listen class listen.cs

public class listen
{
TcpListener server=null;
Thread tcpthread=null;
client cl=null;
public listen()
{
//
// TODO: Add constructor logic here
//
}
public void startlisten()
{
Int32 port = 3310;
IPAddress localAddr = IPAddress.Parse("192.168.0.5");

// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);

// Start listening for client requests.
server.Start();

// Enter the listening loop.
while(true)
{
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
cl= new client(server.AcceptTcpClient());
tcpthread=new Thread(new ThreadStart(cl.getClient));
tcpthread.Start();

}
}
public void stoplisten()
{
server.Stop();
}
}
in client class client.cs

public class client
{
TcpClient tcpClient;

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
public client(TcpClient Client)
{
//
// TODO: Add constructor logic here
tcpClient =Client;
}
public void getClient()
{
try
{
data = null;

// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetStream();

int i;

// Loop to receive all the data sent by the client.
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.
string replyMsg = data;
clamdCommand x=new clamdCommand();
replyMsg=x.Command(replyMsg);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

// Send back a response.
stream.Write(msg, 0, msg.Length);
//Console.WriteLine(String.Format("Sent: {0}", data));
}
}
catch(Exception se)
{
}

// Shutdown and end connection
tcpClient.Close();
}
}

Dec 2 '05 #1
8 2712
define global list e.g. ArraList

ArraList client = new ArrayList(25);

and in method startlisten you initalize the list with instances of client
class.

client class must have property TcpClient. This property will be used
instead of contructor.
So the here is the control flow. You recevie connection check in the list
what class has TcpClient == null, pick it from the list, initialize
TcpClient and use....

All of these operations have to be synchronized as you'are working on
multiple threads.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com
"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:Oi***************@TK2MSFTNGP11.phx.gbl...
i am doing here some some socket-client work in C# windows service
it is working fine for multiple clients

now i want to limit these multiple clients to 25 for example
i want that when service starts objects for all these 25 clients
are created and when client connects it should be accepted and will not
allow more than 25 clients to connect
and when client diconnects that object can be allocated to another client
who requests
i am not able to make up logic how i will first create 25 objects and
store
them in array
and how i will be checking wether object is free or not to be allocated to
another client
code:

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
listen obj;
Thread threadlisten=null;
obj=new listen();
threadlisten =new Thread(new ThreadStart(obj.startlisten));
threadlisten.Start();
}

in listen class listen.cs

public class listen
{
TcpListener server=null;
Thread tcpthread=null;
client cl=null;
public listen()
{
//
// TODO: Add constructor logic here
//
}
public void startlisten()
{
Int32 port = 3310;
IPAddress localAddr = IPAddress.Parse("192.168.0.5");

// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);

// Start listening for client requests.
server.Start();

// Enter the listening loop.
while(true)
{
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
cl= new client(server.AcceptTcpClient());
tcpthread=new Thread(new ThreadStart(cl.getClient));
tcpthread.Start();

}
}
public void stoplisten()
{
server.Stop();
}
}
in client class client.cs

public class client
{
TcpClient tcpClient;

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
public client(TcpClient Client)
{
//
// TODO: Add constructor logic here
tcpClient =Client;
}
public void getClient()
{
try
{
data = null;

// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetStream();

int i;

// Loop to receive all the data sent by the client.
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.
string replyMsg = data;
clamdCommand x=new clamdCommand();
replyMsg=x.Command(replyMsg);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

// Send back a response.
stream.Write(msg, 0, msg.Length);
//Console.WriteLine(String.Format("Sent: {0}", data));
}
}
catch(Exception se)
{
}

// Shutdown and end connection
tcpClient.Close();
}
}

Dec 2 '05 #2
Hi Ankit,

I think you're trying to implement a socket pool that has a limit of
clients. In this case, you can put a count when creating thread in the
while block. When the limit is reached, do not create any more threads.
Here is an article that might help you on this issue.

http://www.codeproject.com/internet/jbsocketserver1.asp

http://www.codeproject.com/internet/jbsocketserver2.asp

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 2 '05 #3
Can you give me the example for
" and in method startlisten you initalize the list with instances of client
class.

client class must have property TcpClient. This property will be used
instead of contructor.
So the here is the control flow. You recevie connection check in the list
what class has TcpClient == null, pick it from the list, initialize
TcpClient and use....

All of these operations have to be synchronized as you'are working on
multiple threads.
"
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:ek**************@tk2msftngp13.phx.gbl...
define global list e.g. ArraList

ArraList client = new ArrayList(25);

and in method startlisten you initalize the list with instances of client
class.

client class must have property TcpClient. This property will be used
instead of contructor.
So the here is the control flow. You recevie connection check in the list
what class has TcpClient == null, pick it from the list, initialize
TcpClient and use....

All of these operations have to be synchronized as you'are working on
multiple threads.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com
"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:Oi***************@TK2MSFTNGP11.phx.gbl...
i am doing here some some socket-client work in C# windows service
it is working fine for multiple clients

now i want to limit these multiple clients to 25 for example
i want that when service starts objects for all these 25 clients
are created and when client connects it should be accepted and will not
allow more than 25 clients to connect
and when client diconnects that object can be allocated to another client who requests
i am not able to make up logic how i will first create 25 objects and
store
them in array
and how i will be checking wether object is free or not to be allocated to another client
code:

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
listen obj;
Thread threadlisten=null;
obj=new listen();
threadlisten =new Thread(new ThreadStart(obj.startlisten));
threadlisten.Start();
}

in listen class listen.cs

public class listen
{
TcpListener server=null;
Thread tcpthread=null;
client cl=null;
public listen()
{
//
// TODO: Add constructor logic here
//
}
public void startlisten()
{
Int32 port = 3310;
IPAddress localAddr = IPAddress.Parse("192.168.0.5");

// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);

// Start listening for client requests.
server.Start();

// Enter the listening loop.
while(true)
{
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
cl= new client(server.AcceptTcpClient());
tcpthread=new Thread(new ThreadStart(cl.getClient));
tcpthread.Start();

}
}
public void stoplisten()
{
server.Stop();
}
}
in client class client.cs

public class client
{
TcpClient tcpClient;

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
public client(TcpClient Client)
{
//
// TODO: Add constructor logic here
tcpClient =Client;
}
public void getClient()
{
try
{
data = null;

// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetStream();

int i;

// Loop to receive all the data sent by the client.
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.
string replyMsg = data;
clamdCommand x=new clamdCommand();
replyMsg=x.Command(replyMsg);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

// Send back a response.
stream.Write(msg, 0, msg.Length);
//Console.WriteLine(String.Format("Sent: {0}", data));
}
}
catch(Exception se)
{
}

// Shutdown and end connection
tcpClient.Close();
}
}


Dec 5 '05 #4
Hi Ankit

I think Vadym means to initialize an instance for each item in the array.

public void startlisten()
{
//..............
for(int i=0;i<25;i++)
{
clients[i] = new client();
}
//..............
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 6 '05 #5
i am using this code but it is giving following error
System.NullReferenceException' occurred in
Additional information: Object reference not set to an instance of an
object.

it is breaking on line:if(cl[i].status==true)
code for listen class
public class listen

{

TcpListener server=null;

Thread tcpthread=null;

client[] cl=new client[5];
public listen()

{

//

// TODO: Add constructor logic here

//

}

public void startlisten()

{

Int32 port = 3310;

IPAddress localAddr = IPAddress.Parse("192.168.0.5");
// TcpListener server = new TcpListener(port);

server = new TcpListener(localAddr, port);

// Start listening for client requests.

server.Start();
// Enter the listening loop.

// for(int i=0;i<5;i++)

// {

// cl[i].status=true;

// }

Boolean flag;

while(true)

{ flag=false;

// Perform a blocking call to accept requests.

// You could also user server.AcceptSocket() here.

for(int i=0;i<5;i++)

{

if(cl[i].status==true)

{

cl[i]= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl[i].getClient));

tcpthread.Start();

flag=true;

break;

}

}

if(flag!=true)

{

//display error message

}

}

}

}

code for client class

public class client

{

TcpClient tcpClient;

public Boolean status;

// Buffer for reading data

Byte[] bytes = new Byte[256];

String data = null;

public client()

{ //

// TODO: Add constructor logic here

//

//status=true;

}

public client(TcpClient Client)

{

tcpClient =Client;

//

// TODO: Add constructor logic here

//

status=false;

}

public void getClient()

{

try

{

data = null;

// Get a stream object for reading and writing

NetworkStream stream = tcpClient.GetStream();

int i;

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

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.

string replyMsg = data;

clamdCommand x=new clamdCommand();

replyMsg=x.Command(replyMsg);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

// Send back a response.

stream.Write(msg, 0, msg.Length);

//Console.WriteLine(String.Format("Sent: {0}", data));

}

}

catch(Exception se)

{

MessageBox.Show(se.ToString());

}

// Shutdown and end connection

tcpClient.Close();

status=true;

}

}

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:ir*************@TK2MSFTNGXA02.phx.gbl...
Hi Ankit

I think Vadym means to initialize an instance for each item in the array.

public void startlisten()
{
//..............
for(int i=0;i<25;i++)
{
clients[i] = new client();
}
//..............
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 7 '05 #6
Hi,

The cl at this point is an array with 5 nulls. You have to new a client
object for its reference. Or, you can check to see if the cl[i] is a null
reference first.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 7 '05 #7
thanks
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:J9**************@TK2MSFTNGXA02.phx.gbl...
Hi,

The cl at this point is an array with 5 nulls. You have to new a client
object for its reference. Or, you can check to see if the cl[i] is a null
reference first.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 7 '05 #8
You're welcome.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 8 '05 #9

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

Similar topics

8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
4
by: Scott Robinson | last post by:
I have been having trouble with the garbage collector and sockets. Unfortunately, google keeps telling me that the problem is the garbage collector ignoring dead (closed?) sockets instead of...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
2
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send...
10
by: groups.20.thebriguy | last post by:
socket objects have a little quirk. If you try to receive 0 bytes on a blocking socket, they block. That is, if I call recv(0), it blocks (until some data arrives). I think that's wrong, but I...
8
by: Mark Fink | last post by:
I try to port a server application to Jython. At the moment I use Jython21\Lib\socket.py Currently I do face problems with casting the string "localhost" to the desired value:...
11
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running...
10
by: Hendrik van Rooyen | last post by:
While doing a netstring implementation I noticed that if you build a record up using socket's recv(1), then when you close the remote end down, the recv(1) hangs, despite having a short time out...
4
by: O.B. | last post by:
I have a socket configured as TCP and running as a listener. When I close socket, it doesn't always free up the port immediately. Even when no connections have been made to it. So when I open...
3
by: Giampaolo Rodola' | last post by:
Hi there, since the socket.socket.family attribute has been introduced only in Python 2.5 and I need to have my application to be backward compatible with Python 2.3 and 2.4 I'd like to know how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.