By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

socket -client

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
You're welcome.

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

Dec 8 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.