Socket sendSocket = null;
try
{
sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sendSocket.Connect(IP);
string fileName = Path.GetFileName(filePath);
byte[] buffCompanyID = new byte[4];
buffCompanyID = BitConverter.GetBytes(fCompanyID);
byte[] buffImageLength = new byte[4];
buffImageLength = BitConverter.GetBytes(fileName.Length);
byte[] buffImageName = new byte[fileName.Length];
buffImageName = Encoding.Default.GetBytes(fileName);
Stream s = File.Open(filePath, FileMode.Open);
int leng = 0;
if (s.Length < int.MaxValue)
leng = (int)s.Length;
byte[] by = new byte[leng];
s.Read(by, 0, leng);
s.Close();
MemoryStream ms = new MemoryStream();
ms.Write(buffCompanyID, 0, 4);
ms.Write(buffImageLength, 0, 4);
ms.Write(buffImageName, 0, fileName.Length);
ms.Write(by, 0, leng);
byte[] anotherBytes = ms.GetBuffer();
sendSocket.Send(anotherBytes, 8 + fileName.Length + leng, SocketFlags.None);
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (sendSocket != null)
{
sendSocket.Shutdown(SocketShutdown.Both);
sendSocket.Close();
}
}
return true;
} |