public static void startTcp(string ip, int port, DealWith dealWith) { // throw new NotImplementedException(); // Int32 port = port; IPAddress localAddr = IPAddress.Parse(ip); var server = new TcpListener(localAddr, port); server.Start(); //AutoResetEvent allDone = new AutoResetEvent(false); while (true) { Console.Write("Waiting for a connection... "); //string notifyJson; // bool isRight; try { TcpClient client = server.AcceptTcpClient(); { Console.WriteLine("Connected!"); SetMsgAndIsRight smr = new SetMsgAndIsRight(SetMsgAndIsRightF); GetMsg(client, smr, dealWith); } client.Close(); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } } }
private static void GetMsg(TcpClient client, SetMsgAndIsRight smr, DealWith dealWith, out bool doNext) { NetworkStream stream = client.GetStream(); { // do { Common.CheckBeforeReadReason reason; var length = Common.ReceiveLength(stream, out reason); if (reason == Common.CheckBeforeReadReason.Ok) { } else { stream.Close(); doNext = true; return; } Common.SendLength(length, stream); byte[] bytes = new byte[length]; Common.CheckBeforeRead(stream, out reason); if (reason == Common.CheckBeforeReadReason.Ok) { } else { stream.Close(); doNext = true; return; } int bytesRead = stream.Read(bytes, 0, length); // Console.WriteLine($"receive:{returnResult.result}"); var notifyJson = Encoding.UTF8.GetString(bytes, 0, bytesRead); var md5Respon = CommonClass.Random.GetMD5HashByteFromBytes(bytes); stream.Write(md5Respon, 0, 16); var isRight = Common.ReveiveRight(stream); if (isRight) { Common.SendEnd(stream); } else { stream.Close(); doNext = true; return; } doNext = false; smr(notifyJson, isRight, dealWith); stream.Close(2000); } } }
private static void GetMsg(TcpClient client, SetMsgAndIsRight smr, DealWith dealWith) { NetworkStream stream = client.GetStream(); var length = Common.ReceiveLength(stream); Common.SendLength(length, stream); byte[] bytes = new byte[length]; int bytesRead = stream.Read(bytes, 0, length); var notifyJson = Encoding.UTF8.GetString(bytes, 0, bytesRead); smr(notifyJson, dealWith); stream.Close(4000); }