Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

Socket shuts down unexpectedly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SBC - 21 Apr 2005 21:10 GMT
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
Ioannis Vranos - 22 Apr 2005 00:11 GMT
> Hello,
>
[quoted text clipped - 8 lines]
>
> 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?
SBC - 22 Apr 2005 00:40 GMT
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.

> > Hello,
> >
[quoted text clipped - 18 lines]
> after using a TcpListener to setup a connection, do you create a new TcpListener object
> afterwards?
Ioannis Vranos - 22 Apr 2005 13:22 GMT
> Hello,
> I apologize for posting to the wrong group.
[quoted text clipped - 42 lines]
> }
> }

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.
German - 18 Jul 2005 19:15 GMT
Can I use this comannds in VC6.00? what directive i need to include ?

German Medina
+519-662-8500

> > Hello,
> > I apologize for posting to the wrong group.
[quoted text clipped - 65 lines]
> TcpListener::AcceptSocket(), you should create a new TcpListener if you want to accept
> connections from more than one client.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.