Skip to content

Instantly share code, notes, and snippets.

@pingkunga
Created August 22, 2023 06:41
Show Gist options
  • Save pingkunga/32e527e08425b577d958de56108d64eb to your computer and use it in GitHub Desktop.
Save pingkunga/32e527e08425b577d958de56108d64eb to your computer and use it in GitHub Desktop.
TCP_JSON#07
public class InvsRequestHandler : AbstractBaseRequestHandler, IRTRequestHandler
{
protected override void run()
{
try
{
while (true)
{
NetworkStream ns = clientSocket.GetStream();
StringBuilder response = new StringBuilder();
var buffer = new byte[clientSocket.ReceiveBufferSize+10];
while (ns.DataAvailable)
{
int bytes = ns.Read(buffer, 0, clientSocket.ReceiveBufferSize);
response.Append(Encoding.Unicode.GetString(buffer, 0, bytes));
}
if (String.IsNullOrEmpty(response.ToString()))
{
continue;
}
ParamDTO deserialized = JSONHelper.deserialize<ParamDTO>(response.ToString());
//Your Business Logic
IList<BusinnesLogicDTO> resultls= ProcessSomeLogic(param);
//...
//Serialize Object and Send Result Back to Client
Byte[] mb = JSONHelper.serializeObject<IList<BusinnesLogicDT>(resultls);
ns.Write(mb, 0, mb.Length);
ns.Flush();
}
}
catch (Exception wex)
{
clientSocket.Close();
logger.Error(wex.Message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment