elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Enviar Recibir Archivo via Socket
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Enviar Recibir Archivo via Socket  (Leído 3,412 veces)
spysecurityca

Desconectado Desconectado

Mensajes: 58


Ver Perfil
Enviar Recibir Archivo via Socket
« en: 21 Enero 2016, 01:38 am »

Bien, estoy haciendo un programa para enviar Archivos por Socket, cuando pruebo la aplicacion como localhost (Client-Server en la misma PC) todo va bien.

El problema es cuando lo pruebo en PCs diferentes.

1. Hago el envío y nada, no obtengo errores pero tampoco se envía el archivo.
2. Vuelvo a ejecutar el programa en ambos lados y obtengo un error de que la Direccion ya está en uso (Logico, no estoy cerrando el socket como debe ser), PERO en mi Server aparece el archivo recibido pero en BLANCO.

Otra cosa loca, cierro el Client y abro el server solo y sigo recibiendo el archivo en blanco.

Aqui les coloco el codigo de cliente y server.

Código:
public class FileSender {

public static void main(String[] args) {
// TODO Auto-generated method stub
FileSender nioClient = new FileSender();
SocketChannel socketChannel = nioClient.createChannel();
try {
nioClient.sendFile(socketChannel);
} catch (FileNotFoundException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//SocketChannel socketChannel = nioClient.createc
}
public SocketChannel createChannel(){

SocketChannel socketChannel = null;

try {
socketChannel = SocketChannel.open();
SocketAddress socketAddress = new InetSocketAddress("x.xxx.xxx.x", 10002);
socketChannel.connect(socketAddress);
System.out.println("Connected..Now Sending the File");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return socketChannel;
}

public void sendFile(SocketChannel socketChannel) throws FileNotFoundException, InterruptedException{

RandomAccessFile afile = null;

try {
File file = new File("/home/dionisio/Imágenes/ImagenesOriginalesPrueba/flowers.jpg");
afile = new RandomAccessFile(file, "r");
FileChannel inChannel = afile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(8192);
while (inChannel.read(buffer) != -1) {
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
}
socketChannel.close();
afile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


Server

Código:
public class FileReceiver {

public static void main(String[] args) {
// TODO Auto-generated method stub
FileReceiver nioServer = new FileReceiver();
SocketChannel socketChannel = nioServer.createServerSocketChannel();
nioServer.readFileFromSocket(socketChannel);
}

private SocketChannel createServerSocketChannel() {
// TODO Auto-generated method stub
ServerSocketChannel serverSocketChannel = null;
SocketChannel socketChannel = null;

try {
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(10002));
socketChannel = serverSocketChannel.accept();
System.out.println("Connection Stablished..."+socketChannel.getRemoteAddress());

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return socketChannel;
}


private void readFileFromSocket(SocketChannel socketChannel) {
// TODO Auto-generated method stub

RandomAccessFile afile = null;

try {
afile = new RandomAccessFile("/home/dionisio/Imágenes/imagenesCopiaPrueba/flowersCopia.jpg","rw");
ByteBuffer buffer = ByteBuffer.allocate(8192);
FileChannel fileChannel = afile.getChannel();
while (socketChannel.read(buffer)>0) {
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
}
Thread.sleep(1000);
fileChannel.close();
System.out.println("End of file reached...Closing Channel");
socketChannel.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}


En línea

MNicolas

Desconectado Desconectado

Mensajes: 127


Developer


Ver Perfil WWW
Re: Enviar Recibir Archivo via Socket
« Respuesta #1 en: 21 Enero 2016, 10:46 am »

1. Añade una excepción al Firewall para añadir el puerto utilizado (si usas Windows).
2. Desactiva el Firewall.

Prueba con esas dos opciones.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Enviar y recibir .txt y .jpg por puerto serie con C#
.NET (C#, VB.NET, ASP)
Meta 4 11,294 Último mensaje 4 Diciembre 2009, 15:05 pm
por MANULOMM
Problema extraño al recibir string por socket TCP (ANSI-C + Berkeley sockets)
Programación C/C++
Scott Code 1 2,589 Último mensaje 6 Noviembre 2011, 20:44 pm
por Scott Code
Enviar y Recibir SMS desde la PC con vb.NET
.NET (C#, VB.NET, ASP)
enterariel 4 16,525 Último mensaje 14 Noviembre 2013, 15:39 pm
por WHK
Enviar y recibir información
.NET (C#, VB.NET, ASP)
Meta 0 1,737 Último mensaje 26 Enero 2023, 21:18 pm
por Meta
Enviar y recibir comandos con PowerShell
Scripting
Meta 1 1,820 Último mensaje 21 Febrero 2024, 14:05 pm
por elektrostudios2
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines