通过udp获取服务器文件,c# - 通过UDP套接字获取已接收数据的计数(客户端-服务器聊天应用程序) - 堆栈内存溢出...

在开发一个UDP客户端-服务器聊天应用程序时,遇到一个问题:当用户输入长句子时,listview只能显示部分内容。已设定缓冲区大小为2000字节,但接收到的消息长度始终显示为2000,导致消息分割逻辑失效。问题可能在于数据接收处理。代码中包含了接收和发送消息的逻辑,以及尝试根据字符数分割消息的尝试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我试图通过UDP network开发客户端服务器应用程序。 我已经使用listview来显示聊天。 但是,当user1输入一个很长的句子时, listview将显示句子的一部分(因为它仅占一行)。

因此,我编写了一种逻辑,如果字符数超过70, sentdata分成几行。它在服务器端工作正常,但在客户端不工作。 我已将缓冲区数组大小设置为2000。

当我尝试在标签上打印接收到的数据massage.length时,它始终显示为2000。(这就是为什么,逻辑不起作用)。 是什么原因造成的? 如何克服呢? 我想要接收的消息中的字符计数..(与此一起发布代码)..

public partial class Form1 : Form

{

Socket sckt;

EndPoint epLocal, epRemote;

byte[] buffer;

public Form1()

{

InitializeComponent();

txtChat.Text = "";

}

private void Form1_Load(object sender, EventArgs e)

{

//setting up the socket

sckt = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

sckt.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

//getting users' IP

txtLocalIp.Text = GetIPLocal();

}

private string GetIPLocal()

{

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";

}

private void buttonConnect_Click(object sender, EventArgs e)

{

if (buttonConnect.Text != "Disconnect !")

{

//binding socket to the Button

epLocal = new IPEndPoint(IPAddress.Parse(txtLocalIp.Text), Convert.ToInt32(txtLocalPort.Text));

sckt.Bind(epLocal);

//connecting to Remote IP

epRemote = new IPEndPoint(IPAddress.Parse(txtRemoteIp.Text), Convert.ToInt32(txtRemotePort.Text));

sckt.Connect(epRemote);

listView1.Columns.Add("",0, HorizontalAlignment.Left);

listView1.Columns.Add("starting chat . . .", 444, HorizontalAlignment.Left);

//listening to (receiving from) the specific port on Remote side

buffer = new byte[2000];

sckt.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);

buttonConnect.Text = "Disconnect !";

}

else

{

txtRemoteIp.Text = "";

buttonConnect.Text = "Connect";

}

}

private void MessageCallBack(IAsyncResult aResult)

{

try

{

if (buttonConnect.Text == "Disconnect !")

{

byte[] receivedData = new byte[2000];

receivedData = (byte[])aResult.AsyncState;

//converting byte[] to string

ASCIIEncoding aEncoding = new ASCIIEncoding();

string receivedMessage = aEncoding.GetString(receivedData);

Console.Beep();

Thread.Sleep(1000);

int l = Convert.ToInt32(receivedData.Length) ;

label2.Text = "";

label1.Text = l.ToString();

string[] arr = new string[2];

ListViewItem itm;

arr[0] = "";

string firstString = receivedMessage;

if (firstString.Length > 70)

{

firstString = firstString.Remove(70);

string renewedString = receivedMessage;

renewedString = renewedString.Remove(0, 70);

if (renewedString.Length > 70)

{

string secondstring = renewedString.Remove(70);

renewedString = renewedString.Remove(0, 70);

if (renewedString.Length <= 70)

{

string s = "Friend : " + firstString;

arr[1] = s;

itm = new ListViewItem(arr);

listView1.Items.Add(itm);

arr[1] = secondstring;

itm = new ListViewItem(arr);

listView1.Items.Add(itm);

arr[1] = renewedString;

itm = new ListViewItem(arr);

listView1.Items.Add(itm);

}

else

{

MessageBox.Show("message too long, reduce size !");

}

}

}

else

{

arr[1] = "Friend : " + firstString;

itm = new ListViewItem(arr);

listView1.Items.Add(itm);

}

//callbacking again

buffer = new byte[2000];

sckt.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);

}

}

catch(Exception ex)

{

MessageBox.Show("ERROR : " + ex.ToString());

}

}

private void buttonSend_Click(object sender, EventArgs e)

{

try

{

if (buttonConnect.Text == "Disconnect !")

{

//convert string message to byte[]

ASCIIEncoding aEncoding = new ASCIIEncoding();

byte[] sendData = new byte[2000];

sendData = aEncoding.GetBytes(txtChat.Text);

//sending the Encoded message

sckt.Send(sendData);

string[] arr = new string[2];

ListViewItem itm;

arr[0] = "";

string firstString = txtChat.Text;

if (firstString.Length > 70)

{

firstString = firstString.Remove(70);

string renewedString = txtChat.Text;

renewedString = renewedString.Remove(0, 70);

if (renewedString.Length > 70)

{

string secondstring = renewedString.Remove(70);

renewedString = renewedString.Remove(0, 70);

if (renewedString.Length <= 70)

{

string s = "me : " + firstString;

arr[1] = s;

itm = new ListViewItem(arr);

listView1.Items.Add(itm);

arr[1] = secondstring;

itm = new ListViewItem(arr);

listView1.Items.Add(itm);

arr[1] = renewedString;

itm = new ListViewItem(arr);

listView1.Items.Add(itm);

}

else

{

MessageBox.Show("message too long, reduce size !");

}

}

}

else

{

arr[1] = "Me123 : " + firstString;

itm = new ListViewItem(arr);

listView1.Items.Add(itm);

}

txtChat.Text = "";

}

else

{

MessageBox.Show("Sorry, Session Expired !");

}

}

catch (Exception ex)

{

MessageBox.Show("ERROR : " + ex.ToString());

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值