G
Guest
At the TCP server side I'm using:
///////////////////////////////////////////////////////////////////////////////
IPAddress DEFAULT_SERVER = IPAddress.Parse("127.0.0.1");
IPEndPoint ipNport = new IPEndPoint(DEFAULT_SERVER, 31001);
TcpListener m_server = new TcpListener(ipNport);
while( true )// For ever for this example
{
Socket m_clientSocket = m_server.AcceptSocket();
m_clientSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveBuffer, 65535);
Thread m_clientListenerThread = new Thread(new
ThreadStart(SocketListenerThreadStart));
m_clientListenerThread.Start();
}
private void SocketListenerThreadStart()
{
int size = 0;
byte [] byteBuffer = new byte[131072];// 128 Kilobyte.
while( true )// For ever for this example
{
size = m_clientSocket.Receive(byteBuffer);
}
}
///////////////////////////////////////////////////////////////////////////////
And a the client side:
///////////////////////////////////////////////////////////////////////////////
Thread m_clientThread = new Thread( new ThreadStart(ClientThreadStart) );
m_clientThread.Start();
private void ClientThreadStart()
{
Socket clientSocket = null;
int blockSize = 65535; // 64 KByte - 1 byte
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 31001);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
clientSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendBuffer, 65535);
clientSocket.Connect(ep);
byte [] rowData = new byte[ 1024 * 1024 ];
int blockCount = rowData.Length/blockSize + ((rowData.Length % blockSize
== 0) ? 0 : 1);
// Transmitting the buffer in blocks.
int length, sentSize;
for( i=0; i < blockCount ; ++i )
{
if( i < (blockCount - 1) )
{
length = blockSize;
}
else
{
length = rowData.Length - (i * blockSize);
}
sentSize = clientSocket.Send(rowData, i*blockSize, length,
System.Net.Sockets.SocketFlags.None);
if( sentSize < length )
{
// ERROR ?!
}
}
clientSocket.Close();
}
///////////////////////////////////////////////////////////////////////////////
The Send at the client side always return the same size that I asked for,
BUT the Receive on the server side almost all the time return size of smaller
size.
Is that how it should work? Why the receive socket does not buffer as I
asked (65535) ?
Can I improve it?
///////////////////////////////////////////////////////////////////////////////
IPAddress DEFAULT_SERVER = IPAddress.Parse("127.0.0.1");
IPEndPoint ipNport = new IPEndPoint(DEFAULT_SERVER, 31001);
TcpListener m_server = new TcpListener(ipNport);
while( true )// For ever for this example
{
Socket m_clientSocket = m_server.AcceptSocket();
m_clientSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveBuffer, 65535);
Thread m_clientListenerThread = new Thread(new
ThreadStart(SocketListenerThreadStart));
m_clientListenerThread.Start();
}
private void SocketListenerThreadStart()
{
int size = 0;
byte [] byteBuffer = new byte[131072];// 128 Kilobyte.
while( true )// For ever for this example
{
size = m_clientSocket.Receive(byteBuffer);
}
}
///////////////////////////////////////////////////////////////////////////////
And a the client side:
///////////////////////////////////////////////////////////////////////////////
Thread m_clientThread = new Thread( new ThreadStart(ClientThreadStart) );
m_clientThread.Start();
private void ClientThreadStart()
{
Socket clientSocket = null;
int blockSize = 65535; // 64 KByte - 1 byte
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 31001);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
clientSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendBuffer, 65535);
clientSocket.Connect(ep);
byte [] rowData = new byte[ 1024 * 1024 ];
int blockCount = rowData.Length/blockSize + ((rowData.Length % blockSize
== 0) ? 0 : 1);
// Transmitting the buffer in blocks.
int length, sentSize;
for( i=0; i < blockCount ; ++i )
{
if( i < (blockCount - 1) )
{
length = blockSize;
}
else
{
length = rowData.Length - (i * blockSize);
}
sentSize = clientSocket.Send(rowData, i*blockSize, length,
System.Net.Sockets.SocketFlags.None);
if( sentSize < length )
{
// ERROR ?!
}
}
clientSocket.Close();
}
///////////////////////////////////////////////////////////////////////////////
The Send at the client side always return the same size that I asked for,
BUT the Receive on the server side almost all the time return size of smaller
size.
Is that how it should work? Why the receive socket does not buffer as I
asked (65535) ?
Can I improve it?