private void ConnectionHandler(StateSaverClass stateSaver)
        {
            RequestBuilderClass reqBuilder = new RequestBuilderClass();

            byte[] msg;
            try
            {
                while (stateSaver.sentCounter < Convert.ToInt32(stateSaver.numMessages))
                {
                    msg = reqBuilder.BuildRequest(stateSaver);
                    stateSaver.sock.Send(msg);

                    stateSaver.lb.sentMessages.Add(msg);

                    Thread.Sleep(Convert.ToInt32(stateSaver.pace));
                    stateSaver.sentCounter++;
                }

                //System.Windows.Forms.MessageBox.Show("Finished Sending");
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }
        }
Example #2
0
        public byte[] BuildRequest(StateSaverClass stateSaver)
        {
            byte[]        msg = null;
            StringBuilder sb  = new StringBuilder();

            sb.Append("REQ|");
            sb.Append(stateSaver.stpWatch.ElapsedMilliseconds.ToString());
            sb.Append("|RequestNo: ");
            sb.Append(stateSaver.sentCounter.ToString());
            sb.Append("|WurdingerO|19-3410|0|");
            sb.Append(Dns.GetHostAddresses(Dns.GetHostName()).Where(
                          address => address.AddressFamily ==
                          AddressFamily.InterNetwork).First().ToString());
            sb.Append("|2605|");
            sb.Append(stateSaver.sock.Handle.ToString());
            sb.Append("|");
            sb.Append(stateSaver.serverIP);
            sb.Append("|2605|StudentData: ");
            sb.Append(stateSaver.sentCounter.ToString());
            sb.Append("|1|");

            msg = SetSize(sb.ToString());


            //string msgString = "REQ|" + msTime
            //    + "|RequestNo:" + i + "|WurdingerO|19-3410|" + "0|" + ip.ToString()
            //    + "|" + portNum + "|" + sock.Handle.ToString() + "|" + serverIP + "|"
            //    + serverPort + "|StudentData:" + i + "|1|";

            return(msg);
        }
        /// <summary>
        /// Begins the client send receive thread functions
        /// </summary>
        /// <param name="stateSaver">
        /// StateSaver class object
        /// </param>
        internal void Begin(StateSaverClass stateSaver)
        {
            //System.Windows.Forms.MessageBox.Show("Sending and receiving beginning");
            try
            {
                stateSaver.stpWatch.Start();

                Thread receiveThread = new Thread(delegate()
                {
                    ReceivingHandler(stateSaver);
                });

                receiveThread.Start();

                Thread sendThread = new Thread(delegate()
                {
                    ConnectionHandler(stateSaver);
                });

                sendThread.Start();

                //System.Windows.Forms.MessageBox.Show("Waiting on join");
                sendThread.Join();
                receiveThread.Join();
                stateSaver.sock.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
                stateSaver.sock.Shutdown(System.Net.Sockets.SocketShutdown.Send);
                //System.Windows.Forms.MessageBox.Show("Joined threads");
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }

            stateSaver.lb.PrintLogs(stateSaver);
        }
        public byte[] BuildRequest(StateSaverClass stateSaver)
        {
            byte[] msg = null;
            StringBuilder sb = new StringBuilder();

            sb.Append("REQ|");
            sb.Append(stateSaver.stpWatch.ElapsedMilliseconds.ToString());
            sb.Append("|RequestNo: ");
            sb.Append(stateSaver.sentCounter.ToString());
            sb.Append("|WurdingerO|19-3410|0|");
            sb.Append(Dns.GetHostAddresses(Dns.GetHostName()).Where(
                address => address.AddressFamily ==
                    AddressFamily.InterNetwork).First().ToString());
            sb.Append("|2605|");
            sb.Append(stateSaver.sock.Handle.ToString());
            sb.Append("|");
            sb.Append(stateSaver.serverIP);
            sb.Append("|2605|StudentData: ");
            sb.Append(stateSaver.sentCounter.ToString());
            sb.Append("|1|");

            msg = SetSize(sb.ToString());

            //string msgString = "REQ|" + msTime
            //    + "|RequestNo:" + i + "|WurdingerO|19-3410|" + "0|" + ip.ToString()
            //    + "|" + portNum + "|" + sock.Handle.ToString() + "|" + serverIP + "|"
            //    + serverPort + "|StudentData:" + i + "|1|";

            return msg;
        }
        /// <summary>
        /// Begins the client send receive thread functions
        /// </summary>
        /// <param name="stateSaver">
        /// StateSaver class object
        /// </param>
        internal void Begin(StateSaverClass stateSaver)
        {
            //System.Windows.Forms.MessageBox.Show("Sending and receiving beginning");
            try
            {
                stateSaver.stpWatch.Start();

                Thread receiveThread = new Thread(delegate()
                    {
                        ReceivingHandler(stateSaver);
                    });

                receiveThread.Start();

                Thread sendThread = new Thread(delegate()
                    {
                        ConnectionHandler(stateSaver);
                    });

                sendThread.Start();

                //System.Windows.Forms.MessageBox.Show("Waiting on join");
                sendThread.Join();
                receiveThread.Join();
                stateSaver.sock.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
                stateSaver.sock.Shutdown(System.Net.Sockets.SocketShutdown.Send);
                //System.Windows.Forms.MessageBox.Show("Joined threads");
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }

            stateSaver.lb.PrintLogs(stateSaver);
        }
