private void btnSendFile_Click_1(object sender, EventArgs e)
        {
            try
            {
                string         fileString = string.Empty;
                OpenFileDialog ofd        = new OpenFileDialog();
                ofd.AutoUpgradeEnabled = false;
                ofd.Filter             = "Text File|*.txt";
                // ofd.Filter = "Image Files |*.JPG;*.PNG| Text File|*.txt";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string path     = ofd.FileName;
                    byte[] fileByte = File.ReadAllBytes(path);
                    fileString += Encoding.ASCII.GetString(fileByte);

                    MessagePackage package = new MessagePackage();
                    string         toSendMessage;
                    toSendMessage = package.ConcatenateMessage(MessageType, 1, MyName, this.Text, fileString);
                    byte[] buff = Encoding.ASCII.GetBytes(toSendMessage);
                    ClientSocket.BeginSend(buff, 0, buff.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
                }
            }
            catch (SocketException) { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 private void buttonSend_Click(object sender, EventArgs e)
 {
     btnSendFile.Enabled = true;
     try
     {
         MessagePackage package = new MessagePackage();
         string         toSendMessage;
         toSendMessage = package.ConcatenateMessage(MessageType, 0, MyName, this.Text, textBoxType.Text);
         byte[] buff = Encoding.ASCII.GetBytes(toSendMessage);
         ClientSocket.BeginSend(buff, 0, buff.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
     }
     catch (SocketException) { }
     catch (Exception ex)
     {
         MessageBox.Show("2 " + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Beispiel #3
0
        private void buttonConnect_Click_1(object sender, EventArgs e)
        {
            //I am trying to connect tto the server
            MessageBox.Show("clicked On connect Button");
            try
            {
                ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ClientSocket.BeginConnect(new IPEndPoint(IPAddress.Parse(this.textBoxAddress.Text), Int32.Parse(this.textBoxPort.Text)), new AsyncCallback(ConnectCallback), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            try
            {
                MessagePackage package     = new MessagePackage();
                string         fullMessage = package.ConcatenateMessage(4, 0, textBoxName.Text, String.Empty, String.Empty);
                buffer = Encoding.ASCII.GetBytes(fullMessage);
                ClientSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
            }
            catch (SocketException) { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            try
            {
                buffer = new byte[ClientSocket.ReceiveBufferSize];
                ClientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }