473,785 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

socket programming and windows service

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->Administrati ve 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(por t);
server = new TcpListener(loc alAddr, 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.AcceptSo cket() here.
cl= new client(server.A cceptTcpClient( ));
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(TcpClien t Client)
{
//
// TODO: Add constructor logic here
tcpClient =Client;
}
public void getClient()
{
data = null;

// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetSt ream();

int i;

// Loop to receive all the data sent by the client.
while((i = stream.Read(byt es, 0, bytes.Length))! =0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Enc oding.ASCII.Get String(bytes, 0, i);
Console.WriteLi ne(String.Forma t("Received: {0}", data));

// Process the data sent by the client.
data = data.ToUpper();

byte[] msg = System.Text.Enc oding.ASCII.Get Bytes(data);

// Send back a response.
stream.Write(ms g, 0, msg.Length);
//Console.WriteLi ne(String.Forma t("Sent: {0}", data));
}

// Shutdown and end connection
tcpClient.Close ();
}
}
Nov 30 '05 #1
2 11162
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->Administrati ve 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(por t);
server = new TcpListener(loc alAddr, 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.AcceptSo cket() here.
cl= new client(server.A cceptTcpClient( ));
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(TcpClien t Client)
{
//
// TODO: Add constructor logic here
tcpClient =Client;
}
public void getClient()
{
data = null;

// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetSt ream();

int i;

// Loop to receive all the data sent by the client.
while((i = stream.Read(byt es, 0, bytes.Length))! =0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Enc oding.ASCII.Get String(bytes, 0, i);
Console.WriteLi ne(String.Forma t("Received: {0}", data));

// Process the data sent by the client.
data = data.ToUpper();

byte[] msg = System.Text.Enc oding.ASCII.Get Bytes(data);

// Send back a response.
stream.Write(ms g, 0, msg.Length);
//Console.WriteLi ne(String.Forma t("Sent: {0}", data));
}

// Shutdown and end connection
tcpClient.Close ();
}
}

Nov 30 '05 #2
thanks it works

"Lenard Gunda" <ar***********@ freemail.hu> wrote in message
news:en******** ******@TK2MSFTN GP12.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->Administrati ve 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(por t);
server = new TcpListener(loc alAddr, 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.AcceptSo cket() here.
cl= new client(server.A cceptTcpClient( ));
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(TcpClien t Client)
{
//
// TODO: Add constructor logic here
tcpClient =Client;
}
public void getClient()
{
data = null;

// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetSt ream();

int i;

// Loop to receive all the data sent by the client.
while((i = stream.Read(byt es, 0, bytes.Length))! =0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Enc oding.ASCII.Get String(bytes, 0, i);
Console.WriteLi ne(String.Forma t("Received: {0}", data));

// Process the data sent by the client.
data = data.ToUpper();

byte[] msg = System.Text.Enc oding.ASCII.Get Bytes(data);

// Send back a response.
stream.Write(ms g, 0, msg.Length);
//Console.WriteLi ne(String.Forma t("Sent: {0}", data));
}

// Shutdown and end connection
tcpClient.Close ();
}
}

Nov 30 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
434
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 sockets. The "client" to the service is a C# class library that also uses sockets. I've managed to get the performance of the service up to a decent speed but I've hit a strange problem.
1
2229
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 on my Citrix servers for usage tracking purposes. The service itself seems to function properly, it starts, stops, logs to the event viewer, etc. The problem seems to be with the portion where it either waits to be stopped or waits for remote...
4
4842
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 call to sendto. I was hoping that if someone has the time they could take a look at my code posted below and let me know what i'm doing wrong?
5
3687
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 but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic. My programming environment is Windows XP, Visual Studio .NET 2003 and C#. So here it...
0
1275
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 everyday they need to restart the service so that the application starts listening on the sockets again. I have not been able to recreate this situation and don't know why the socket would shutdown. Can anyone point me to a reason why sockets would...
4
5347
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 trying to decide if I should use remoting vs. writing a server that uses networkstreams. I have read that networkstreams\tcp programming should be faster than remoting and is a better choice for what I am doing but that it is difficult to code.
9
20633
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 linux based system or Windows based servers. WCF, as I understand, has overhead vs. native socket ..Net programming. Should I continue to use socket programming or look into the new WCF model? Thanks. Hao
0
1902
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 to use the select statement to do only do this periodically. (I'm trying to be a client, accepting data, that might be my first problem) Basically this is the code that im working on to TEST to mimic what the httplib does, only return a socket...
8
4685
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 page_load event, I am using atl com component. Here one for loop is there. In this for loop, number of iterations are 1000, I can receive some data using com component. It is just set of some characters like
3
2093
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. But when a web service tries to use it or a windows service or even if I try to "remote it up" in a windows service to a remoting client I get nothing..It just doesnt connect. Once again the socket client is fine(OK honestly theres a problem iterating ...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.