By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

C# Socket Problem

P: n/a
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_nam e);
//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(B oolean 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
Aug 25 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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_nam e);
//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(B oolean 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
Aug 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.