A
Ankit Aneja
I am making windows service using Microsoft visual studio.Net in C#
service name is clamservice
problem is that when i start the service it through
control pannel->Administrative tools->services
it shows "starting" and never shows "started"
it goes somewhere in loop i am doing some logical mistake
i want to create service supporting multiple clients using threading
pls help me
code:
listen obj;
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
obj=new listen();
obj.startlisten();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
obj.stoplisten();
}
now code for listen class which i make
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();
}
}
code for client class
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()
{
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);
Console.WriteLine(String.Format("Received: {0}", data));
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
//Console.WriteLine(String.Format("Sent: {0}", data));
}
// Shutdown and end connection
tcpClient.Close();
}
}
service name is clamservice
problem is that when i start the service it through
control pannel->Administrative tools->services
it shows "starting" and never shows "started"
it goes somewhere in loop i am doing some logical mistake
i want to create service supporting multiple clients using threading
pls help me
code:
listen obj;
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
obj=new listen();
obj.startlisten();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
obj.stoplisten();
}
now code for listen class which i make
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();
}
}
code for client class
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()
{
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);
Console.WriteLine(String.Format("Received: {0}", data));
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
//Console.WriteLine(String.Format("Sent: {0}", data));
}
// Shutdown and end connection
tcpClient.Close();
}
}