对等C#重置连接

时间:2019-11-18 20:23:08

标签: c# sockets tcp

所以我有Windows任务计划程序,每隔x分钟运行我的可执行文件。

可执行文件执行以下操作:

  1. 查看是否需要使用套接字将任何新消息发送到服务器。
  2. 如果有新消息,则连接到服务器 并发送每封邮件。
  3. 如果没有新消息,则退出 可执行文件。

问题: 我的可执行文件没有退出并保持运行。因此,Windows任务计划程序再次运行时,由于可执行文件的实例仍在运行,因此无法执行。

我发送邮件的服务器在错误日志中告诉我,他们看到了以下内容:

11-12-19 15:34:09 TCP / IP接收失败; errno 73-由对等方重置连接。

我的代码:

namespace TransportMessage
{
    class Program
    {
        static void Main(string[] args)
        {
            var systemUserId = 1

            try
            {
                using (var dbContext = new DBContext())
                {
                    var unsentRecords = dbContext.Records.Where(x => x.status == "QUEUED").ToList();

                    if (unsentRecords != null && unsentRecords.Count > 0)
                    {
                        var portNumber = 45454;
                        var dnsName = "xxxx.xxx.xxx.xxx";

                        IPAddress[] ipAddresses = Dns.GetHostAddresses(dnsName);

                        Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        IPAddress ipAdd = System.Net.IPAddress.Parse(ipAddresses[0].ToString());
                        IPEndPoint remoteEP = new IPEndPoint(ipAdd, portNumber);
                        soc.Connect(remoteEP);

                        foreach (var rec in unsentRecords)
                        {
                            try
                            {
                                byte[] byData = System.Text.Encoding.ASCII.GetBytes(rec.message_text);
                                var numberOfBytesSent = soc.Send(byData);

                                byte[] buffer = new byte[8000];
                                int iRx = soc.Receive(buffer);
                                char[] chars = new char[iRx];

                                Decoder d = Encoding.UTF8.GetDecoder();
                                int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
                                string recv = new string(chars);

                                rec.sent_ack_message = recv;
                                rec.number_of_bytes_sent = numberOfBytesSent;

                                var ackMessageStatus = rec.AckMessageStatus();

                                if (!String.IsNullOrEmpty(ackMessageStatus))
                                {
                                    if (ackMessageStatus == "NNN")
                                    {
                                        //Sent successfully.  NNN= Good
                                        rec.status = Status.SENT.ToString();
                                    }
                                    else
                                    {
                                        //User received anything else which means something bad in the message.
                                        rec.status = Status.FAILED.ToString();
                                        rec.last_failed_date = DateTime.Now;
                                        rec.last_failed_reason = "Message format incorrect. Please contact IT Operations. Ack Message status " + ackMessageStatus + ".";
                                    }
                                }
                                else
                                {
                                    rec.status = Status.QUEUED.ToString();
                                    rec.last_failed_date = DateTime.Now;
                                    rec.last_failed_reason = "Message sent but no acknowledgment message was received. Message sent back to queued state to try again.";
                                }

                                rec.last_modified_by = systemUserId;
                                rec.last_modified_date = DateTime.Now;
                                rec.status_date = DateTime.Now;
                                rec.sent_date = DateTime.Now;
                                rec.sent_attempt_count = rec.sent_attempt_count + 1;
                                dbContext.SaveChanges();

                            }
                            catch (Exception e)
                            {
                                rec.status = Status.FAILED.ToString();
                                rec.last_modified_by = systemUserId;
                                rec.last_modified_date = DateTime.Now;
                                rec.status_date = DateTime.Now;
                                rec.last_failed_date = DateTime.Now;
                                rec.last_failed_reason = e.ToString();
                                rec.sent_attempt_count = rec.sent_attempt_count + 1;
                                dbContext.SaveChanges();
                            }
                        }
                    }
                }
            }
            catch (System.Net.Sockets.SocketException e)
            {
                Util.LogError(LogType.FAILED_CONNECTION, e.Message, e.StackTrace);
            }
            catch (System.Data.DataException e)
            {
                Util.LogError(LogType.FAILED_TO_CONNECT_TO_DATABASE, e.Message, e.StackTrace);
            }
            catch (Exception e)
            {
                //General error if its not database or a socket exception issue.
                Util.LogError(LogType.ERROR, e.Message, e.StackTrace);
            }
        }
    }
}

问题: 当对等方重置连接后如何处理?

我在想,也许是在我致电soc.Connect(remoteEP)之后。我检查是否soc.Connected为假。如果是这样,那么我断开连接。

  if (!soc.Connected)
  {
    soc.Disconnect(true);
  }

注意: 发送消息后,我正在断开套接字,但是服务器上的管理员告诉我不要这样做。而且可执行文件已经运行了3周。没有错误或任何东西。

0 个答案:

没有答案