Example #6
0
        private void startButton_Click(object sender, EventArgs e)
        {
            Classes.ConnectClass connClass = new Classes.ConnectClass();
            Classes.SendReceiveClass sendClass = new Classes.SendReceiveClass();
            Classes.StateSaverClass stateSaver = new Classes.StateSaverClass();
            stateSaver.serverIP = ipTextbox.Text;
            stateSaver.numMessages = msgCountTextbox.Text;
            stateSaver.pace = paceTextbox.Text;
            stateSaver.path += logNameTextbox.Text;

            stateSaver.sock = connClass.Connect(ipTextbox.Text);
            //stateSaver.sock.ReceiveTimeout = 4000;
            sendClass.Begin(stateSaver);
        }
Example #7
0
        private void startButton_Click(object sender, EventArgs e)
        {
            Classes.ConnectClass     connClass  = new Classes.ConnectClass();
            Classes.SendReceiveClass sendClass  = new Classes.SendReceiveClass();
            Classes.StateSaverClass  stateSaver = new Classes.StateSaverClass();
            stateSaver.serverIP    = ipTextbox.Text;
            stateSaver.numMessages = msgCountTextbox.Text;
            stateSaver.pace        = paceTextbox.Text;
            stateSaver.path       += logNameTextbox.Text;

            stateSaver.sock = connClass.Connect(ipTextbox.Text);
            //stateSaver.sock.ReceiveTimeout = 4000;
            sendClass.Begin(stateSaver);
        }
        private void ConnectionHandler(StateSaverClass stateSaver)
        {
            RequestBuilderClass reqBuilder = new RequestBuilderClass();
            byte[] msg;
            try
            {
                while (stateSaver.sentCounter < Convert.ToInt32(stateSaver.numMessages))
                {
                    msg = reqBuilder.BuildRequest(stateSaver);
                    stateSaver.sock.Send(msg);

                    stateSaver.lb.sentMessages.Add(msg);

                    Thread.Sleep(Convert.ToInt32(stateSaver.pace));
                    stateSaver.sentCounter++;
                }

                //System.Windows.Forms.MessageBox.Show("Finished Sending");
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }
        }
        private void ReceivingHandler(StateSaverClass stateSaver)
        {
            byte[] buffer   = new byte[BUFFER_SIZE];
            byte[] byteSize = new byte[LENGTH_BITS];
            int    testsize = 0;

            while (stateSaver.receivecounter < Convert.ToInt16(stateSaver.numMessages))
            {
                try
                {
                    int    offset    = 0;
                    int    size      = 0;
                    int    bytesRead = 0;
                    byte[] receivedMsg;

                    while (bytesRead < 2)
                    {
                        bytesRead = stateSaver.sock.Receive(buffer, offset,
                                                            LENGTH_BITS, System.Net.Sockets.SocketFlags.None);
                    }

                    Array.Copy(buffer, offset, byteSize, 0, LENGTH_BITS);

                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(byteSize);
                    }

                    size = BitConverter.ToInt16(byteSize, 0);


                    testsize = size;


                    offset += LENGTH_BITS;

                    while (bytesRead < size)
                    {
                        bytesRead = stateSaver.sock.Receive(buffer, offset,
                                                            size, System.Net.Sockets.SocketFlags.None);
                    }

                    receivedMsg = new byte[bytesRead];
                    Array.Copy(buffer, offset, receivedMsg, 0, bytesRead);

                    stateSaver.lb.receivedMessages.Add(receivedMsg);
                    //System.Windows.Forms.MessageBox.Show(stateSaver.receivecounter.ToString() + " recieved messages");
                    stateSaver.receivecounter++;
                }
                catch (Exception e)
                {
                    stateSaver.receivecounter++;
                    testsize.ToString();
                    buffer.ToString();
                    e.Message.ToString();
                    //System.Windows.Forms.MessageBox.Show("SendReceiveClass.ReceivingHandler\r\n" + e.Message.ToString());
                }
            }

            //System.Windows.Forms.MessageBox.Show("Finished Receiving");
        }
