QueueManager.Disconnect() and QueueManager.Close() Difference? - c#

I have a code which disconnects queuemanger when the connection is broken or exception is thrown like below
if (queueManagerreceive != null)
if (queueManagerreceive.IsConnected)
{
if (queuereceive != null)
{
queuereceive.Close();
}
queueManagerreceive.Disconnect();
}
So i have issue with the above code when i have a 'MQRC_RECONNECT_TIMED_OUT'
exception then when it does a queuemanager.Disconnect() it throws an exception "MQRC_CONNECTION_BROKEN" and obviously which breaks the code as an exception is thrown so when i use queuemanager.Close() there was no exception thrown and the service was stable.
What is the difference? what should i use to drop and recreate a new connection?
Please help.

The MQQueueManager.Disconnect() closes all queues/topics/processed opened and closes the connection to queue manager. Calling Disconnect() on an already disconnected connection helps in freeing any resources allocated internally.
MQQueueManager.Close method is actually an inherited method of it's base class and this method closes any internal objects the MQQueueManager has allocated. . But you must always call Disconnect instead of Close because the Disconnect method closes the connection to queue manager while Close does not.
After MQRC_RECONNECT_TIMED_OUT error you have to create a new connection again using new MQQueueManager constructor.
You are getting a MQRC_RECONNECT_TIMED_OUT error which means that an established connection was broken (for whatever reason) and the MQ client attempted to reconnect for 30 minutes but still could not establish connection. You have to understand the reasons for this:
1) Is your queue manager down for that long?
2) Is there a network issue which is preventing connection to queue manager?
3) If your are using a multi-instance queue manager, why application is not getting connected to stand-by instance?

Related

WTelegramClient not reconnecting after Internet connection is lost

I use WTelegramClient. The client does not reconnect after the Internet connection is restored.
Showing this:
Connecting to 149.154.167.91:443...
SocketException HostUnreachable (10065): A socket operation was attempted to an unreachable host.
Connecting to [2001:67c:4e8:f004::a]:443...
Connecting to 149.154.167.50:443...
After the connection has been established, transient connection losses should be detected and handled automatically by WTelegramClient:
A reconnection is attempted automatically after a few seconds and pending API calls are automatically resent.
After MaxAutoReconnect reconnection attempts, if the connection can still not be re-established, the client.OnUpdate event receives a ReactorError object, so you can decide what to do.
Now the log you provide doesn't give much context so I presume this happens at the very beginning of opening a session with WTelegramClient.
In this case there is no automatic retry, you should just catch the connection error using a try..catch around the ConnectAsync or LoginUserIfNeeded call, and decide what to do, when to retry the connection.

WMQ Exception CWSMQ0006E ConnectionFactory.CreateConnection: CompCode 2, Reason 2102 while creating connection

I am facing some issue in WMQ when trying to establish connection with WMQ Topic from out C# application.
IBM.XMS.XMSException: CWSMQ0006E: An exception was received during the call to the method ConnectionFactory.CreateConnection: CompCode: 2, Reason: 2102.
During execution of the specified method an exception was thrown by another component.
See the linked exception for more information.
at IBM.XMS.Client.WMQ.Factories.WmqConnectionFactory.CreateProviderConnection(XmsPropertyContext connectionProps)
at IBM.XMS.Client.Impl.XmsConnectionFactoryImpl.CreateConnection(String userID, String password)
at IBM.XMS.Client.Impl.XmsConnectionFactoryImpl.CreateConnection()**
WMQ Client Log:
AMQ12984.0.FDC 2018/03/08 06:26:22.700000 Installation1 w3wp 12984 4235 XC035007 xcsCreateThread xecP_E_NO_RESOURCE OK
AMQ12984.0.FDC 2018/03/08 06:26:23.403000 Installation1 w3wp 12984 4235 XC022001 xcsDisplayMessage rrcE_CREATE_THREAD_ERROR OK**
The connectivity works perfectly fine the whole day but sometime in morning it starts throwing the exception. We are reusing the Factory object across difference services but each time service Open - Write Message - Close the connection in factory.
We are using IBM Client V7.5.0.5. This exception is occurring on multiple servers, but on different times, so can't blame processor or server configuration.
We are reusing the Factory object across difference services but each
time service Open - Write Message - Close the connection in factory.
Are you actually closing the connection? Or you letting the framework close the connection which of course it is not? Check your code and explicitly close the connection in your code.

socket disconnection notification method

