socket -client

A

Ankit Aneja

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();
}
}
 
V

Vadym Stetsyak

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 said:
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();
}
}
 
K

Kevin Yu [MSFT]

A

Ankit Aneja

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 said:
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 said:
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();
}
}
 
K

Kevin Yu [MSFT]

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 = new client();
}
//..............
}

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

Ankit Aneja

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.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.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.status==true)

{

cl= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl.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;

}

}
 
K

Kevin Yu [MSFT]

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 is a null
reference first.

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

Kevin Yu [MSFT]

You're welcome.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top