socket programming and windows service

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

Lenard Gunda

Hello,

If I understand your code correctly, in OnStart, you call startlisten(),
which will contain a loop that listens to connections.

However, OnStart should return as soon as possible. This works a little
differently with C# services than with services that are done using C++
and native code.

So, simply put the code that is now in OnStart to another thread, and
return from OnStart when the thread is started. That will mark your
service running.

You might also need some synchronization code added, if you move that
functionality to another thread.

-Lenard


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

Ankit Aneja

thanks it works

Lenard Gunda said:
Hello,

If I understand your code correctly, in OnStart, you call startlisten(),
which will contain a loop that listens to connections.

However, OnStart should return as soon as possible. This works a little
differently with C# services than with services that are done using C++
and native code.

So, simply put the code that is now in OnStart to another thread, and
return from OnStart when the thread is started. That will mark your
service running.

You might also need some synchronization code added, if you move that
functionality to another thread.

-Lenard


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

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