socket buffsize optimize

main
syneffort 2 years ago
parent 13334aeb21
commit 1c2b35345a
  1. 2
      SocketStudy/ClientForm/MainForm.cs
  2. 26
      SocketStudy/PComm/PClient.cs

@ -17,7 +17,7 @@ namespace ClientForm
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
private readonly string SERVER_IP = "10.233.96.172";//"124.56.13.173"; private readonly string SERVER_IP = "124.56.13.173";//"10.233.96.172", "124.56.13.173";
private PClient heartBeatClient; private PClient heartBeatClient;
private PClient client; private PClient client;

@ -16,7 +16,6 @@ namespace PComm
public class PClient public class PClient
{ {
private readonly int BUFF_SIZE = 1000 * 50; // 50kB private readonly int BUFF_SIZE = 1000 * 50; // 50kB
private readonly int LARGE_DATA_SIZE_THRESHOLD = 1000 * 50; // 50kB
public delegate void ClientReceivedHandler(PClient sender, PDataType dataType, byte[] data); public delegate void ClientReceivedHandler(PClient sender, PDataType dataType, byte[] data);
public event ClientReceivedHandler OnReceived; public event ClientReceivedHandler OnReceived;
@ -40,6 +39,8 @@ namespace PComm
id = Guid.NewGuid().ToString(); id = Guid.NewGuid().ToString();
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.ReceiveBufferSize = BUFF_SIZE;
socket.SendBufferSize = BUFF_SIZE;
this.ID = id; this.ID = id;
this.EndPoint = new IPEndPoint(IPAddress.Parse(ip), port); this.EndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
@ -52,6 +53,8 @@ namespace PComm
this.ID = Guid.NewGuid().ToString(); this.ID = Guid.NewGuid().ToString();
socket = accepted; socket = accepted;
socket.ReceiveBufferSize = BUFF_SIZE;
socket.SendBufferSize = BUFF_SIZE;
this.EndPoint = (IPEndPoint)socket.RemoteEndPoint; this.EndPoint = (IPEndPoint)socket.RemoteEndPoint;
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
@ -60,7 +63,11 @@ namespace PComm
public void Connect() public void Connect()
{ {
if (socket == null || !socket.Connected) if (socket == null || !socket.Connected)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.ReceiveBufferSize = BUFF_SIZE;
socket.SendBufferSize = BUFF_SIZE;
}
try try
{ {
@ -100,8 +107,8 @@ namespace PComm
} }
else else
{ {
Debug.WriteLine($"[ERROR] Socket close {socket.RemoteEndPoint.ToString()}"); Debug.WriteLine($"[ERROR] Socket close {this.EndPoint.ToString()}");
PFileManager.Instance.WriteLog($"[ERROR] Socket close {socket.RemoteEndPoint.ToString()}"); PFileManager.Instance.WriteLog($"[ERROR] Socket close {this.EndPoint.ToString()}");
Close(); Close();
@ -173,15 +180,11 @@ namespace PComm
socket.Receive(sizeBuff, sizeBuff.Length, SocketFlags.None); socket.Receive(sizeBuff, sizeBuff.Length, SocketFlags.None);
int dataSize = BitConverter.ToInt32(sizeBuff, 0); int dataSize = BitConverter.ToInt32(sizeBuff, 0);
// if data size is large, ready to receive data; // if data size is larger than buff size, ready to receive data;
if (dataSize >= LARGE_DATA_SIZE_THRESHOLD) if (dataSize >= BUFF_SIZE)
{ Thread.Sleep(100);
int factor = dataSize / LARGE_DATA_SIZE_THRESHOLD;
int gain = factor > 10000 ? 10000 : factor;
Thread.Sleep(gain);
}
using (MemoryStream ms = new MemoryStream()) using (MemoryStream ms = new MemoryStream(dataSize))
{ {
while (dataSize > 0) while (dataSize > 0)
{ {
@ -193,6 +196,7 @@ namespace PComm
int receiveSize = socket.Receive(buff, buff.Length, SocketFlags.None); int receiveSize = socket.Receive(buff, buff.Length, SocketFlags.None);
// Thread.Sleep(1);
ms.Write(buff, 0, buff.Length); ms.Write(buff, 0, buff.Length);
dataSize -= receiveSize; dataSize -= receiveSize;
} }

Loading…
Cancel
Save