Ejemplo n.º 1
0
        public MainForm()
        {
            InitializeComponent();

            // Default vaule
            coding = Encoding.UTF8;
            ipaddress = IPAddress.Parse("127.0.0.1");
            port = 5964;

            client = new AsyncSocketSender();
            client.messageReceiver += new AsyncSocketSender.MessageReceiverDelegate(socketHandle);

            txtIP.Text = ipaddress.ToString();
            txtPort.Text = port.ToString();

            if (File.Exists("debug.log"))
                File.Delete("debug.log");
        }
Ejemplo n.º 2
0
        private void socketHandle(byte[] data, AsyncSocketSender.AsyncState state)
        {
            if (this.InvokeRequired) {
                this.Invoke(new AsyncSocketSender.MessageReceiverDelegate(socketHandle), new object[] { data, state });
                return;
            }

            switch (state)
            {
                case AsyncSocketSender.AsyncState.ConnectSuccess:
                    txtMessage.AppendText("Connection Successful.\n");
                    txtIP.Enabled = false;
                    txtPort.Enabled = false;
                    btnConnect.Enabled = false;
                    btnDisconn.Enabled = true;
                    btnSend.Enabled = true;
                    break;
                case AsyncSocketSender.AsyncState.ConnectFail:
                case AsyncSocketSender.AsyncState.Disconncted:
                    txtMessage.AppendText("Connection Failed.\n");
                    txtIP.Enabled = true;
                    txtPort.Enabled = true;
                    btnConnect.Enabled = true;
                    btnDisconn.Enabled = false;
                    btnSend.Enabled = false;
                    break;
                case AsyncSocketSender.AsyncState.SendSuccess:
                    txtMessage.AppendText("Send Successful.\n");
                    break;
                case AsyncSocketSender.AsyncState.SendFail:
                    txtMessage.AppendText("Send Failed.\n");
                    break;
                case AsyncSocketSender.AsyncState.ReadMessage:
                    if (cbLogFile.Checked == true) {
                        using (FileStream sw = new FileStream("debug.log", FileMode.OpenOrCreate|FileMode.Append, FileAccess.Write)) {
                            sw.Write(data, 0, data.Count());
                            sw.Flush();
                        }
                    }
                    txtMessage.AppendText("(" + txtIP.Text + " " + DateTime.Now.ToString() + ")\r\n" + coding.GetString(data) + "\n");
                    break;
            }
        }