我有两种方法,prepareData()
并且sendData()
:
private void prepareData(string longFileName, string shortFileName)
{
try
{
byte[] fileNameByte = Encoding.ASCII.GetBytes(shortFileName);
byte[] fileData = File.ReadAllBytes(longFileName);
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
}
catch
{
}
}
private void sendData(string clientIP, int clientPort, byte[] clientData)
{
TcpClient clientSocket = new TcpClient(clientIP, clientPort);
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Write(clientData, 0, clientData.GetLength(0));
networkStream.Close();
clientSocket.Close();
}
prepareData()
在程序加载时调用;这是一项艰巨的任务。
sendData()
每隔几秒调用一次,应该发送byte[] clientData
from prepareData()
。
我如何byte[]
从第一种方法到第二种方法?