Comments (3)
Hello @Reda-Beloued, thank you very much, yes of course, It would be a very nice feature to add.
from p2p.
Hey @miroslavpejic85, I would like to contribute to this issue. I have around 6 months of experience in ASP.NET and C#.
from p2p.
We can create a new UdtSocket
for a new connection
to handle the File Transfer
.
I leave a snippet to start/take inspiration from and a simple video on how to enable the drag & drop
on the PictureBox item.
Contributions are welcome :)
Receiver.cs
using System.IO;
using UdtSharp;
namespace p2pconn
{
static class Receiver
{
static internal void Run(UdtSocket connection)
{
using (var netStream = new UdtNetworkStream(connection))
using (var writer = new BinaryWriter(netStream))
using (var reader = new BinaryReader(netStream))
{
string fileName = reader.ReadString();
long size = reader.ReadInt64();
byte[] buffer = new byte[4 * 1024 * 1024];
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
{
long read = 0;
while (read < size)
{
int toRecv = reader.ReadInt32();
ReadFragment(reader, toRecv, buffer);
fileStream.Write(buffer, 0, toRecv);
read += toRecv;
writer.Write(true);
}
}
}
}
static int ReadFragment(BinaryReader reader, int size, byte[] buffer)
{
int read = 0;
while (read < size)
{
read += reader.Read(buffer, read, size - read);
}
return read;
}
}
}
Sender.cs
using System;
using System.IO;
using UdtSharp;
namespace p2pconn
{
static class Sender
{
static internal void Run(UdtSocket connection, string file, bool logVerbose)
{
using (var netStream = new UdtNetworkStream(connection))
using (var writer = new BinaryWriter(netStream))
using (var reader = new BinaryReader(netStream))
using (var fileReader = new FileStream(file, FileMode.Open, FileAccess.Read))
{
long fileSize = new FileInfo(file).Length;
writer.Write(Path.GetFileName(file));
writer.Write(fileSize);
byte[] buffer = new byte[512 * 1024];
long pos = 0;
while (pos < fileSize)
{
int toSend = buffer.Length < (fileSize - pos)
? buffer.Length
: (int)(fileSize - pos);
fileReader.Read(buffer, 0, toSend);
int iteration = Environment.TickCount;
writer.Write(toSend);
connection.Send(buffer, 0, toSend);
if (!reader.ReadBoolean())
{
Console.WriteLine("Error in transmission");
return;
}
pos += toSend;
if (logVerbose)
{
Console.WriteLine();
Console.WriteLine("Current: {0} / s",
SizeConverter.ConvertToSizeString(toSend / (Environment.TickCount - iteration) * 1000));
}
}
}
}
}
}
SizeConverter.cs
namespace p2pconn
{
internal enum DataSizeUnit
{
Bytes = 1,
KiloBytes = 1024,
MegaBytes = 1024 * 1024,
GigaBytes = 1024 * 1024 * 1024
};
internal static class SizeConverter
{
internal static string ConvertToSizeString(long size)
{
DataSizeUnit totalSizeUnit = SizeConverter.GetSuitableUnit(size);
return string.Format("{0:#0.##} {1}", SizeConverter.ConvertToSize(
size, totalSizeUnit), SizeConverter.GetUnitString(totalSizeUnit));
}
internal static float ConvertToSize(long size, DataSizeUnit unit)
{
return (float)size / (float)unit;
}
static string GetUnitString(DataSizeUnit unit)
{
switch (unit)
{
case DataSizeUnit.Bytes: return "bytes";
case DataSizeUnit.KiloBytes: return "KB";
case DataSizeUnit.MegaBytes: return "MB";
case DataSizeUnit.GigaBytes: return "GB";
}
return string.Empty;
}
static DataSizeUnit GetSuitableUnit(long size)
{
if (size >= 0 && size < (long)DataSizeUnit.KiloBytes)
return DataSizeUnit.Bytes;
else if (size >= (long)DataSizeUnit.KiloBytes && size <= (long)DataSizeUnit.MegaBytes)
return DataSizeUnit.KiloBytes;
else if (size >= (long)DataSizeUnit.MegaBytes && size <= (long)DataSizeUnit.GigaBytes)
return DataSizeUnit.MegaBytes;
else
return DataSizeUnit.GigaBytes;
}
}
}
from p2p.
Related Issues (20)
- join the repository
- Can we have the source of the server please ? HOT 1
- Reported as virus
- Capture UAC messages
- virus win32 detect from avast HOT 1
- Grey Screen During Remote HOT 3
- Could not find a working STUN server HOT 2
- Trojan HOT 1
- Connect clients using WAN HOT 7
- App is crashing if endpoint is without port number HOT 1
- Can´t connect with stun server. HOT 2
- Error when Build HOT 3
- test result HOT 1
- HELLO CAN YOU SHARE THE BUILD EXE THANKS HOT 3
- Two nodes cannot connect to each other and are not on the same LAN. Why? HOT 1
- How to build self-hosted P2P intermediate server like Stun(http://stun.l.goolg.com) ? HOT 1
- Ctrl+Alt+Del HOT 1
- Stun server doesnt work / ipv6 ? HOT 5
- Ks,samsun
- Could it be possible to connect by only from the one side like ANYDESK or Team Viewer? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from p2p.