Socket shuts down unexpectedly

G

Guest

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
shutdown unexpectedly?

Thank you for your time
 
I

Ioannis Vranos

SBC said:
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
shutdown unexpectedly?

Thank you for your time


Since you are using C# a more proper group to begin with would be
microsoft.public.dotnet.languages.csharp

(here is for VC++).


However since it is not a language-specific question I think you can get good answers here
too. Myself am currently studying .NET networking and do not know much on this issue, but,
after using a TcpListener to setup a connection, do you create a new TcpListener object
afterwards?
 
G

Guest

Hello,
I apologize for posting to the wrong group.

Within the constructor I create a new TcpListener object...

<start of code snippet>
//start listing on the given port
port = Convert.ToInt32(sPort);
_tcpListener = new TcpListener(ipAddress, port) ;
_tcpListener.Start();

//start the thread which calls the method 'StartListen'
m_thread = new Thread(new ThreadStart(StartListen));
m_thread.Start() ;
<end of code snippet>


in my next snippet for "StartListen" I wait until a client connects...
<Start Snippet>
StartListen()
{
// variables here...
while(true)
{
// Accept a new connection
Socket _clientSocket = _tcpListener.AcceptSocket();
if (_clientSocket.Connected)
{
// At this point we have another application that does logging since the
windows
// service does not have a user interface...
TcpClient _tcpClient = new TcpClient();
try
{
_tcpClient.Connect(ipAddress, port)
NetworkStream ns = _tcpClient.GetStream();
BinaryWriter bw = new BinaryWriter(ns);
bw.Write(sb.ToString());
_tcpClient.Close();
sb.Remove(0, sb.Length); // sb is a StringBuilder
}
catch(Exception ex)
{
// stuff
}
}
}


One of the things I have recently read is that I need to close the
NetworkStream also, do you think this would cause the sockets to shutdown?
So far I don't receive any types of error messages, the socket just seems to
stop communicating and the windows service needs to be rebooted...

Thanks for your help.
 
I

Ioannis Vranos

SBC said:
Hello,
I apologize for posting to the wrong group.

Within the constructor I create a new TcpListener object...

<start of code snippet>
//start listing on the given port
port = Convert.ToInt32(sPort);
_tcpListener = new TcpListener(ipAddress, port) ;
_tcpListener.Start();

//start the thread which calls the method 'StartListen'
m_thread = new Thread(new ThreadStart(StartListen));
m_thread.Start() ;
<end of code snippet>


in my next snippet for "StartListen" I wait until a client connects...
<Start Snippet>
StartListen()
{
// variables here...
while(true)
{
// Accept a new connection
Socket _clientSocket = _tcpListener.AcceptSocket();
if (_clientSocket.Connected)
{
// At this point we have another application that does logging since the
windows
// service does not have a user interface...
TcpClient _tcpClient = new TcpClient();
try
{
_tcpClient.Connect(ipAddress, port)
NetworkStream ns = _tcpClient.GetStream();
BinaryWriter bw = new BinaryWriter(ns);
bw.Write(sb.ToString());
_tcpClient.Close();
sb.Remove(0, sb.Length); // sb is a StringBuilder
}
catch(Exception ex)
{
// stuff
}
}
}



If I understood correctly you are talking about a server service. Why do you use TcpClient
inside StartListen()? The TcpListener::AcceptSocket() method establishes the connection
and returns you a Socket object.



One of the things I have recently read is that I need to close the
NetworkStream also, do you think this would cause the sockets to shutdown?


Yes, the Socket object used by TcpClient shuts down with the above and you should close
all the above in turn:


// In C++ syntax

bw->Close();
ns->Close();
_tcpClient.Close();


So far I don't receive any types of error messages, the socket just seems to
stop communicating and the windows service needs to be rebooted...


Also in any case, as far as I know so far, after establishing a connection with
TcpListener::AcceptSocket(), you should create a new TcpListener if you want to accept
connections from more than one client.
 
G

Guest

Can I use this comannds in VC6.00? what directive i need to include ?

German Medina
+519-662-8500
 

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