public static async void ListenIpAndPort(string hostIP, int tcpPort, DealWith dealWith) { Int32 port = tcpPort; IPAddress localAddr = IPAddress.Parse(hostIP); var server = new TcpListener(localAddr, port); server.Start(); while (true) { Console.Write("Waiting for a connection... "); string notifyJson; TcpClient client = server.AcceptTcpClient(); { Console.WriteLine("Connected!"); // bool isRight; NetworkStream ns = client.GetStream(); GetMsg01(client, ns, out notifyJson); var outPut = await dealWith(notifyJson); GetMsg02(client, ns, outPut); ns.Close(4000); } client.Close(); } }
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); } } }
static void SetMsgAndIsRightF(string notifyJson, bool isRight, DealWith dealWith) { Console.WriteLine($"notify receive:{notifyJson}"); if (isRight) { Task.Run(() => dealWith(notifyJson)); } }
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); }
public static async void ListenIpAndPort(string hostIP, int tcpPort, DealWith dealWith) { Int32 port = tcpPort; IPAddress localAddr = IPAddress.Parse(hostIP); var server = new TcpListener(localAddr, port); server.Start(); while (true) { Console.Write("Waiting for a connection... "); string notifyJson; TcpClient client = server.AcceptTcpClient(); { Console.WriteLine("Connected!"); // bool isRight; NetworkStream ns = client.GetStream(); // using (NetworkStream stream = client.GetStream()) { bool doNext; GetMsg01(client, ns, out notifyJson, out doNext); if (doNext) { ns.Close(2000); client.Close(); continue; } else { var outPut = await dealWith(notifyJson); GetMsg02(client, ns, outPut, out doNext); if (doNext) { ns.Close(2000); client.Close(); continue; } } // Common.CheckBeforeReadReason reason; } ns.Close(2000); } client.Close(); } }
static void SetMsgAndIsRightF(string notifyJson, DealWith dealWith) { Console.WriteLine($"notify receive:{notifyJson}"); dealWith(notifyJson); }