Hola Saludos de nuevo aqui les dejo un ejemplo basico para transferir archivos desde un cliente a un servidor en C# ojala les sirva la idea para crear alguna herramienta “interesante” en el archivo de descarga la ip esta configurada como la 127.0.0.1 o sea la localhost pero uds pueden modificarla a su gusto ya saben cualquier duda comuniquense con el staff…
Descargalo Aqui—–> tranferencia_C#
xxxnocturnoxxx
Este es el codigo del Cliente
//
namespace FTClientCode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FileDialog fDg = new OpenFileDialog();
if (fDg.ShowDialog() == DialogResult.OK)
{
FTClientCode.SendFile(fDg.FileName);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label3.Text = FTClientCode.curMsg;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("www.aztekmindz.org");
}
}
class Cliente
{
public static string curMsg = "En espera";
public static void SendFile(string fileName)
{
try
{
IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
string filePath = "";
fileName = fileName.Replace("\\", "/");
while (fileName.IndexOf("/") > -1)
{
filePath += fileName.Substring(0, fileName.IndexOf("/") + 1);
fileName = fileName.Substring(fileName.IndexOf("/") + 1);
}
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
if (fileNameByte.Length > 850 * 1024)
{
curMsg = "Trata con un Tamano menor";
return;
}
curMsg = "Almacenando….";
byte[] fileData = File.ReadAllBytes(filePath + fileName);
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);
curMsg = "Conectando al server…";
clientSock.Connect(ipEnd);
curMsg = "Enviando archivo";
clientSock.Send(clientData);
curMsg = "Desconectando..";
clientSock.Close();
curMsg = "Archivo transferido";
}
catch (Exception ex)
{
if (ex.Message == "No se establecio la coneccion")
curMsg = "Fallo en el envio Server no respondio.";
else
curMsg = "Error Coneccion no establecida con el server.";
}
}
}
}
Este es el codigo del servidor
namespace ServerCode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
FTServerCode.receivedPath = "";
}
private void button1_Click(object sender, EventArgs e)
{
if (FTServerCode.receivedPath.Length > 0)
backgroundWorker1.RunWorkerAsync();
else
MessageBox.Show("Selecciona la carpeta destino");
}
private void timer1_Tick(object sender, EventArgs e)
{
label5.Text = FTServerCode.receivedPath;
label3.Text = FTServerCode.curMsg;
}
FTServerCode obj = new FTServerCode();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
obj.StartServer();
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
FTServerCode.receivedPath = fd.SelectedPath;
}
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("www.aztekmindz.org");
}
}
class FTServerCode
{
IPEndPoint ipEnd;
Socket sock;
public FTServerCode()
{
ipEnd = new IPEndPoint(IPAddress.Any, 5656);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
}
public static string receivedPath;
public static string curMsg = "Detenido";
public void StartServer()
{
try
{
curMsg = "Iniciando..";
sock.Listen(100);
curMsg = "Corriendo y esperando transferencia";
Socket clientSock = sock.Accept();
byte[] clientData = new byte[1024 * 5000];
int receivedBytesLen = clientSock.Receive(clientData);
curMsg = "Recibiendo Archivo";
int fileNameLen = BitConverter.ToInt32(clientData, 0);
string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath +"/"+ fileName, FileMode.Append)); ;
bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
curMsg = "Guardando archivo";
bWrite.Close();
clientSock.Close();
curMsg = "Operacion exitosa";
}
catch (Exception ex)
{
curMsg = "Error en el proceso";
}
}
}
}
Justo lo que estaba buscando. Lo estudiarĂ© para aprender bien el funcionamiento y poder agregarle cosas. Muchas gracias…