现要通过socket传送一些较大的数据,通过异步方式实现。存在以下问题:
一、原始接收和发送缓冲区大小都定认为1024 编码为Unicode 传输中文没有问题,但只要将缓冲区大小变大,如1024*2,接收到的中文就成为乱码。请问是什么原因?
二、如果不修改缓冲区大小,采用分包传输的方式来传送,那如何能让socket对象多次读取,我代码如下: ConnectionState state = (ConnectionState)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
do{
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.Unicode.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, ConnectionState.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
//Send(client,Analysis(client,state.sb.ToString()));
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
} while (!state.sb.ToString().Contains("#&end&#"));
系统提示错误:只能为每个异步操作调用一次EndReceive。如何解决
一、原始接收和发送缓冲区大小都定认为1024 编码为Unicode 传输中文没有问题,但只要将缓冲区大小变大,如1024*2,接收到的中文就成为乱码。请问是什么原因?
二、如果不修改缓冲区大小,采用分包传输的方式来传送,那如何能让socket对象多次读取,我代码如下: ConnectionState state = (ConnectionState)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
do{
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.Unicode.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, ConnectionState.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
//Send(client,Analysis(client,state.sb.ToString()));
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
} while (!state.sb.ToString().Contains("#&end&#"));
系统提示错误:只能为每个异步操作调用一次EndReceive。如何解决
解决方案 »
- C#中出现未能进入中断模式,源文件不属于正在调试的项目。怎么解决呢?
- C#中Thread问题
- [高分求助]C# 2005 treeview控件如何实现以下效果?
- CheckedListBox属性SelectionMode已经设为System.Windows.Forms.SelectionMode.One;为什么还是多选?
- winform运行后,按回车,要在label1里显示"a",怎么写代码
- 怎么知道已经注册了哪些handler
- 晕了,我做了个Web控件,也加了属性,但是设计器不显示
- dotnetbar中的datagridviewx控件问题
- C#自定义控件怎么把背景色设成透明
- C#代码IIS7.5创建FTP站点
- 急!怎么在DataGridView中输入多条数据并添加到数据库中
- win7下Direct编程问题
最好每个包的包头都有一个此包的长度,如2楼所说
如果是 试着
把要发送的字段 如 string txt="您好!"+“ ”;
产生的乱码:涓嬪垪鍏充簬foreach璇彞鐨勮〃杩颁腑鍝簺鏄敊璇殑
一般有没有空格和乱码是没有关系的