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();
}
} 2 11063
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 Aneja wrote: 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(); } }
thanks it works
"Lenard Gunda" <ar***********@freemail.hu> wrote in message
news:en**************@TK2MSFTNGP12.phx.gbl... 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 Aneja wrote: 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(); } }
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J. Dudgeon |
last post by:
Hello,
I've written a server that acts as a proxy between the PC and the mainframe
worlds. This server service is writting in C# w/.NET 1.1. The service is
TCP/IP based and makes heavy use of...
|
by: JDF |
last post by:
I am fairly new to python and seem to have gotten ahead of myself. I
am trying to write a Windows service that will return the current
number of Citrix sessions to a client. This service will run...
|
by: Ted |
last post by:
Hi all,
I am trying to learn C socket programming and I have a small program
which is a UP client.
The problem is, when I run the program, I get a runtime error -
"Invalid Argument" - from a...
|
by: John Sheppard |
last post by:
Hi all,
I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
|
by: SBC |
last post by:
Hello,
I have developed a windows service using C#, TcpListener class and its only
function is to listen for clients to connect and send back a respsonse. I
have a customer who states that...
|
by: Michael Lindsey |
last post by:
I need to write a server app to send images to client GUIs that are outside
of the server's domain.
The client will have the file system path to the image but can not access
the file system.
I am...
|
by: Hao |
last post by:
We are doing very intensive network communication. We collect data from
thousands of electric devices every minutes. The devices understand both
socket and web service. They are either embeded...
|
by: shonen |
last post by:
I'm currently attempting to connect to a shoutcast server
pull down the information from here and then I'll parse it.
I got this working with the httplib, which was great, the problem is I want...
|
by: =?Utf-8?B?Sm9obg==?= |
last post by:
Hi all,
I am new to .net technologies.
ASP.NET supports socket programming like send/receive in c or c++?
I am developing web-site application in asp.net and code behind is Visual
C#. In...
|
by: ChristianProgrammer |
last post by:
Its been a month and a half now. I have tried to get my WSSF web service to reference a class library that IS a Socket Client. The Socket Client in question runs fine when called by a testing exe....
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |