471,354 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

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->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();
}
}
Nov 30 '05 #1
2 10789
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();
}
}

Nov 30 '05 #2
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();
}
}

Nov 30 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by J. Dudgeon | last post: by
1 post views Thread by JDF | last post: by
4 posts views Thread by Ted | last post: by
5 posts views Thread by John Sheppard | last post: by
reply views Thread by SBC | last post: by
4 posts views Thread by Michael Lindsey | last post: by
8 posts views Thread by =?Utf-8?B?Sm9obg==?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.