本文整理汇总了C#中System.Net.Sockets.Socket.SendFile方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.SendFile方法的具体用法?C# Socket.SendFile怎么用?C# Socket.SendFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.SendFile方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendFile
private static void SendFile(Socket client, string fileName, byte[] bytes)
{
client.SendFile(fileName, null, bytes, TransmitFileOptions.UseDefaultWorkerThread);
}
示例2: TransmitFile
void TransmitFile(Socket client)
{
uint length = (uint)new FileInfo(mData.FilePath).Length;
int ini = Environment.TickCount;
using (NetworkStream ns = new NetworkStream(client))
{
byte[] lengthBytes = LengthSerializer.Serialize(length);
// send the header
client.SendFile(mData.FilePath, lengthBytes, null, TransmitFileOptions.UseKernelApc);
}
LogPrinter.LogTime("transmitfile: sent", length, ini);
}
示例3: sendFile
/// <summary>
/// Deze functie stuurt gegevens over de door de client gevraagde file, en daarop volgend
/// de file zelf in delen op naar de client en sluit vervolgens de verbinding.
/// </summary>
/// <param name="clientSock">De verbinding tussen de client en de server</param>
/// <param name="fileFullPath">Het bestandpad van de gevraagde file.</param>
public static void sendFile(Socket clientSock, string fileFullPath)
{
FileInfo file = null;
try
{
//File openen en gegevens uitlezen
file = new FileInfo(@"C:\Fileserver\" + fileFullPath);
string fileLength = file.Length.ToString();
string fileName = file.Name;
string fileNameLength = fileName.Length.ToString();
//integers aanvullen
while (fileLength.Length < 15)
{
fileLength = "0" + fileLength;
}
while (fileNameLength.Length < 3)
{
fileNameLength = "0" + fileNameLength;
}
//Headerpakketdelen maken
byte[] identifierBytes = Encoding.ASCII.GetBytes("4");
byte[] fileLengthBytes = Encoding.ASCII.GetBytes(fileLength);
byte[] fileNameLengthBytes = Encoding.ASCII.GetBytes(fileNameLength);
byte[] fileNameBytes = Encoding.ASCII.GetBytes(fileName);
byte[] data = new byte[1024];
//Headerpakket samenstellen
identifierBytes.CopyTo(data, 0);
fileLengthBytes.CopyTo(data, 1);
fileNameLengthBytes.CopyTo(data, 16);
fileNameBytes.CopyTo(data, 19);
//Headerpakket sturen
clientSock.Send(data);
//FileData pakketten sturen
clientSock.SendFile(@"C:\Fileserver\" + fileFullPath);
//Socket sluiten%
clientSock.Close();
}
catch (Exception e)
{
sendMessage(clientSock, e.Message);
}
}
示例4: sendFile
public void sendFile()
{
Socket s = new Socket(SocketType.Stream, ProtocolType.Tcp);
s.Connect(IPAddress.Parse("127.0.0.1"), 2013);
s.SendFile(path);
s.Close();
}
示例5: send
public int send(string Address, string file, int port, bool SaveLog)
{
try
{
Debug.WriteLine("Starting sender....");
IPEndPoint ipEndPoint = CreateIPEndPoint(Address + ":" + port.ToString());
Debug.WriteLine("IpEndPoint created");
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Debug.WriteLine("client created");
client.Connect(ipEndPoint);
Debug.WriteLine("Connected");
byte[] data = new byte[11];
Debug.WriteLine("data created");
client.Receive(data);
Debug.WriteLine("Recieved data:");
string RecivedData = "";
for (int i = 0; i < data.Length; i++) RecivedData = RecivedData + data[i].ToString();
Debug.WriteLine(RecivedData);
string ShouldRecive = "";
for (int i = 0; i < Encoding.UTF8.GetBytes("YATA SENDER").Length; i++) ShouldRecive = ShouldRecive + Encoding.UTF8.GetBytes("YATA SENDER")[i].ToString();
if (!RecivedData.Contains(ShouldRecive)) { return 1;}
Debug.WriteLine("data matches YATA SENDER");
if (!File.Exists(file)) { return 2; }
Debug.WriteLine("File exists");
client.SendFile(file);
ShouldRecive = "";
for (int i = 0; i < Encoding.UTF8.GetBytes("YATA TERM").Length; i++) ShouldRecive = ShouldRecive + Encoding.UTF8.GetBytes("YATA TERM")[i].ToString();
data = new byte[9];
client.Receive(data);
RecivedData = "";
for (int i = 0; i < data.Length; i++) RecivedData = RecivedData + data[i].ToString();
Debug.WriteLine(RecivedData);
if (!RecivedData.Contains(ShouldRecive)) { Debug.WriteLine("YATA TERM not recived"); return 2; }
client.Shutdown(SocketShutdown.Both);
Debug.WriteLine("Client shutdown");
client.Close();
Debug.WriteLine("Client close");
return 0;
}
catch (Exception ex)
{
Debug.WriteLine("Exception:");
Debug.WriteLine("Message" + ex.Message);
Debug.WriteLine("Inner exception" + ex.InnerException);
Debug.WriteLine(ex.ToString());
// MessageBox.Show("There was an error: " + ex.Message + "\r\n" + ex.InnerException);
return -1;
}
}
示例6: SendFile
public void SendFile(string filePath)
{
if (string.IsNullOrEmpty(filePath)) filePath = GetFileName();
var fileInfo = new FileInfo(filePath);
var fName = fileInfo.Name;
byte[] preBuffer;
using (var memoryStream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(memoryStream))
{
writer.Write(fName);
//writer.Write(md5Hash);
}
preBuffer = memoryStream.ToArray();
}
var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect("127.0.0.1", 9050);
clientSocket.SendFile(filePath, preBuffer, null,
TransmitFileOptions.UseDefaultWorkerThread);
clientSocket.Close();
}
示例7: sendFileTCP
private static bool sendFileTCP(Stream stream, long fileSize, string fileName)
{
try
{
byte[] buf = new byte[SIZE_STRUCTURE_UPLOAD];
BitConverter.GetBytes((char)1).CopyTo(buf, 0);
//копируем токен
var utf8bytes = Encoding.UTF8.GetBytes(USER_TOKEN);
var ascii = Encoding.Convert(
Encoding.UTF8, Encoding.GetEncoding("ASCII"), utf8bytes);
ascii.CopyTo(buf, 1);
//--
//копируем имя файла
utf8bytes = Encoding.UTF8.GetBytes(fileName);
ascii = Encoding.Convert(
Encoding.UTF8, Encoding.GetEncoding("ASCII"), utf8bytes);
ascii.CopyTo(buf, 130);
//--
//копируем директори id
utf8bytes = Encoding.UTF8.GetBytes(DIRICTORYID);
ascii = Encoding.Convert(
Encoding.UTF8, Encoding.GetEncoding("ASCII"), utf8bytes);
ascii.CopyTo(buf, 386);
//----------
BitConverter.GetBytes(fileSize).CopyTo(buf, 412);
//stream.Read(buf, SIZE_STRUCTURE_UPLOAD, (int)stream.Length);
//byte[] b = new byte[1];
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var ip = IPAddress.Parse(SERVER_UPLOAD);
IPEndPoint ipep = new IPEndPoint(ip, PORT_CONN_TCP);
soc.NoDelay = true;
soc.Connect(ipep);
soc.Send(buf);
soc.SendFile(@"C:\Users\ilya\Desktop\CloudsRIS-devClient\sendFile\sendFile\map1.png");
byte[] sendForFile = new byte[8193];
int bytesAllSend = 0;
while (bytesAllSend < fileSize)
{
int bytesWasRead = stream.Read(sendForFile, 0, 8192);
bytesAllSend += bytesWasRead;
soc.Send(sendForFile, bytesWasRead, SocketFlags.None);
}
soc.Shutdown(SocketShutdown.Send);
// soc.Receive(b);
soc.Close();
// if (b[0] == 1)
// return true;
// return false;
return true;
}
catch (Exception e)
{
return false;
}
}
示例8: SendFileMethod
private void SendFileMethod(Socket s, params object[] objects)
{
if (objects.Length != 3) throw new ArgumentException(objects.Length.ToString());
s.SendFile((string)objects[0], (byte[])objects[1], (byte[])objects[2], TransmitFileOptions.UseKernelApc);
}
示例9: SendFile
/// <summary>
/// Synchronous file transmission operation
/// </summary>
///
/// <param name="FilePath">The full path to the file</param>
public void SendFile(string FilePath)
{
// establish the local endpoint for the socket
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
// create a TCP socket.
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint
client.Connect(ipEndPoint);
// send file fileName to remote device
client.SendFile(FilePath);
// release the socket
client.Shutdown(SocketShutdown.Both);
client.Close();
}
示例10: button1_Click
private void button1_Click(object sender, EventArgs ev)
{
textBox2.Text = "";
byte[] bytes = new byte[1024];
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse(ip.Text);
IPEndPoint remoteEP = new IPEndPoint(ipaddress, Int32.Parse(puerto.Text));
// Create a TCP/IP socket.
Socket sendi = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint. Catch any errors.
try
{
sendi.Connect(remoteEP);
textBox2.Text = "Socket connected to {0}" +
sendi.RemoteEndPoint.ToString();
// Encode the data string into a byte array.
if (msge.Text != "")
{
byte[] msg = Encoding.ASCII.GetBytes(msge.Text);
// Send the data through the socket.
int bytesSent = sendi.Send(msg);
}
if (path.Text != "")
{
sendi.SendFile(path.Text);
}
// Receive the response from the remote device.
int bytesRec = sendi.Receive(bytes);
textBox2.Text = "Echoed test = {0} " +
Encoding.ASCII.GetString(bytes, 0, bytesRec);
// Release the socket.
sendi.Shutdown(SocketShutdown.Both);
sendi.Close();
}
catch (ArgumentNullException ane)
{
textBox2.Text = "ArgumentNullException : {0}" + ane.ToString();
}
catch (SocketException se)
{
textBox2.Text = "SocketException : {0}" + se.ToString();
}
catch (Exception e)
{
textBox2.Text = "Unexpected exception : {0}" + e.ToString();
}
}
catch (Exception e)
{
textBox2.Text = e.ToString();
}
}
示例11: Send
public void Send(string filePath)
{
if (string.IsNullOrEmpty(filePath)) filePath = GetFileName();
var fileInfo = new FileInfo(filePath);
var fName = fileInfo.Name;
var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] fileName = Encoding.UTF8.GetBytes(fName); //file name
//byte[] fileData = File.ReadAllBytes(filePath); //file
byte[] fileNameLen = BitConverter.GetBytes(fileName.Length); //lenght of file name
//var clientData = new byte[4 + fileName.Length + fileData.Length];
var clientData = new byte[4 + fileName.Length];
fileNameLen.CopyTo(clientData, 0);
fileName.CopyTo(clientData, 4);
//fileData.CopyTo(clientData, 4 + fileName.Length);
clientSocket.Connect("127.0.0.1", 9050); //target machine's ip address and the port number
//Connection.Send(clientData);
clientSocket.SendFile(filePath, clientData, null, TransmitFileOptions.WriteBehind);
clientSocket.Close();
}