PC Review


Reply
Thread Tools Rate Thread

C# Socket Problem

 
 
=?Utf-8?B?TXIuIEJlY2s=?=
Guest
Posts: n/a
 
      25th Aug 2006
Hello,

Please Help.....

I have been working with some tcp/ip socket communication within a C#
program recently. Basicly, I have a program (myProblemProgram) that has a
socket connected to another program for information passing. Upon receiving
a particular "command" from the the information passing program,
myProblemProgram will launch a separate thread to do individual communication
with another file transfer program. The thread spawned off will create it's
own server connection to accept an incoming client. The thread creates a
network stream to do the communication over. I am also using SSL sockets in
order to keep my communication secure. MyProblemProgram will then try to
authenticate the stream with a particular SSL certificate and proceed to do
some functionality to transfer files over the net.

For about 2 months this process has been working great until a few days ago.
All of the sudden myProblemProgram has been failing on the authentication of
the stream from the connection. I start up myProblemProgram and it works
great for a while. Sometimes it's for up to 24 hours before the problem
happens and sometimes it's only 30 minutes before the authentication fails...
but once it fails, it keeps continuing to fail. I've been looking into what
the cause of the problem is, but have been unable to find an answer. Another
weird thing is that after I restart myProblemProgram, it fixes the
authentication problem right away and continues to work fine without having
any problems (at least for a little while).


The code below is straight from my program and I will explain what exactly
is happening.

//This function is created as a separate thread and tries to accept incoming
connections from ProgramX and to do the actual file transfers to it
private void AcceptIncomingTransfer(object thread_ref_num)
{
X509Certificate serverCertificate = null;
serverCertificate =
X509Certificate.CreateFromCertFile(server_cert_name);
//server_cert_name is the name of an SSL certificate I am choosing to
use
//(within the program's working directory)

//server is a TcpListener object, that is set up in another function
TcpClient client = server.AcceptTcpClient();
SslStream sslStream = new SslStream(client.GetStream(), false);

try
{
//!!!! This works for a long time and then all of the sudden fails
//and throws an exception
sslStream.AuthenticateAsServer(serverCertificate);

//sslStream.AuthenticateAsServer(serverCertificate, false,
SslProtocols.Default, true); //<--- I have also thried authenticating this
way, and this will work for awhile and fail just the same as the above
previsou liine
}
catch (Exception authentication_exception)
{
//The exception that gets caught is a System.IO.IOException
//It also contains an inner exception of type
//System.Net.Sockets.SocketException.

//In a "logging class" I log all the information from these
exceptions....
//the information from the exceptions from an
//authentication_exception.ToString() call is as follows:

System.IO.IOException: Unable to read data from the transport connection: An
existing connection was forcibly closed by the remote host. --->
System.Net.Sockets.SocketException: An existing connection was forcibly
closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset,
Int32 count)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer,
AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst,
Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult
lazyResult)
at System.Net.Security.SslStream.AuthenticateAsServer(X509Certificate
serverCertificate, Boolean clientCertificateRequired, SslProtocols
enabledSslProtocols, Boolean checkCertificateRevocation)
at System.Net.Security.SslStream.AuthenticateAsServer(X509Certificate
serverCertificate)

//I also try to make a SocketException out of the inner exception
and do
//a socket_exception.ErrorCode to see what the
//socket error code is but for some reason it will not give me it.
}

// .
// .
// .
// .
// .
//
// At the bottom of this function is where I close the stream & client
connection and terminate the thread
}



I am not sure if this problem is do to the certificate not authenticating
properly or if there is some sort of internal program socket error or if
there is something on the system I am running it on that is causing a
problem. I have not been able to find anybody with any similar issue and
have been at a loss on how to fix this. If anybody could help out with this
problem, I would greatly appreciate it.


Thank You,
Martin
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TXIuIEJlY2s=?=
Guest
Posts: n/a
 
      25th Aug 2006
Update from me....

I got this error to happen again, but this time I was able to get an error
code out of the inner SocketException. The error code outputted was "10054".
I looked this up on Microsoft's Socket Error Code reference and they say:

WSAECONNRESET
10054
Connection reset by peer.
An existing connection was forcibly closed by the remote host. This normally
results if the peer application on the remote host is suddenly stopped, the
host is rebooted, the host or remote network interface is disabled, or the
remote host uses a hard close (see setsockopt for more information on the
SO_LINGER option on the remote socket). This error may also result if a
connection was broken due to keep-alive activity detecting a failure while
one or more operations are in progress. Operations that were in progress fail
with WSAENETRESET. Subsequent operations fail with WSAECONNRESET.


It doesn't seem that any of the causes listed in this description are what
is happening in my situation. I'm not entirely sure.

I still need help trying to figure this one out. I just thought I'd try to
provide some more information.

Thanks again,
Martin.



"Mr. Beck" wrote:

