TCPClient

R

Regfellow

What is wrong with the following code? After 20-40 server connections i
get a socket Exception:

No connection could be made because the target machine actively refused
it. Error code 10064.


public byte[] SendMessage(byte[] byteSendData) {


try {
TcpClient objClient = new TcpClient();

this.objClient.Connect(address,port);

// Get a client stream for reading and writing.
NetworkStream stream = objClient.GetStream();

// Send the message to the connected TcpServer.
stream.Write(byteSendData, 0, byteSendData.Length);

// Receive the TcpServer.response.
// Buffer to store the response bytes.
byte[] byteReceiveData = new Byte[500];

// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(byteReceiveData , 0, byteReceiveData
..Length);

return byteReceiveData ;


} catch (SocketException se) {

throw new Exception("SocketException: " + se.Message);
} catch (Exception ex) {
throw new Exception("Exception: " + ex.Message);
} finally {
// Close everything.
objClient.Close();
}
 
C

Chris Botha

Actually error 10064 means "Host is down" and the error message "the target
machine actively refused it" my be confusing. Maybe you should check after
the error occurred if you can connect to another IP address, my guess would
be that the problem is on the device side rather than the server. Write a
Windows app and see if you can connect to the server more than 40 times.

There was an issue in the "big" Framework that may be related. In this case
the person created and closed sockets in a tight loop and eventually his
program would brake (I can't remember the error message). By using some
monitor tool he found that the Framework did not garbage-collect the sockets
while in the loop and even though he did call Close, the client port was
only released when the garbage collection ran, so his app ran out of
available ports.
 
L

LostAtC

Closing a TCP Client connection seems not be be as easy as:

objClient.Close()

In fact the VS2005 Beta 2 docs, in regard to the TCPClient.Close
method says:

Disposes this TcpClient instance without closing the underlying
connection.

Which begs the question, what does close the connection?

I think that you need to close the stream first then the client:

stream.Close()
objClient.Close()

Try that and see if your results are any better. I'd also add a small
delay between closing then reopening the connection, if your hammering
it.

Actually error 10064 means "Host is down" and the error message "the target
machine actively refused it" my be confusing. Maybe you should check after
the error occurred if you can connect to another IP address, my guess would
be that the problem is on the device side rather than the server. Write a
Windows app and see if you can connect to the server more than 40 times.

There was an issue in the "big" Framework that may be related. In this case
the person created and closed sockets in a tight loop and eventually his
program would brake (I can't remember the error message). By using some
monitor tool he found that the Framework did not garbage-collect the sockets
while in the loop and even though he did call Close, the client port was
only released when the garbage collection ran, so his app ran out of
available ports.


Regfellow said:
What is wrong with the following code? After 20-40 server connections i
get a socket Exception:

No connection could be made because the target machine actively refused
it. Error code 10064.


public byte[] SendMessage(byte[] byteSendData) {


try {
TcpClient objClient = new TcpClient();

this.objClient.Connect(address,port);

// Get a client stream for reading and writing.
NetworkStream stream = objClient.GetStream();

// Send the message to the connected TcpServer.
stream.Write(byteSendData, 0, byteSendData.Length);

// Receive the TcpServer.response.
// Buffer to store the response bytes.
byte[] byteReceiveData = new Byte[500];

// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(byteReceiveData , 0, byteReceiveData
.Length);

return byteReceiveData ;


} catch (SocketException se) {

throw new Exception("SocketException: " + se.Message);
} catch (Exception ex) {
throw new Exception("Exception: " + ex.Message);
} finally {
// Close everything.
objClient.Close();
}
 
R

Regfellow

Hi,

After alot of reading threads on the internet and alot of
debugging/testing it seems to be Active Sync that is the problem.

It seems to be a "feature" in AS 3.7/3.8 that doesnt handle tcp and
threads so well... saw a thread about someone that had the same problem
with the opennetcfs ftp class..

When using my program with GPRS, it seems to work as it should.

Thanx for the input!

Mats Boberg
 

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