Example #10
0
        public void PrintLogs(StateSaverClass stateSaver)
        {
            //string path = @"C:\Logs\ClientLog.txt";
            System.IO.StreamWriter sw = new System.IO.StreamWriter(stateSaver.path, true);
            string[] recievedStrings  = new string[receivedMessages.Count];
            string[] sentStrings      = new string [sentMessages.Count];
            //System.IO.StreamWriter sw = System.IO.File.AppendText(path);

            if (receivedMessages.Count == sentMessages.Count)
            {
                for (int i = 0; i < receivedMessages.Count; i++)
                {
                    string recStr     = System.Text.Encoding.ASCII.GetString(receivedMessages[i]);
                    byte[] choppedRec = new byte[sentMessages[i].Length - 2];
                    Array.Copy(sentMessages[i], 2, choppedRec, 0, choppedRec.Length);

                    string sentStr = System.Text.Encoding.ASCII.GetString(choppedRec);

                    //string sentStr = System.Text.Encoding.ASCII.GetString(sentMessages[i]);

                    sw.Write(sentStr + "\n\r");
                    sw.Write(recStr + "\n\r");
                }
            }
            else
            {
                int count = receivedMessages.Count;
                int flag  = RECEIVE_FLAG;
                if (count > sentMessages.Count)
                {
                    count = sentMessages.Count;
                    flag  = SEND_FLAG;
                }

                for (int i = 0; i < count; i++)
                {
                    string recStr     = System.Text.Encoding.ASCII.GetString(receivedMessages[i]);
                    byte[] choppedRec = new byte[sentMessages[i].Length - 2];
                    Array.Copy(sentMessages[i], 2, choppedRec, 0, choppedRec.Length);

                    string sentStr = System.Text.Encoding.ASCII.GetString(choppedRec);

                    //string sentStr = System.Text.Encoding.ASCII.GetString(sentMessages[i]);

                    sw.Write(sentStr + "\n\r");
                    sw.Write(recStr + "\n\r");
                }

                switch (flag)
                {
                case RECEIVE_FLAG:
                    for (int i = count; i < receivedMessages.Count; i++)
                    {
                        string recStr     = System.Text.Encoding.ASCII.GetString(receivedMessages[i]);
                        byte[] choppedRec = new byte[receivedMessages[i].Length - 2];
                        Array.Copy(receivedMessages[i], 2, choppedRec, 0, choppedRec.Length);
                        sw.Write(recStr + "\n\r");
                    }
                    break;

                case SEND_FLAG:
                    for (int i = count; i < sentMessages.Count; i++)
                    {
                        string sentStr    = System.Text.Encoding.ASCII.GetString(sentMessages[i]);
                        byte[] choppedRec = new byte[sentMessages[i].Length - 2];
                        Array.Copy(sentMessages[i], 2, choppedRec, 0, choppedRec.Length);
                        sw.Write(sentStr + "\n\r");
                    }
                    break;
                }
            }

            // Trailer Record:
            // MMDDYYYY | HHMMSS | 0 | 0 | 0
            StringBuilder trailerSB = new StringBuilder();

            trailerSB.Append(System.DateTime.Now.ToString("MMddyyyy"));
            trailerSB.Append("|");
            trailerSB.Append(System.DateTime.Now.ToString("HHmmss"));
            trailerSB.Append("|0|0|0");
            sw.Write(trailerSB.ToString());

            sw.Close();
            System.Windows.Forms.MessageBox.Show("Finished logging Received Count: " + receivedMessages.Count.ToString() + " Sent Count: " + sentMessages.Count.ToString());
        }