> Hello,
>
> Please Help.....
>
> I have been working with some tcp/ip socket communication within a C#
> program recently. Basicly, I have a program (myProblemProgram) that has a
> socket connected to another program for information passing. Upon receiving
> a particular "command" from the the information passing program,
> myProblemProgram will launch a separate thread to do individual communication
> with another file transfer program. The thread spawned off will create it's
> own server connection to accept an incoming client. The thread creates a
> network stream to do the communication over. I am also using SSL sockets in
> order to keep my communication secure. MyProblemProgram will then try to
> authenticate the stream with a particular SSL certificate and proceed to do
> some functionality to transfer files over the net.
>
> For about 2 months this process has been working great until a few days ago.
> All of the sudden myProblemProgram has been failing on the authentication of
> the stream from the connection. I start up myProblemProgram and it works
> great for a while. Sometimes it's for up to 24 hours before the problem
> happens and sometimes it's only 30 minutes before the authentication fails...
> but once it fails, it keeps continuing to fail. I've been looking into what
> the cause of the problem is, but have been unable to find an answer. Another
> weird thing is that after I restart myProblemProgram, it fixes the
> authentication problem right away and continues to work fine without having
> any problems (at least for a little while).
>
>
> The code below is straight from my program and I will explain what exactly
> is happening.
>
> //This function is created as a separate thread and tries to accept incoming
> connections from ProgramX and to do the actual file transfers to it
> private void AcceptIncomingTransfer(object thread_ref_num)
> {
> X509Certificate serverCertificate = null;
> serverCertificate =
> X509Certificate.CreateFromCertFile(server_cert_name);
> //server_cert_name is the name of an SSL certificate I am choosing to
> use
> //(within the program's working directory)
>
> //server is a TcpListener object, that is set up in another function
> TcpClient client = server.AcceptTcpClient();
> SslStream sslStream = new SslStream(client.GetStream(), false);
>
> try
> {
> //!!!! This works for a long time and then all of the sudden fails
> //and throws an exception
> sslStream.AuthenticateAsServer(serverCertificate);
>
> //sslStream.AuthenticateAsServer(serverCertificate, false,
> SslProtocols.Default, true); //<--- I have also thried authenticating this
> way, and this will work for awhile and fail just the same as the above
> previsou liine
> }
> catch (Exception authentication_exception)
> {
> //The exception that gets caught is a System.IO.IOException
> //It also contains an inner exception of type
> //System.Net.Sockets.SocketException.
>
> //In a "logging class" I log all the information from these
> exceptions....
> //the information from the exceptions from an
> //authentication_exception.ToString() call is as follows:
>
> System.IO.IOException: Unable to read data from the transport connection: An
> existing connection was forcibly closed by the remote host. --->
> System.Net.Sockets.SocketException: An existing connection was forcibly
> closed by the remote host
> at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
> size, SocketFlags socketFlags)
> at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
> Int32 size)
> --- End of inner exception stack trace ---
> at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
> Int32 size)
> at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset,
> Int32 count)
> at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer,
> AsyncProtocolRequest asyncRequest)
> at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst,
> Byte[] buffer, AsyncProtocolRequest asyncRequest)
> at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult
> lazyResult)
> at System.Net.Security.SslStream.AuthenticateAsServer(X509Certificate
> serverCertificate, Boolean clientCertificateRequired, SslProtocols
> enabledSslProtocols, Boolean checkCertificateRevocation)
> at System.Net.Security.SslStream.AuthenticateAsServer(X509Certificate
> serverCertificate)
>
> //I also try to make a SocketException out of the inner exception
> and do
> //a socket_exception.ErrorCode to see what the
> //socket error code is but for some reason it will not give me it.
> }
>
> // .
> // .
> // .
> // .
> // .
> //
> // At the bottom of this function is where I close the stream & client
> connection and terminate the thread
> }
>
>
>
> I am not sure if this problem is do to the certificate not authenticating
> properly or if there is some sort of internal program socket error or if
> there is something on the system I am running it on that is causing a
> problem. I have not been able to find anybody with any similar issue and
> have been at a loss on how to fix this. If anybody could help out with this
> problem, I would greatly appreciate it.
>
>
> Thank You,
> Martin

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory configurations in socket?2x256 socket 1/3 and 2x512 socket GT Windows Vista Hardware 1 25th Nov 2007 06:18 AM
Socket.BeginSendTo and Socket.BeginSendFrom on a single Socket instancefrom multiple threads Jonas Hei Microsoft Dot NET Framework 9 5th Jul 2005 03:27 AM
Socket.BeginSendTo and Socket.BeginSendFrom on a single Socket instancefrom multiple threads Jonas Hei Microsoft C# .NET 2 22nd Jun 2005 12:10 PM
Socket.BeginReceive/Socket.EndReceive Documentation Problem Cool Guy Microsoft Dot NET Framework 5 11th Jun 2005 11:19 PM
Network problem: "Could not start DB server: socket() failed for UDP socket" Antti Heiskanen Windows XP Networking 3 2nd Apr 2004 08:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:30 PM.