SafeDispatch/SafeMobileLIB_DLL/smnetwork.cs

72 lines
2.0 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace SafeMobileLib_MySql
{
public class SMnetwork
{
/* NetworkStream that will be used */
private NetworkStream m_stream;
/* TcpClient that will connect for us */
private TcpClient m_client;
/* Storage space */
private byte[] m_buffer;
public string m_server;
public int m_port;
public SMnetwork(string p_server, int p_port)
{
m_server = p_server;
m_port = p_port;
}
~SMnetwork()
{
this.Close();
}
public void Connect()
{
m_client = new TcpClient(m_server, m_port);
/* Store the NetworkStream */
m_stream = m_client.GetStream();
/* Create data buffer */
m_buffer = new byte[m_client.ReceiveBufferSize];
}
public string Read()
{
/* Reading data from socket (stores the length of data) */
int lData = m_stream.Read(m_buffer, 0, m_client.ReceiveBufferSize);
/* String conversion (to be displayed on console) */
return Encoding.ASCII.GetString(m_buffer, 0, lData);
}
public void Write(string p_data)
{
/* Sending the data */
m_stream.Write(Encoding.ASCII.GetBytes(p_data.ToCharArray()), 0, p_data.Length);
}
public void WriteWithSize(string p_data)
{
char[] buf = new char[600];
buf[1] = (char)(p_data.Length & 0x00ff);
buf[0] = (char)((p_data.Length & 0xff00)>>8);
char[] tmp = p_data.ToCharArray();
for (int i = 0; i < p_data.Length; i++)
buf[i + 2] = tmp[i];
/* Sending the data */
m_stream.Write(Encoding.ASCII.GetBytes(buf), 0, p_data.Length+2);
}
public void Close()
{
m_client.Close();
}
}
}