just searched for a posibble solution to indetify when the client disconnecets.
i found this:
public bool IsConnected( Socket s)
{
try
{
return !(s.Poll(1, SelectMode.SelectRead) &&s.Available == 0);
}
catch (SocketException) { return false; }
}
im using a while loop in my main with thread.sleep(500) and running the Isconnectedmthod it works allright when i run it through the visual studio and when i click stop debugging it actually notify me in the server side program but when i just go to the exe in the bin directory and launch it-it's Indeed notify me for a connection but when i close the program (manually from the 'x' button) or through the task manager theIsConnected method apparently return still true.....
im using a simple tcp connection
client = new TcpClient();
client.Connect("10.0.0.2", 10);
server:
Socket s = tcpClient.Client;
while(true)
{
if (!IsConnected(s))
MessageBox.Show("disconnected");
}
(it's running on a thread btw).
any suggestion guys?
i even tried to close the connection when the client closes:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
client.Close();
s.Close();
Environment.Exit(0);
}
dont know what to do
What you are asking for is not possible. TCP will not report an error on the connection unless an attempt is made to send on the connection. If all your program ever does is receive, it will never notice that the connection no longer exists.
There are some platform-dependent exceptions to this rule, but none involving the simple disappearance of the remote endpoint.
The correct way for a client to disconnect is for it to gracefully close the connection with a "shutdown" operation. In .NET, this means the client code calls Socket.Shutdown(SocketShutdown.Send). The client must then continue to receive until the server calls Socket.Shutdown(SocketShutdown.Both). Note that the shutdown "reason" is generally "send" for the endpoint initiating the closure, and "both" for the endpoint acknowledging and completing the closure.
Each endpoint will detect that the other endpoint has shutdown its end by the completion of a receive operation with 0 as the byte count return value for that operation. Neither endpoint should actually close the socket (i.e. call Socket.Close()) until this two-way graceful closure has completed. I.e. each endpoint has both called Socket.Shutdown() and seen a zero-byte receive operation completion.
The above is how graceful closure works, and it should be the norm for server/client interactions. Of course, things do break. A client could crash, the network might be disconnected, etc. Typically, the right thing to do is to delay recognition of such problems as long as possible; for example, as long as the server and client have no need to actually communicate, then a temporary network outage should not cause an error. Forcing one is pointless in that case.
In other words, don't add code to try to detect a connection failure. For maximum reliability, let the network try to recover on its own.
In some less-common cases, it is desirable to detect connection failures earlier. In these cases, you can enable "keep alive" on the socket (to force data to be sent over the connection, thus detecting interruptions in the connection…see SocketOptionName.KeepAlive) or implement some timeout mechanism (to force the connection to fail if no data is sent after some period of time). I would generally recommend against the use of this kind of technique, but it's a valid approach in some cases.

What is the difference between an ObjectDisposedException and a SocketException

I've already looked up the definitions and all but since I am dealing with server code I wanna know EXACTLY what is going on here.
So the way I'm interpreting it so far is that a SocketException happens if something outside of the program happens to the socket; such as, the network going down or the client closing the connection on his end. Meaning I have to call shutdown and close on my end? Is this how I'm supposed to detect that a client shuts down the connection? When an exception is thrown?
The way I'm interpreting the ObjectDisposedException is that it occurs when I called shutdown and close on that socket from another thread or something and I am still using the dead socket somewhere else.

Closing System.Net.Sockets.TcpClient kills the connection for other TCPClients at the same IP Address

Just to be clear, all of the TCPClients I'm referring to here are not instances of my own class, they are all instances of System.Net.Sockets.TcpClient from Mono's implementation of .NET 4.0.
I have a server that is listening for client connections, as servers do. Whenever it gets a new client it creates a new TCPClient to handle the connection on a new thread. I'm keeping track of all the connections and threads with a dictionary. If the client disconnects, it sends a disconnect message to the server, the TCPClient is closed, the dictionary entry is removed and the thread dies a natural death. No fuss, no muss. The server can handle multiple clients with no problem.
However, I'm simulating what happens if the client gets disconnected, doesn't have a chance to send a disconnect message, then reconnects. I'm detecting whether a client has reconnected with a username system (it'll be more secure when I'm done testing). If I just make a new TCPClient and leave the old one running, the system works just fine, but then I have a bunch of useless threads lying around taking up space and doing nothing. Slackers.
So I try to close the TCPClient associated with the old connection. When I do that, the new TCPClient also dies and the client program throws this error:
E/mono (12944): Unhandled Exception: System.IO.IOException: Write failure ---> System.Net.Sockets.SocketException: The socket has been shut down
And the server throws this error:
Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.
Cannot read from a closed TextReader.
So closing the old TCPClient with a remote endpoint of say: 192.168.1.10:50001
Also breaks the new TCPClient with a remote endpoint of say:192.168.1.10:50002
So the two TCPClient objects have the same remote endpoint IP address, but different remote endpoint ports. But closing the one seems to stop the other from working. I want to be able to close the old TCPClient to do my cleanup, without closing the new TCPClient.
I suspect this is something to do with how TCPClient works with sockets at a low level, but not having any real understanding of that, I'm not in a position to fix it.
I had a similar issue on my socket server. I used a simple List instead of a dictionary to hold all of my current connections. In a continuous while loop that listens for new streams, I have a try / catch and in the catch block it kills the client if it has disconnected.
Something like this on the sever.cs:
public static void CloseClient(SocketClient whichClient)
{
ClientList.Remove(whichClient);
whichClient.Client.Close();
// dispose of the client object
whichClient.Dispose();
whichClient = null;
}
and then a simple dispose method on the client:
public void Dispose()
{
System.GC.SuppressFinalize(this);
}
EDIT: this paste is the OPs resolution which he or she found on their own with help from my code.
So to clarify, the situation is that I have two TCPClient objects TCPClientA and TCPClientB with different remote endpoints ports, but the same IP:
TCPClientA.Client.RemoteEndPoint.ToString();
returns: 192.168.1.10:50001
TCPClientB.Client.RemoteEndPoint.ToString();
returns: 192.168.1.10:50002
TCPClientA needs to be cleaned up because it's no longer useful, so I call
TCPClientA.Close();
But this closes the socket for the client at the other end of TCPClientB, for some reason. However, writing
TCPClientA.Client.Close();
TCPClientA.Close();
Successfully closes TCPClientA without interfering with TCPClientB. So I've fixed the problem, but I don't understand why it works that way.
Looks like you have found a solution but just so you are aware there are many similar pitfalls when writing client/server applications in .net. There is an open source network library (which is fully supported in mono) where these problems have already been solved, networkComms.net. A basic sample is here.
Disclaimer: This is a commercial product and I am the founder.
This is clearly an error in your code. Merely closing one inbound connection cannot possibly close another one. Clearly something else is happening elsewhere in your code.

Categories

Resources