473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

C# Socket Problem

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
1 17501
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Thomas Hervé | last post by:
My problem is not really python specific but as I do my implementation in python I hope someone here can help me. I have two programs that talk through a socket. Here is the code : <server>...
4
by: Bryan Olson | last post by:
Here's the problem: Suppose we use: import socket f = some_socket.makefile() Then: f.read() is efficient, but verbose, and incorrect (or at least does not play will with others);
4
by: faktujaa | last post by:
Hi, I am having some problem with callback used in socket implementation. private static void Connect(string strPrtrIPAddr, int intPrtrPort, ref Socket rsocClient) { try { // Create remote end...
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
4
by: Sa¹o Zagoranski | last post by:
Hi! I'm writing a simple 3D First person shooter game. It is a multiplayer game, where all the players connect to one server.
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
6
by: ahlongxp | last post by:
socket.makefile() may lose data when "connection reset by peer". and socket.recv() will never lose the data. change the "1" to "0" in the client code to see the difference. confirmed on both...
0
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
16
by: =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?= | last post by:
hi all! I got a problem. I declared a SOCKET var in my C program but when i compiled the program it displayed like *--------------------------------------------------------------* *'SOCKET':...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.