所以我有点挑剔,我有这个代码在桌面 .net 4.5 上运行,但它不会在 Windows CE/Windows Mobile 6.1 Pro 的 .NET 3.5 中编译......
public void Init()
{
try
{
myTcpListener = new TcpListener(myAddress, myPortNumber);
myTcpListener.Server.RecieveBufferSize = 500000;
}
catch (Exception ex)
{
throw ex;
}
}
也尝试使用TcpClient.ReceiveBufferSize = 500000;
,但这会引发异常。
在 getsockopt 或 setsockopt 调用中指定了未知、无效或不受支持的选项或级别
我有点不确定如何继续......目的是有一个足够大的缓冲区来容纳 500.000 字节......也许我需要以不同的方式处理这个问题。
编辑:这是源代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Diagnostics;
namespace SmartDevice_Server
{
/// <summary>
/// ClientConnection saves connection information is used to keep context in Async and Event calls
/// </summary>
/// <param name="networkStream"> NetworkStream Object of TCPClient</param>
/// <param name="data">Byte array that serves as Buffer</param>
/// <returns></returns>
public class ClientConnection : EventArgs
{
public NetworkStream NetworkStream { get; private set; }
public byte[] Data { get; private set; }
public int byteReadCount { get; set; }
public ClientConnection(NetworkStream networkStream, byte[] data)
{
NetworkStream = networkStream;
Data = data;
}
}
/// <summary>
/// MySocket - Is a server that listens for events and triggers Events upon Request Completion
/// </summary>
public class MySocketTCP
{
#region Members
TcpListener myTcpListener;
TcpClient myTcpClient;
NetworkStream myNetworkStream;
const string localHost = "127.0.0.1";
IPAddress myAddress = IPAddress.Parse(localHost);
int myPortNumber = 58889;
byte[] myData;
int bytesReadCount;
const int MIN_REQUEST_STRING_SIZE = 10;
int TimeStart;
//Event Setup
public event socketReadCompleteHandler socketReadCompleteEvent;
public EventArgs eventArguments = null;
public delegate void socketReadCompleteHandler(MySocketTCP myTcpSocket, ClientConnection eventArguments);
//BufferSize
public const int myBufferSize = 300000;
#endregion
//Constructor
/// <summary>
/// MySocketTCP Constructor
/// NOTE: By default connects to local host (127.0.0.1:58889)
/// </summary>
/// <returns></returns>
public MySocketTCP()
{
Init();
}
/// <summary>
/// Constructor overloaded to receive IPAdress Host, and Port number
/// </summary>
/// <param name="hostAddress">IPAdress that represent an IP address</param>
/// <param name="portNumber">Integer that represents the port number</param>
/// <returns></returns>
public MySocketTCP(IPAddress hostAddress, int portNumber)
{
myAddress = hostAddress;
myPortNumber = portNumber;
Init();
}
/// <summary>
/// Initializes the TCPListner with address, and port number
/// </summary>
/// <returns></returns>
public void Init()
{
try
{
myTcpListener = new TcpListener(myAddress, myPortNumber);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Listens Asynchronously to Clients, class a recieveMessageHandler to process the read
///
/// * TODO_Listener_Timer: After you accept a connection you wait for data to be Read indefinitely
/// Possible solution: Use a timeout to close the socket connection.
/// Check WIKI, TODOS
/// </summary>
/// <returns></returns>
public void ListenAsync()
{
myTcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
myTcpClient = myTcpListener.AcceptTcpClient();
//myTcpClient.ReceiveBufferSize = 300000;
var client = new ClientConnection(myTcpClient.GetStream(), new byte[myTcpClient.ReceiveBufferSize]);
// Capture the specific client and pass it to the receive handler
client.NetworkStream.BeginRead(client.Data, 0, client.Data.Length, r => receiveMessageHandler(r, client), null);
}
}
/// <summary>
/// Callback is used to read the request Asynchronously, triggers socketReadCompleteEvent
/// </summary>
/// <param name="asyncResult"></param>
/// <param name="clientInstance"></param>
/// <returns></returns>
public void receiveMessageHandler(IAsyncResult asyncResult, ClientConnection clientInstance)
{
bytesReadCount = 0;
lock (clientInstance.NetworkStream)
{
try
{
bytesReadCount = clientInstance.NetworkStream.EndRead(asyncResult);
clientInstance.byteReadCount = bytesReadCount;
}
catch (System.IO.IOException ioexcp)
{
Debug.WriteLine(ioexcp.ToString());
}
catch (System.ObjectDisposedException objdispexc)
{
Debug.WriteLine(objdispexc.ToString());
}
catch (Exception excp)
{
Debug.WriteLine(excp.ToString());
}
}
if (bytesReadCount < MIN_REQUEST_STRING_SIZE)
{
//Could not read form client.
Debug.WriteLine("NO DATA READ. Close Connection");
//If there is no data then close connection.
//clientInstance.NetworkStream.Close();
}
else
{
if (socketReadCompleteEvent != null)
{
socketReadCompleteEvent(this, clientInstance);
}
}
}
/// <summary>
/// Reads the request, uses the ClientConnection for context
/// </summary>
/// <param name="connObj">Connection object contains the NetworkStream,
/// Data Buffer, and Number of bytes read.</param>
/// <returns></returns>
public string ReadAsync(ClientConnection connObj)
{
int bytesReadCount = connObj.byteReadCount;
byte[] myData = connObj.Data;
string xmlMessage;
try
{
xmlMessage = Encoding.ASCII.GetString(myData, 0, bytesReadCount);
}
catch (Exception ex)
{
throw ex;
}
return xmlMessage;
}
/// <summary>
/// Is used to send/write the message to the correct socket
/// Closes the connection.
/// </summary>
/// <param name="connObj">context object</param>
/// <param name="outMessage">message to send</param>
/// <returns></returns>
public void WriteAsync(ClientConnection connObj, string outMessage)
{
const int timeDivisor = 10000;
byte[] outBytes = Encoding.ASCII.GetBytes(outMessage);
try
{
Delay(outBytes.Length/timeDivisor);
connObj.NetworkStream.Write(outBytes, 0, outBytes.Length);
}
catch (Exception ex)
{
throw ex;
}
//TODO_5254: Closes the connection, check MoSync side.
//try
//{
// connObj.NetworkStream.Close();
//}
//catch (Exception ex)
//{
// throw ex;
//}
int TimeEnd = Environment.TickCount;
int TimeResult = TimeEnd - TimeStart;
}
/// <summary>
/// Closes TCPClient
/// Warning: Does not close the underlying connection.
/// </summary>
/// <returns></returns>
public void Close()
{
try
{
myTcpClient.Close();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Helper Function creates timeouts and delays.
/// </summary>
/// <param name="ms"></param>
/// <returns></returns>
private void Delay(int ms)
{
int time = Environment.TickCount;
do
{
if (Environment.TickCount - time >= ms) return;
} while (true);
}
}
}
更新:
try
{
bytesReadCount = clientInstance.NetworkStream.EndRead(asyncResult);
clientInstance.stringBuilder.Append(Encoding.ASCII.GetString(clientInstance.Data, 0, bytesReadCount));
//If buffer is bigger than myTcpClient.ReceiveBufferSize
while (clientInstance.NetworkStream.DataAvailable)
{
Debug.WriteLine("More data to read...");
//clientInstance.NetworkStream.EndRead(asyncResult);
int chuckByteSize = clientInstance.NetworkStream.Read(clientInstance.Data, 0, myTcpClient.ReceiveBufferSize);
bytesReadCount += chuckByteSize;
//string textReceived = Encoding.ASCII.GetString(clientInstance.Data, 0, chuckByteSize);
clientInstance.stringBuilder.Append(Encoding.ASCII.GetString(clientInstance.Data, 0, chuckByteSize));
//BP - Ver Data
}
clientInstance.byteReadCount = bytesReadCount;
//string sbString = clientInstance.sb.ToString();
//int sizeString = sbString.Length;
}
catch (System.IO.IOException ioexcp)
{
Debug.WriteLine(ioexcp.ToString());
}
catch (System.ObjectDisposedException objdispexc)
{
Debug.WriteLine(objdispexc.ToString());
}
catch (Exception excp)
{
Debug.WriteLine(excp.ToString());
}
}