private void PerformCloseByPS2() { client.Close(); Console.Error.WriteLine("PS2 has closed connection"); //Connection Close Part 2, Send ACK to PS2 ReceivedSequenceNumbers.RemoveAt(0); ReceivedSequenceNumbers.Add(ExpectedSequenceNumber); unchecked { ExpectedSequenceNumber += 1; } TCP ret = CreateBasePacket(); ret.ACK = true; ret.FIN = true; recvbuff.Add(ret); state = TCPState.ConnectionClosedByPS2AndRemote; lock (sentry) { OldMyNumber = MySequenceNumber; unchecked { MySequenceNumber += (1); } } }
public bool TCPRead(List <byte> tx) { bool ret; Debug.WriteLine(tx.Count); Debug.Write(Encoding.ASCII.GetString(tx.ToArray())); TCPTxData = tx; // IPPacket ip = NewIPPacket(); // TCPSeqNum++; // TCPPacket tcp = (TCPPacket)ip.oProtocolContent; // tcp.TCPOptions = new List<byte>(); // tcp.TCPOptions.Add(0x02);//段字节数最多为1024:0x4000 // tcp.TCPOptions.Add(0x04); // tcp.TCPOptions.Add(0x04); // tcp.TCPOptions.Add(0x00); // tcp.DataOffset++; // tcp.TCPFlags = 0x02;//同步 // IPSender(ip); // tcpState = TCPState.SYN; tcpState = TCPState.SYN; int ComTimeOut = Com.ReadTimeout; Com.ReadTimeout = 300; //Com.DataReceived -= COM_DataReceived; //注销事件关联,为发送做准备 ret = Read(); Com.ReadTimeout = ComTimeOut; //Com.DataReceived += COM_DataReceived; //ComWaitEvent.WaitOne(); return(ret); }
/// <summary> /// This is the main routine called from TCP layer for handling DICOM data /// </summary> /// <param name="state"></param> /// <param name="direction"></param> public void ReceiveTCPData(TCPState state, int direction) { byte[] p = state.data[direction]; int length = (int)state.Position[direction]; uint Position = 0; Association Assoc = (Association)state.UserInfo; bool TryAnotherPDU = true; while (length >= Position + 6 && TryAnotherPDU) { TryAnotherPDU = false; byte PDUType = p[Position]; uint Index = Position + 2; uint PDULength = DICOMUtility.Get4BytesBigEndian(p, ref Index); if (Assoc == null && PDUType == 1) { Assoc = new Association(state, BaseFileName + state.Signature[0], System.DateTime.Now); Assoc.Output += new Association.OutputEvent(OutputHandler); state.UserInfo = Assoc; string signature = state.Signature[0]; list.Add(signature, Assoc); OutputHandler("New : " + signature + "\r\n", true, true); // snifferObj.Invoke(snifferObj.AddConnectionHandler, new object[] { signature }); } if (Assoc != null && length >= Index + PDULength) { lock (Assoc) { Assoc.HandlePDU(p, Position, PDULength, direction, state); TryAnotherPDU = true; } } else if (length > 64000 && Assoc == null) // non-DICOM { OutputHandler("Non-DICOM data : " + state.Signature[0] + "\r\n", true, true); break; // don't process any more if we have a currently unhandleable PDU } else { break; // don't prcess any more if we have a currently unhandleable PDU } Position += PDULength + 6; } if (Position > 0) { if (state.data[direction] != null) { state.UsedData(Position, direction); } // Check if there is anything we have "stacked up" on the other direction ReceiveTCPData(state, 1 - direction); } return; }
public static TCPState Instance() { if (state == null) { state = new TCPEstablished(); } return(state); }
public static TCPState Instance() { if (state == null) { state = new TCPListen(); } return(state); }
private void AsyncConnectComplete(IAsyncResult res) { TCP tcp = (TCP)res.AsyncState; try { client.EndConnect(res); } catch (System.Net.Sockets.SocketException err) { Console.Error.WriteLine("TCP Connection Error: " + err.Message); Console.Error.WriteLine("ErrorCode: " + err.ErrorCode); } client.NoDelay = true; lock (sentry) { if (client.Connected) { open = true; state = TCPState.SentSYN_ACK; byte[] emptyByte = new byte[0]; TCP ret = new TCP(emptyByte); //Return the fact we connected ret.SourcePort = tcp.DestinationPort; ret.DestinationPort = tcp.SourcePort; ret.SequenceNumber = MySequenceNumber; MySequenceNumber += 1; ret.AcknowledgementNumber = ExpectedSequenceNumber; ret.SYN = true; ret.ACK = true; ret.WindowSize = 16 * 1024; ret.Options.Add(new TCPopMSS(MaxSegmentSize)); ret.Options.Add(new TCPopNOP()); ret.Options.Add(new TCPopWS(0)); if (SendTimeStamps) { ret.Options.Add(new TCPopNOP()); ret.Options.Add(new TCPopNOP()); ret.Options.Add(new TCPopTS((UInt32)TimeStamp.Elapsed.Seconds, LastRecivedTimeStamp)); } recvbuff.Add(ret); } else { open = false; state = TCPState.None; } } }
public void Receive() { try { // Create the state object. TCPState state = new TCPState(m_session); // Begin receiving the data from the remote device. m_session.BeginReceive(state.Buffer, 0, TCPState.BuffSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { Debug.LogError(e.ToString()); } }
private void PerformCloseByRemote() { client.Close(); Console.Error.WriteLine("Remote has closed connection"); TCP ret = CreateBasePacket(); ret.ACK = true; ret.FIN = true; recvbuff.Add(ret); state = TCPState.ConnectionClosedByRemote; lock (sentry) { OldMyNumber = MySequenceNumber; unchecked { MySequenceNumber += (1); } } }
public void EndOfStream(TCPState state, int direction, string result) { Association Assoc = (Association)state.UserInfo; if (Assoc != null) // i.e. if DICOM { lock (Assoc) { OutputHandler("End : " + state.Signature[0] + "\r\n" + result + "\r\n", true, true); Assoc.Out(result + "\r\n", true); Assoc.HandleEnd(); if ((state.State[0] != TCPState.States.RUNNING) && (state.State[1] != TCPState.States.RUNNING)) // i.e. if DICOM { state.Clear(); // should have been done elsewhere, but no harm to repeat // Select the first association from the list by default snifferObj.Invoke(snifferObj.SelectConnectionHandler); } //Handle incomplete byte stream if ((result.IndexOf("DEAD data seen") != -1) && (((state.State[0] != TCPState.States.RUNNING) && (state.State[1] != TCPState.States.FINISHED)) || ((state.State[0] != TCPState.States.FINISHED) && (state.State[1] != TCPState.States.RUNNING)))) { Assoc.Dispose(true); state.Clear(); } //Handle unexpected Abort if (result == "Forced Close") { Assoc.Dispose(true); state.Clear(); } } } }
private void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the asynchronous state object. TCPState state = (TCPState)ar.AsyncState; Socket client = state.Socket; if (!IsConnected()) { Debug.Log("<color=red>不想接收 了</color>"); return; } // Read data from the remote device. int bytesRead = client.EndReceive(ar); //Debug.Log("get from server, length = " + bytesRead); if (bytesRead > 0) { byte[] realData = new byte[bytesRead]; Array.Copy(state.Buffer, realData, bytesRead); //Globals.SceneSingleton<SocketNetworkMng>().Handler(realData); // 拼接数据包 if (m_buffer.TargetLength < 0) { m_buffer.Init(realData); if (m_buffer.CheckComplete()) { CmdRequest.Handle(this, m_buffer.Buffer.ToArray()); m_buffer.Clear(); } } else { if (m_buffer.CheckComplete()) { CmdRequest.Handle(this, m_buffer.Buffer.ToArray()); m_buffer.Clear(); } else { m_buffer.Buffer.AddRange(realData); } } Receive(); } } catch (SocketException e) { Debug.Log(e.ToString()); } catch (Exception e) { Debug.LogError(e.ToString()); } }
protected void ChangeState(TCPConnection connection, TCPState state) { connection.ChangeState(state); }
public override bool send(IPPayload payload) { TCP tcp = (TCP)payload; if (DestPort != 0) { if (!(tcp.DestinationPort == DestPort && tcp.SourcePort == SrcPort)) { Console.Error.WriteLine("TCP packet invalid for current session (Duplicate key?)"); return(false); } } if (tcp.RST == true) //Test this { if (client.Connected) { lock (sentry) { client.Close(); } state = TCPState.Closed; open = false; return(true); } } switch (state) { case TCPState.None: #region "SYN" DestPort = tcp.DestinationPort; SrcPort = tcp.SourcePort; if (tcp.SYN == false) { PerformRST(); Console.Error.WriteLine("Connection Not in Connected State"); return(true); } ExpectedSequenceNumber = tcp.SequenceNumber + 1; //Fill out last received numbers ReceivedSequenceNumbers.Add(tcp.SequenceNumber); ReceivedSequenceNumbers.Add(tcp.SequenceNumber); ReceivedSequenceNumbers.Add(tcp.SequenceNumber); ReceivedSequenceNumbers.Add(tcp.SequenceNumber); ReceivedSequenceNumbers.Add(tcp.SequenceNumber); for (int i = 0; i < tcp.Options.Count; i++) { switch (tcp.Options[i].Code) { case 0: //End case 1: //Nop continue; case 2: //MSS MaxSegmentSize = ((TCPopMSS)(tcp.Options[i])).MaxSegmentSize; break; case 3: //WinScale //Console.Error.WriteLine("Got WinScale"); // = ((TCPopWS)(tcp.Options[i])).WindowScale; break; case 8: //TimeStamp LastRecivedTimeStamp = ((TCPopTS)(tcp.Options[i])).SenderTimeStamp; SendTimeStamps = true; TimeStamp.Start(); break; default: Console.Error.WriteLine("Got Unknown Option " + tcp.Options[i].Code); throw new Exception(); //break; } } client = new TcpClient(); IPAddress address = new IPAddress(DestIP); client.BeginConnect(address, DestPort, new AsyncCallback(AsyncConnectComplete), tcp); state = TCPState.SendingSYN_ACK; open = true; return(true); #endregion case TCPState.SendingSYN_ACK: return(true); //Ignore reconnect attempts while we are still attempting connection case TCPState.SentSYN_ACK: #region "Syn-Ack" lock (sentry) { if (tcp.SYN == true) { throw new Exception("Attempt to Connect to an operning Port"); } NumCheckResult Result = CheckNumbers(tcp); if (Result == NumCheckResult.Bad) { throw new Exception("Bad TCP Number Received"); } for (int i = 0; i < tcp.Options.Count; i++) { switch (tcp.Options[i].Code) { case 0: //End case 1: //Nop continue; case 8: //Timestamp LastRecivedTimeStamp = ((TCPopTS)(tcp.Options[i])).SenderTimeStamp; break; default: Console.Error.WriteLine("Got Unknown Option " + tcp.Options[i].Code); throw new Exception(); //break; } } //Next packet will be data state = TCPState.Connected; } return(true); #endregion case TCPState.Connected: #region "Connected" if (tcp.SYN == true) { throw new Exception("Attempt to Connect to an open Port"); } lock (sentry) { for (int i = 0; i < tcp.Options.Count; i++) { switch (tcp.Options[i].Code) { case 0: //End case 1: //Nop continue; case 8: //Console.Error.WriteLine("Got TimeStamp"); LastRecivedTimeStamp = ((TCPopTS)(tcp.Options[i])).SenderTimeStamp; break; default: Console.Error.WriteLine("Got Unknown Option " + tcp.Options[i].Code); throw new Exception(); //break; } } NumCheckResult Result = CheckNumbers(tcp); if (Result == NumCheckResult.GotOldData) { throw new NotImplementedException(); //return true; } if (Result == NumCheckResult.Bad) { throw new Exception("Bad TCP Number Received"); } if (tcp.FIN == true) //Connection Close Part 1, receive FIN from PS2 { PerformCloseByPS2(); return(true); } if (tcp.GetPayload().Length != 0) { ReceivedSequenceNumbers.RemoveAt(0); ReceivedSequenceNumbers.Add(ExpectedSequenceNumber); //Send the Data try { client.GetStream().Write(tcp.GetPayload(), 0, tcp.GetPayload().Length); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("Got IO Error :" + e.ToString()); //Connection Lost //Send Shutdown (Untested) PerformRST(); open = false; return(true); } unchecked { ExpectedSequenceNumber += ((uint)tcp.GetPayload().Length); } //Done send //ACK data TCP ret = CreateBasePacket(); ret.ACK = true; recvbuff.Add(ret); } } return(true); #endregion case TCPState.ConnectionClosedByPS2AndRemote: #region "Closing" //Close Part 4, Recive ACK from PS2 Console.Error.WriteLine("Compleated Close By PS2"); NumCheckResult ResultFIN = CheckNumbers(tcp); if (ResultFIN == NumCheckResult.GotOldData) { return(false); } if (ResultFIN == NumCheckResult.Bad) { throw new Exception("Bad TCP Number Received"); } state = TCPState.Closed; open = false; return(true); #endregion case TCPState.ConnectionClosedByRemote: #region "Closing" //Expect fin+ack if (tcp.FIN == true) { Console.Error.WriteLine("Compleated Close By Remote"); ReceivedSequenceNumbers.RemoveAt(0); ReceivedSequenceNumbers.Add(ExpectedSequenceNumber); unchecked { ExpectedSequenceNumber += 1; } TCP ret = CreateBasePacket(); ret.ACK = true; recvbuff.Add(ret); state = TCPState.ConnectionClosedByPS2AndRemote; open = false; return(true); } //throw new Exception("Invalid Packet"); return(false); //break; #endregion default: throw new Exception("Invalid State"); } }
public TCPConnection() { this.state = TCPClosed.Instance(); }
public void ChangeState(TCPState state) { this.state = state; }
bool Read() { IPPacket ipUp; IPPacket ip; TCPPacket tcp; bool bFinish = false; ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.TCPOptions = new List <byte>(); tcp.TCPOptions.Add(0x02);//段字节数最多为1024:0x4000 tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x00); tcp.DataOffset++; tcp.TCPFlags = 0x02;//同步 IPSender(ip); int err = 0; int retry = 0; while (true) { ipUp = IPReader(); if (ipUp != null) { err = 0; TCPPacket tcpUp = (TCPPacket)ipUp.oProtocolContent; if (tcpUp == null) { continue; } if (tcpUp.DastinationPort != ourPort) { continue; } switch (tcpUp.TCPFlags) { case 0x12: if (tcpState == TCPState.SYN) //之前正在处于请求连接状态 { ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.InitialSeqNumber = tcpUp.AckSeqNumber; tcp.AckSeqNumber = tcpUp.InitialSeqNumber + 1; tcp.TCPFlags = 0x10; IPSender(ip); tcp.TCPFlags = 0x18; //push数据 TCPSeqNum += (uint)TCPTxData.Count; foreach (byte b in TCPTxData) { tcp.Datas.Add(b); } IPSender(ip); TCPData = new TCPDataRecombine(TCPDataType.HTTP); TCPData.InitSeqNum = tcp.AckSeqNumber; tcpState = TCPState.TransData; } else if (tcpState == TCPState.FIN) { ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.InitialSeqNumber = tcpUp.AckSeqNumber; tcp.AckSeqNumber = tcpUp.InitialSeqNumber + 1; tcp.TCPFlags = 0x10; IPSender(ip); ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.TCPOptions = new List <byte>(); tcp.TCPOptions.Add(0x02); //段字节数最多为1024:0x4000 tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x00); tcp.DataOffset++; tcp.TCPFlags = 0x02; //同步 IPSender(ip); tcpState = TCPState.SYN; } else if (tcpState == TCPState.TransData) { ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.InitialSeqNumber = tcpUp.AckSeqNumber; tcp.AckSeqNumber = tcpUp.InitialSeqNumber + 1; tcp.TCPFlags = 0x10; IPSender(ip); } else { ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.InitialSeqNumber = tcpUp.AckSeqNumber; tcp.AckSeqNumber = tcpUp.InitialSeqNumber + 1; tcp.TCPFlags = 0x14; IPSender(ip); ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.TCPOptions = new List <byte>(); tcp.TCPOptions.Add(0x02); //段字节数最多为1024:0x4000 tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x00); tcp.DataOffset++; tcp.TCPFlags = 0x02; //同步 IPSender(ip); Debug.WriteLine(Com.PortName + "TCP未知命令交互:" + tcpState.ToString() + " " + tcpUp.TCPFlags.ToString("X2")); } break; case 0x10: case 0x18: if (tcpState == TCPState.WaitFIN) { OnIPProcLog("成功关闭连接!", (int)ATCommandLogType.Rx); tcpState = TCPState.WaitFIN1; return(true); } else if (tcpState == TCPState.TransData) { if (tcpUp.Datas.Count != 0) { int rxState = TCPData.bFinish(tcpUp.InitialSeqNumber, tcpUp.Datas, finishper); if (finishper < 100) { switch (TCPData.datatype) { case TCPDataType.HTTP: Http http = (Http)TCPData.oTCPDatas; OnIPFinishPer(http.per.ToString()); break; default: break; } } if (rxState == (int)HttpRxState.Finish) //数据接收完成 { tcp.TCPFlags = 0x11; TCPSeqNum++; tcpState = TCPState.WaitFIN; IPSender(ip); bFinish = true; OnIPProcLog("数据接收完成!", (int)ATCommandLogType.Rx); } else if (rxState == (int)HttpRxState.LengthOut) { ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.TCPOptions = new List <byte>(); tcp.TCPOptions.Add(0x02); //段字节数最多为1024:0x4000 tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x00); tcp.DataOffset++; tcp.TCPFlags = 0x02; //同步 IPSender(ip); tcpState = TCPState.SYN; } else { Debug.WriteLine(Com.PortName + "还需要的数据量为:" + rxState.ToString()); ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.InitialSeqNumber = tcpUp.AckSeqNumber; tcp.AckSeqNumber = tcpUp.InitialSeqNumber + (uint)tcpUp.Datas.Count; tcp.TCPFlags = 0x10; IPSender(ip); } } } else { // ip = NewIPPacket(); // tcp = (TCPPacket)ip.oProtocolContent; // tcp.InitialSeqNumber = tcpUp.AckSeqNumber; // tcp.AckSeqNumber = tcpUp.InitialSeqNumber + 1; // tcp.TCPFlags = 0x14; // IPSender(ip); ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.TCPOptions = new List <byte>(); tcp.TCPOptions.Add(0x02); //段字节数最多为1024:0x4000 tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x00); tcp.DataOffset++; tcp.TCPFlags = 0x02; //同步 IPSender(ip); // Debug.WriteLine(Com.PortName + "TCP未知命令交互:" + tcpState.ToString() + " " + tcpUp.TCPFlags.ToString("X2")); Debug.WriteLine(Com.PortName + "TCP未知命令交互:" + tcpState.ToString() + " " + tcpUp.TCPFlags.ToString("X2")); } break; case 0x11: if (tcpState == TCPState.WaitFIN) { ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.InitialSeqNumber = tcpUp.AckSeqNumber; tcp.AckSeqNumber = tcpUp.InitialSeqNumber + 1; tcp.TCPFlags = 0x10; IPSender(ip); OnIPProcLog("成功关闭连接!11111111111111111111", (int)ATCommandLogType.Rx); return(true); } else { ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.InitialSeqNumber = tcpUp.AckSeqNumber; tcp.AckSeqNumber = tcpUp.InitialSeqNumber + 1; tcp.TCPFlags = 0x10; IPSender(ip); ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.TCPOptions = new List <byte>(); tcp.TCPOptions.Add(0x02); //段字节数最多为1024:0x4000 tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x00); tcp.DataOffset++; tcp.TCPFlags = 0x02; //同步 IPSender(ip); tcpState = TCPState.SYN; } break; case 0x04: if (tcpState == TCPState.WaitFIN || tcpState == TCPState.WaitFIN1) { // ip = NewIPPacket(); // tcp = (TCPPacket)ip.oProtocolContent; // tcp.InitialSeqNumber = tcpUp.AckSeqNumber; // tcp.AckSeqNumber = tcpUp.InitialSeqNumber; // tcp.TCPFlags = 0x10; // IPSender(ip); // tcp.TCPFlags = 0x11; // TCPSeqNum++; // tcpState = TCPState.WaitFIN; // IPSender(ip); OnIPProcLog("彻底关闭连接", (int)ATCommandLogType.Rx); return(true); } else { Debug.WriteLine(Com.PortName + "TCP未知命令交互:" + tcpState.ToString() + " " + tcpUp.TCPFlags.ToString("X2")); // if(tcpState==TCPState.SYN) // { // ip = NewIPPacket(); // tcp = (TCPPacket)ip.oProtocolContent; // tcp.TCPOptions = new List<byte>(); // tcp.TCPOptions.Add(0x02);//段字节数最多为1024:0x4000 // tcp.TCPOptions.Add(0x04); // tcp.TCPOptions.Add(0x04); // tcp.TCPOptions.Add(0x00); // tcp.DataOffset++; // tcp.TCPFlags = 0x02;//同步 // IPSender(ip); // tcpState = TCPState.SYN; // } } break; case 0x14: if (tcpState == TCPState.WaitFIN || tcpState == TCPState.WaitFIN1) { OnIPProcLog("彻底关闭连接", (int)ATCommandLogType.Rx); ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.InitialSeqNumber = tcpUp.AckSeqNumber; tcp.AckSeqNumber = tcpUp.InitialSeqNumber; tcp.TCPFlags = 0x10; IPSender(ip); return(true); } else { Debug.WriteLine(Com.PortName + "TCP未知命令交互:" + tcpState.ToString() + " " + tcpUp.TCPFlags.ToString("X2")); // if (tcpState == TCPState.SYN) // { // ip = NewIPPacket(); // tcp = (TCPPacket)ip.oProtocolContent; // tcp.TCPOptions = new List<byte>(); // tcp.TCPOptions.Add(0x02);//段字节数最多为1024:0x4000 // tcp.TCPOptions.Add(0x04); // tcp.TCPOptions.Add(0x04); // tcp.TCPOptions.Add(0x00); // tcp.DataOffset++; // tcp.TCPFlags = 0x02;//同步 // IPSender(ip); // tcpState = TCPState.SYN; // } // ip = NewIPPacket(); // tcp = (TCPPacket)ip.oProtocolContent; // tcp.InitialSeqNumber = tcpUp.AckSeqNumber; // tcp.AckSeqNumber = tcpUp.InitialSeqNumber; // tcp.TCPFlags = 0x10; // IPSender(ip); // tcpState = TCPState.SYN; } break; case 0x01: if (tcpState == TCPState.WaitFIN1) { ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.InitialSeqNumber = tcpUp.AckSeqNumber; tcp.AckSeqNumber = tcpUp.InitialSeqNumber + 1; tcp.TCPFlags = 0x10; IPSender(ip); OnIPProcLog("成功关闭连接!11111111111111111111", (int)ATCommandLogType.Rx); return(true); } else { break; } } } else { err++; if (err > 30) { if (bFinish) { return(true); } bFinish = false; retry++; if (retry > 12) { break; } OnIPProcLog("TCP超时重试" + (retry + 1).ToString(), (int)ATCommandLogType.TCPTimeOut); err = 0; ip = NewIPPacket(); tcp = (TCPPacket)ip.oProtocolContent; tcp.TCPOptions = new List <byte>(); tcp.TCPOptions.Add(0x02);//段字节数最多为1024:0x4000 tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x04); tcp.TCPOptions.Add(0x00); tcp.DataOffset++; tcp.TCPFlags = 0x02;//同步 IPSender(ip); tcpState = TCPState.SYN; } } } return(false); }