C# windows窗体应用程序发送字符串
我制作了一个tcp/ip聊天windows窗体应用程序,它工作正常,但我想让应用程序自动发送文本,我不希望用户像实时流媒体一样单击发送按钮 我使用的是异步连接C# windows窗体应用程序发送字符串,c#,winforms,networking,asynchronous,tcp,C#,Winforms,Networking,Asynchronous,Tcp,我制作了一个tcp/ip聊天windows窗体应用程序,它工作正常,但我想让应用程序自动发送文本,我不希望用户像实时流媒体一样单击发送按钮 我使用的是异步连接 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.T
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();
}
}
}
我假设您希望发送用户在完成键入时输入的文本。即使我认为这是一个坏主意,因为它会让你的用户感到困惑,你也可以添加一个计时器来计算用户上一次击键后的时间,如果给定的时间过去了(比如说5秒),它也会调用您的按钮单击所调用的方法来发送字符串。嘿,我只是想知道该怎么做,我想在编写每个字符串时发送,所以我所做的只是处理文本框的事件,它自己这样做,所以当它发生更改时,它会立即发送:D祝您好运:)发布一些代码。你想在用户输入时发送文本吗?是:D我会添加代码,只需一秒钟@AbhishekI想在输入时发送字符串,它不是用于发送传感器数据的普通聊天应用程序。