Foro de elhacker.net

Programación => Java => Mensaje iniciado por: spysecurityca en 21 Enero 2016, 01:38 am



Título: Enviar Recibir Archivo via Socket
Publicado por: spysecurityca 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();
}


}


}


Título: Re: Enviar Recibir Archivo via Socket
Publicado por: MNicolas 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.