private void DoClientThread() { string receivedText = ""; try { _client.Send(Encoding.ASCII.GetBytes(TextForSend)); PostLog("Sent."); if (_receivingData == true) { byte[] bytes = new byte[1024 * 16]; int length = 0; if ((length = _client.Receive(bytes)) == 0) { PostLog("Haven't received any data."); } else { receivedText = System.Text.Encoding.ASCII.GetString(bytes, 0, length); PostLog("Received \"" + receivedText + "\".", Brushes.Blue); } } Dispatcher.Invoke((ThreadStart) delegate() { SentReceivedOc.Add(new SentReceivedItem() { Index = SentReceivedOc.Count.ToString(), Sent = TextForSend, Received = receivedText }); }, null); } catch (Exception ex) { PostLog("communication error : " + ex.Message); } InRun = false; }
private void DoClientThread(int indexThread = -1) { int count = int.Parse(ServerRepeat); string receivedText = ""; Socket client = null; string indexThreadDisplay = ""; if (indexThread != -1) { indexThreadDisplay = "Thread " + indexThread.ToString() + " : "; } try { client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress local = IPAddress.Parse(ServerIPAddress); IPEndPoint iep = new IPEndPoint(local, 5080); client.ReceiveTimeout = 60000; if (count == 1) { PostLog(indexThreadDisplay + "Trying to connect..."); } else { PostLog(indexThreadDisplay + count.ToString() + " : Trying to connect..."); } client.Connect(iep); if (client.Connected) { for (int index = 0; index < count; index++) { if (count == 1) { PostLog(indexThreadDisplay + "Connected."); PostLog(indexThreadDisplay + "Trying to send \"" + TextForSend + "\"..."); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Connected."); PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Trying to send \"" + TextForSend + "-" + indexThreadDisplay + count.ToString() + " : " + index.ToString() + "\"..."); } client.Send(Encoding.ASCII.GetBytes(TextForSend + "-" + indexThreadDisplay + count.ToString() + " : " + index.ToString())); if (count == 1) { PostLog(indexThreadDisplay + "Sent."); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Sent."); } byte[] bytes = new byte[1024 * 16]; int length = 0; if (count == 1) { PostLog(indexThreadDisplay + "Trying to receive ..."); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Trying to receive ..."); } if ((length = client.Receive(bytes)) == 0) { if (count == 1) { PostLog(indexThreadDisplay + "Haven't received any data."); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Haven't received any data."); } } else { receivedText = System.Text.Encoding.ASCII.GetString(bytes, 0, length); if (count == 1) { PostLog(indexThreadDisplay + "Received \"" + receivedText + "\".", Brushes.Blue); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Received \"" + receivedText + "\".", Brushes.Blue); } } if (indexThread == -1) { Dispatcher.Invoke((ThreadStart) delegate() { SentReceivedOc.Add(new SentReceivedItem() { Index = (SentReceivedOc.Count + 1).ToString(), Sent = TextForSend + "-" + indexThreadDisplay + count.ToString() + " : " + index.ToString(), Received = receivedText }); }, null); } else { Dispatcher.BeginInvoke((ThreadStart) delegate() { SentReceivedOc.Add(new SentReceivedItem() { Index = (SentReceivedOc.Count + 1).ToString(), Sent = TextForSend + "-" + indexThreadDisplay + count.ToString() + " : " + index.ToString(), Received = receivedText }); }, null); } } client.Disconnect(false); } client.Close(); client.Dispose(); } catch (Exception ex) { if (count == 1) { PostLog(indexThreadDisplay + "Session error : " + ex.Message); } else { PostLog(indexThreadDisplay + count.ToString() + " : Session error : " + ex.Message); } if (client != null) { try { client.Disconnect(false); } catch (Exception) { } try { client.Close(); } catch (Exception) { } try { client.Dispose(); } catch (Exception) { } } } if (count == 1) { PostLog(indexThreadDisplay + "Session finished."); } else { PostLog(indexThreadDisplay + count.ToString() + " : Session finished."); } if (indexThread == -1) { InRun = false; } }
private void DoClientThread(int indexThread = -1) { int count = int.Parse(ServerRepeat); string receivedText = ""; Socket client = null; string indexThreadDisplay = ""; if (indexThread != -1) { indexThreadDisplay = "Thread " + indexThread.ToString() + " : "; } try { client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress local = IPAddress.Parse(ServerIPAddress); int port = 5080; if (int.TryParse(ServerPort, out port) == false) { port = 5080; } IPEndPoint iep = new IPEndPoint(local, port); client.ReceiveTimeout = 60000; if (count == 1) { PostLog(indexThreadDisplay + "Trying to connect..."); } else { PostLog(indexThreadDisplay + count.ToString() + " : Trying to connect..."); } client.Connect(iep); if (client.Connected) { for (int index = 0; index < count; index++) { if (count == 1) { PostLog(indexThreadDisplay + "Connected."); PostLog(indexThreadDisplay + "Trying to send \"" + TextForSend + "\"..."); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Connected."); PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Trying to send \"" + TextForSend + "-" + indexThreadDisplay + count.ToString() + " : " + index.ToString() + "\"..."); } client.Send(Encoding.ASCII.GetBytes(TextForSend + "-" + indexThreadDisplay + count.ToString() + " : " + index.ToString())); if (count == 1) { PostLog(indexThreadDisplay + "Sent."); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Sent."); } if (_receivingData == true) { byte[] bytes = new byte[1024 * 16]; int length = 0; if (count == 1) { PostLog(indexThreadDisplay + "Trying to receive ..."); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Trying to receive ..."); } if ((length = client.Receive(bytes)) == 0) { if (count == 1) { PostLog(indexThreadDisplay + "Haven't received any data."); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Haven't received any data."); } } else { receivedText = System.Text.Encoding.ASCII.GetString(bytes, 0, length); if (count == 1) { PostLog(indexThreadDisplay + "Received \"" + receivedText + "\".", Brushes.Blue); } else { PostLog(indexThreadDisplay + count.ToString() + " : " + index.ToString() + " : Received \"" + receivedText + "\".", Brushes.Blue); } } } bool isCorrectData = false; if (receivedText.EndsWith(TextForSend + "-" + indexThreadDisplay + count.ToString() + " : " + index.ToString())) { isCorrectData = true; PassCount++; } else { FailCount++; } //if (indexThread == -1) //{ // Dispatcher.Invoke((ThreadStart)delegate() // { // SentReceivedOc.Add(new SentReceivedItem() // { // Index = SentReceivedOc.Count.ToString(), // Sent = TextForSend + "-" + indexThreadDisplay + count.ToString() + " : " + index.ToString(), // Received = receivedText, // DataCorrect = (isCorrectData) ? SentReceivedItem.DataCorrectEnum.Ok : SentReceivedItem.DataCorrectEnum.Error // }); // }, null); //} //else { Dispatcher.Invoke((ThreadStart) delegate() { SentReceivedOc.Add(new SentReceivedItem() { Index = SentReceivedOc.Count.ToString(), Sent = TextForSend + "-" + indexThreadDisplay + count.ToString() + " : " + index.ToString(), Received = receivedText, DataCorrect = (isCorrectData) ? SentReceivedItem.DataCorrectEnum.Ok : SentReceivedItem.DataCorrectEnum.Error }); }, null); } if (ServerRepeat != "1") { PostLog((indexThreadDisplay + " Sleep " + MsgIntervalMs + "ms").Trim()); Thread.Sleep(MsgIntervalMs); } } PostLog(indexThreadDisplay + "Trying to disconnecting ..."); client.Disconnect(false); PostLog(indexThreadDisplay + "Disconnected."); } PostLog(indexThreadDisplay + "Trying to closing ..."); client.Close(); PostLog(indexThreadDisplay + "Closed."); PostLog(indexThreadDisplay + "Trying to disposing ..."); client.Dispose(); PostLog(indexThreadDisplay + "Disposed."); } catch (Exception ex) { if (count == 1) { PostLog(indexThreadDisplay + "Session error : " + ex.Message); } else { PostLog(indexThreadDisplay + count.ToString() + " : Session error : " + ex.Message); } if (client != null) { try { client.Disconnect(false); } catch (Exception) { } try { client.Close(); } catch (Exception) { } try { client.Dispose(); } catch (Exception) { } } } if (count == 1) { PostLog(indexThreadDisplay + "Session finished."); } else { PostLog(indexThreadDisplay + count.ToString() + " : Session finished."); } if (indexThread == -1) { InRun = false; } }
private void HttpRequestThread(object obj) { HttpListenerContext ctx = obj as HttpListenerContext; HttpListenerRequest request = ctx.Request; string backData = ""; if (request.HasEntityBody) { using (System.IO.Stream body = request.InputStream) { using (System.IO.StreamReader reader = new System.IO.StreamReader(body, request.ContentEncoding)) { backData = reader.ReadToEnd(); } } } else { PostLog("No body.", Brushes.Red); } string inData = backData; if (string.IsNullOrWhiteSpace(backData)) { backData = "No data."; } else { PostLog("Body : " + backData, Brushes.Blue); } HttpListenerResponse response = ctx.Response; backData = backData + " : " + DateTime.Now.ToLongTimeString(); string responseString = "<HTML><BODY>" + backData + "</BODY></HTML>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; PostLog("Trying to send response..."); output.Write(buffer, 0, buffer.Length); PostLog("Sent."); Dispatcher.Invoke((ThreadStart) delegate() { lock (ObjLock) { SentReceivedOc.Add(new SentReceivedItem() { Index = (SentReceivedOc.Count + 1).ToString(), ClientIP = inData, Received = backData }); } }, null); PostLog("Trying to close response..."); output.Close(); PostLog("Closed."); }