/*CLinte
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Cliente_TCP
{
public partial class FrmClienteTcp : Form
{
public FrmClienteTcp()
{
InitializeComponent();
}
private void FrmClienteTcp_Load(object sender, EventArgs e)
{
}
private void btnExplorar_Click(object sender, EventArgs e)
{
OpenFileDialog dialogo = new OpenFileDialog();
if (dialogo.ShowDialog() == DialogResult.OK)
{
txtNombreArchivo.Text = dialogo.FileName;
btnEnviar.Enabled = true;
}
}
private void btnEnviar_Click(object sender, EventArgs e)
{
Stream flujoArchivo = File.OpenRead(txtNombreArchivo.Text);
byte[] bufferEnvio = new byte[flujoArchivo.Length];
flujoArchivo.Read(bufferEnvio, 0, (int)flujoArchivo.Length);
TcpClient cliente = new TcpClient(txtServidor.Text, 12345);
NetworkStream flujoRed = cliente.GetStream();
flujoRed.Write(bufferEnvio, 0, bufferEnvio.GetLength(0));
flujoRed.Close();
}
}
}
*/
========================================================================
/*Servidor
//Agregar
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Servidor_TCP
{
public partial class FrmServidorTcp : Form
{
private ArrayList clientes;
private IPAddress[] informacionIP;
private IPAddress ipReal;
private delegate void ActualizarListBox(string texto);
public FrmServidorTcp()
{
InitializeComponent();
}
private void FrmServidorTcp_Load(object sender, EventArgs e)
{
//guarda todas las direcciones IP que tiene el hostname.
informacionIP = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in informacionIP)
{
if (ip.AddressFamily.ToString() == ProtocolFamily.InterNetwork.ToString())
if (!ip.Equals("127.0.0.1"))
ipReal = ip;
}
lblInfo.Text = "Mi dirección IP es: " + ipReal.ToString();
clientes = new ArrayList();
//Inicio del hilo
Thread hiloEscucha = new Thread(new ThreadStart(TareaEscucha));
hiloEscucha.Start();
}
public void TareaEscucha()
{
TcpListener socketEscucha = new TcpListener(ipReal, 12345);
socketEscucha.Start();
while (true)
{
Socket socketCliente = socketEscucha.AcceptSocket();
if (socketCliente.Connected)
{
lsbClientes.Invoke(new ActualizarListBox(ActualizarInformacionEnListBox), new object[] { socketCliente.RemoteEndPoint.ToString() + " conectado..." });
lock (this)
{
clientes.Add(socketCliente);
}
ThreadStart iniciadorHiloCliente = new ThreadStart(TareaCliente);
Thread hiloCliente = new Thread(iniciadorHiloCliente);
hiloCliente.Start();
}
}
}
public void TareaCliente()
{
Socket cliente = (Socket)clientes[clientes.Count - 1];
NetworkStream flujoRed = new NetworkStream(cliente);
int indicadorLectura = 0;
int tamanioBloque = 1024;
int numero = 1;
IPEndPoint ipRemoto = cliente.RemoteEndPoint as IPEndPoint;
DateTime hora = DateTime.Now;
string nombreFichero = "Archivos" + numero++ +"_"+ ipRemoto.Address +"_" +hora.Hour.ToString() + "." + hora.Minute.ToString() + "." + hora.Second.ToString() +".txt";
Byte[] datosRx = new Byte[tamanioBloque];
lock(this)
{
Stream flujoArchivo = File.OpenWrite("B:\\Repaso\\Form_TCP_Client_ExplorarArchivos"+nombreFichero);
while(true)
{
indicadorLectura = flujoRed.Read(datosRx, 0, tamanioBloque);
flujoArchivo.Write(datosRx, 0, indicadorLectura);
if (indicadorLectura == 0)
break;
}
flujoArchivo.Close();
}
lsbClientes.Invoke(new ActualizarListBox(ActualizarInformacionEnListBox), new object[] { "Archivo guardado exitosamente..." });
cliente = null;
}
public void ActualizarInformacionEnListBox(string texto)
{
lsbClientes.Items.Add(texto);
}
}
}
*/
No hay comentarios:
Publicar un comentario