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();
}
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();
}