Example #11
0
        public void PrintLogs(StateSaverClass stateSaver)
        {
            //string path = @"C:\Logs\ClientLog.txt";
            System.IO.StreamWriter sw = new System.IO.StreamWriter(stateSaver.path, true);
            string[] recievedStrings = new string[receivedMessages.Count];
            string[] sentStrings = new string [sentMessages.Count];
            //System.IO.StreamWriter sw = System.IO.File.AppendText(path);

            if (receivedMessages.Count == sentMessages.Count)
            {
                for (int i = 0; i < receivedMessages.Count; i++)
                {
                    string recStr = System.Text.Encoding.ASCII.GetString(receivedMessages[i]);
                    byte[] choppedRec = new byte[sentMessages[i].Length - 2];
                    Array.Copy(sentMessages[i], 2, choppedRec, 0, choppedRec.Length);

                    string sentStr = System.Text.Encoding.ASCII.GetString(choppedRec);

                    //string sentStr = System.Text.Encoding.ASCII.GetString(sentMessages[i]);

                    sw.Write(sentStr + "\n\r");
                    sw.Write(recStr + "\n\r");
                }

            }
            else
            {
                int count = receivedMessages.Count;
                int flag = RECEIVE_FLAG;
                if (count > sentMessages.Count)
                {
                    count = sentMessages.Count;
                    flag = SEND_FLAG;
                }

                for (int i = 0; i < count; i++)
                {
                    string recStr = System.Text.Encoding.ASCII.GetString(receivedMessages[i]);
                    byte[] choppedRec = new byte[sentMessages[i].Length - 2];
                    Array.Copy(sentMessages[i], 2, choppedRec, 0, choppedRec.Length);

                    string sentStr = System.Text.Encoding.ASCII.GetString(choppedRec);

                    //string sentStr = System.Text.Encoding.ASCII.GetString(sentMessages[i]);

                    sw.Write(sentStr + "\n\r");
                    sw.Write(recStr + "\n\r");

                }

                switch (flag)
                {
                    case RECEIVE_FLAG:
                        for (int i = count; i < receivedMessages.Count; i++)
                        {
                            string recStr = System.Text.Encoding.ASCII.GetString(receivedMessages[i]);
                            byte[] choppedRec = new byte[receivedMessages[i].Length - 2];
                            Array.Copy(receivedMessages[i], 2, choppedRec, 0, choppedRec.Length);
                            sw.Write(recStr + "\n\r");
                        }
                        break;

                    case SEND_FLAG:
                        for (int i = count; i < sentMessages.Count; i++)
                        {
                            string sentStr = System.Text.Encoding.ASCII.GetString(sentMessages[i]);
                            byte[] choppedRec = new byte[sentMessages[i].Length - 2];
                            Array.Copy(sentMessages[i], 2, choppedRec, 0, choppedRec.Length);
                            sw.Write(sentStr + "\n\r");
                        }
                        break;
                }

            }

            // Trailer Record:
            // MMDDYYYY | HHMMSS | 0 | 0 | 0
            StringBuilder trailerSB = new StringBuilder();
            trailerSB.Append(System.DateTime.Now.ToString("MMddyyyy"));
            trailerSB.Append("|");
            trailerSB.Append(System.DateTime.Now.ToString("HHmmss"));
            trailerSB.Append("|0|0|0");
            sw.Write(trailerSB.ToString());

            sw.Close();
            System.Windows.Forms.MessageBox.Show("Finished logging Received Count: " + receivedMessages.Count.ToString() + " Sent Count: " + sentMessages.Count.ToString());
        }
        private void ReceivingHandler(StateSaverClass stateSaver)
        {
            byte[] buffer = new byte[BUFFER_SIZE];
            byte[] byteSize = new byte[LENGTH_BITS];
            int testsize = 0;

                while (stateSaver.receivecounter < Convert.ToInt16(stateSaver.numMessages))
                {
                    try
                    {
                        int offset = 0;
                        int size = 0;
                        int bytesRead = 0;
                        byte[] receivedMsg;

                        while (bytesRead < 2)
                        {
                            bytesRead = stateSaver.sock.Receive(buffer, offset,
                                LENGTH_BITS, System.Net.Sockets.SocketFlags.None);
                        }

                        Array.Copy(buffer, offset, byteSize, 0, LENGTH_BITS);

                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(byteSize);
                        }

                        size = BitConverter.ToInt16(byteSize, 0);

                        testsize = size;

                        offset += LENGTH_BITS;

                        while (bytesRead < size)
                        {
                            bytesRead = stateSaver.sock.Receive(buffer, offset,
                                size, System.Net.Sockets.SocketFlags.None);
                        }

                        receivedMsg = new byte[bytesRead];
                        Array.Copy(buffer, offset, receivedMsg, 0, bytesRead);

                        stateSaver.lb.receivedMessages.Add(receivedMsg);
                        //System.Windows.Forms.MessageBox.Show(stateSaver.receivecounter.ToString() + " recieved messages");
                        stateSaver.receivecounter++;
                    }
                    catch (Exception e)
                    {
                        stateSaver.receivecounter++;
                        testsize.ToString();
                        buffer.ToString();
                        e.Message.ToString();
                        //System.Windows.Forms.MessageBox.Show("SendReceiveClass.ReceivingHandler\r\n" + e.Message.ToString());
                    }
                }

                //System.Windows.Forms.MessageBox.Show("Finished Receiving");
        }