windows form app发送字符串

时间:2015-09-21 09:11:19

标签: c# winforms networking asynchronous tcp

我制作了一个tcp / ip聊天窗口表单应用程序,它工作正常,但我想让应用程序自动发送文本我不希望用户像实时流媒体一样点击发送按钮! / p>

我正在使用异步连接。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


using System.Net;
using System.Net.Sockets;

namespace Chat
{
    public partial class Form1 : Form
    {
        Socket sck;
        EndPoint epLocal, epRemote;


        public Form1()
        {
            InitializeComponent();

            sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            textBox1.Text = GetLocalIP();//this is where the application is running IP address
            //textBox5.Text = GetLocalIP();
        }

        private string GetLocalIP()
        { 
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if(ip.AddressFamily == AddressFamily.InterNetwork)
                {  
                    return ip.ToString();
                }
            }

            return "127.0.0.1"; //here we put the android device's IP

        }

        private void MessageCallBack(IAsyncResult aResult)
        {
            try {

                int size = sck.EndReceiveFrom(aResult, ref epRemote);

                if(size>0)
                {
                    byte[] receivedData = new byte[1464];
                    receivedData = (byte[])aResult.AsyncState;

                    ASCIIEncoding eEncoding = new ASCIIEncoding();
                    string receivedMessage = eEncoding.GetString(receivedData);
                    //b3deen bntba3 el msg bs b7aletna bdna n5li el touch active.
                    listBox1.Items.Add("Sender:" + receivedMessage);
                }

                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {

                try
                {
                    //binding the message
                    epLocal = new IPEndPoint(IPAddress.Parse("192.168.1.9"), Convert.ToInt32("80"));
                    sck.Bind(epLocal);
                    //hoon el address ta3 el mobile 
                    epRemote = new IPEndPoint(IPAddress.Parse("192.168.1.9"), Convert.ToInt32("81"));//texbox5 bn7at el ip ta3 el android wl txt el tani ta3 le port
                    //hoon bn3ml connect network
                    sck.Connect(epRemote);

                    byte[] buffer = new byte[1500];
                    sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);

                    button1.Text = "Connected";
                    button1.Enabled = false;
                    button2.Enabled = true;
                    textBox3.Focus();

                    //trying to live sending




                }


                catch (Exception exp)
                {
                    MessageBox.Show(exp.ToString());
                }

        }
       private void button2_Click(object sender, EventArgs e)
        {
          try
            {
                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                byte[] msg = new byte[1500];
                msg = enc.GetBytes(textBox3.Text);

                sck.Send(msg);
                listBox1.Items.Add("YOU:" + textBox3.Text);


            }catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
           // Close();

        }
    }
}

2 个答案:

答案 0 :(得分:0)

我假设您要在用户输入时输入用户输入的文字进行输入。即使我说这是一个坏主意,因为它会让你的用户感到困惑,你可以添加一个计时器来计算自用户最后一次击键以来的时间,如果给定的时间已经过去(假设是5秒),它会调用您点击按钮的方法也可以调用以发送字符串。

答案 1 :(得分:0)

嘿所有我只是想知道该做什么我想在每次写入时发送每个字符串所以我做了什么我只是自己处理文本框的事件所以当它改变它会立即发送它:D祝你好运所有:)