我有一個從緩衝區中獲取數據,並將其轉換爲字符串,你可以看到一個功能:在c#中獲得unicode緩衝區字節返回'??????'
public string GetParameters(byte[] buf)
{
string result = System.Text.UnicodeEncoding.Unicode.GetString(buf);
return result;
}
另一個應用程序發送Farsi
字符串來緩衝和我的應用程序應該得到這個數據,但我的應用程序的函數返回這個:
㨲께裙듘�����¯
我怎麼能接受的所有數據: 我定義的:
public byte[] dataBuffer = new byte[4000];
我有這樣的功能:
public void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
// Specify the call back function which is to be
// invoked when there is any write activity by the
// connected client
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
SocketPacket theSocPkt = new SocketPacket();
theSocPkt.m_currentSocket = soc;
// Start receiving any data written by the connected client
// asynchronously
soc.BeginReceive(theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Length,
SocketFlags.None,
pfnWorkerCallBack,
theSocPkt);
}
catch (Exception qqq)
{
}
}
而另一個問題:
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
int iRx = 0;
// Complete the BeginReceive() asynchronous call by EndReceive() method
// which will return the number of characters written to the stream
// by the client
iRx = socketData.m_currentSocket.EndReceive(asyn);
string res = GetParameters(socketData.dataBuffer);
}
使用'UTF32'而不是'Unicode' –
恰好4k字節是一個可疑的整數。您只需要解碼具有數據的緩衝區的部分。 –
@ M.kazemAkhgary utf32返回: –