English 中文(简体)
适当实施C# TCP与客户重新连接
原标题:Proper implementation of C# TCP reconnecting client

我必须写一份<代码>TCP客户,以便能够与服务器重新连接。 由于网络连接质量差或一些维修问题,服务器无法使用。 寻找该领域的质量解决办法。

我目前的解决方案如下:

  • keep connection state in ConnectionState enum {Offline, Online, Connecting}
  • create client with TcpClient class.
  • create two timers called ConnectionCheckTimer, and ReconnectTimer
  • connect to server
  • start reader thread and connection check timer
  • reading is performed with tcpClient.GetStream() and then reading from this stream
  • when Exception is caught in readerLoop client state is changed to offline and ReconnectTimer is launched
  • ConnectionCheckTimer periodically checks lastMessageTimestamp and compares it with current time if the interval is greater then maxValue it launches ReconnectTimer

目前,由于这种解决办法仍然产生例外,例如对TcpClient的反对意见。 网络Stream。 I m 寻找一些清洁和可再利用的Tcp重新连接客户执行工作,以便能够应对连接、脱节、阅读数据过程中可能出现的所有袖珍问题。

问题回答

如果你有联系问题,你总是有例外。 我认为,你有一个健全的大纲,你刚刚需要处理这些例外情况。 您可以开始使用自己的Socket级杂质,并撰写TCPIP服务器。 起始代码为:

http://msdn.microsoft.com/en-us/library/fx6588te(VS.71).aspx

C#代码在VB页下半行。

The class you should use is "SocketAsyncEventArgs". I ve used it in this project:

http://ts3querylib.codeplex.com/

检查阿辛卡特帕迪班。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签