LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   can't send files with sockets (https://www.linuxquestions.org/questions/programming-9/cant-send-files-with-sockets-414351/)

crapodino 02-11-2006 08:03 PM

can't send files with sockets
 
hi! I need to send files from a java client to a c++ server. What i do is: I divide the size of the files into blocks. For example if the client wants to send a file of 100 KB he to this:
1º que sends the size of the block (an int). For examplee 30 KB
2º que sends the amount of bloacks ( 3 )
3º the bytes that were not send ( 10 )
4º sends all blocks one each time
5º finish

The problem is this: the server receive the file, i mean, the size of the file is perfect but i can't open it. for exampleif its a JPG when you open it, it is all black. What can be happening ?? Please, i really need to solve this. I use kdevelop in suse 10.

This client code is this:
Code:

import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.StringTokenizer;

public class ejemplo {



        public static void main (String[] args){

        Enviar_Archivo(Abrir_Archivo());

        }

        public static File Abrir_Archivo() {

                String cadena="",aux;

                File archivo = new File( "/home/mariano/000_2386.jpg" );

                if ( !archivo.exists() || !archivo.canRead()) {
                            System.out.println("Can't read " + archivo);

                }
                return archivo;



        }



          public static void Enviar_Archivo(File archivo){



        try {
                FileInputStream file = new FileInputStream(archivo);
                BufferedInputStream buffer = new BufferedInputStream (file);
                //DataInputStream input = new DataInputStream (buffer);
                //System.out.println(archivo.length());

                /*FileReader reader= new FileReader(archivo);
                BufferedReader buffer = new BufferedReader (input);

                while ((aux=buffer.readLine())!=null)
                        cadena=cadena+'\n'+aux;*/


                Sock socket = new Sock();
                socket.iniciar((short) 1400,"127.0.0.1");
                socket.CrearESSocket();

                int bloque=1024;
                int total = file.available( );
                int veces=total / bloque;
                int resto=total % bloque;
                byte  data [] = new byte [ bloque ];


                System.out.println("Quedan por enviar:");
                System.out.println(resto);
                System.out.println("tam bloque: ");
                System.out.println(bloque);
                System.out.println("cant bloques:");
                System.out.println(veces);
                //int got = input.available();
                //socket.enviar("hollaaado");
                /*socket.*/enviar(socket,bloque);
                /*socket.*/enviar(socket,veces);

                /*System.out.println("Quedan por enviar:");
                System.out.println(resto);
                resto = 9;*/
                        /*socket.*/enviar(socket,resto);

                int k=0;
                while ( total > bloque ) {
                        k=k+1;
                        //int leidos=buffer.read( data );
                          total=total-bloque;
                        socket.enviar(buffer.read(data));
                        //System.out.println(leidos);
                }
                System.out.println(k);
                if (total > 0){
                //for (long i=0;i<99999999;i++);
                System.out.println("sisis");
                        /*socket.*///enviar(socket,total);
                enviar(socket,1000);
                System.out.println("Quedan por enviar:");
                System.out.println(total);
                        socket.enviar(buffer.read(data));
                        /*int leidos=buffer.read(data);
                        System.out.println(leidos);*/
                        System.out.println("Se leyo todo el buffer");
                }



        }

        catch(FileNotFoundException excepcion){
                System.out.println("Archivo no encontrado"+excepcion);
                  //return null;
        }

        catch (IOException excepcion) {
                System.out.println("Error"+excepcion);
                //return null;


        }
  }


static public void enviar(Sock s, int b){
byte auxInt[]=new byte[4];
auxInt=toByteArray(b);
byte[] buff=new byte[4];
int i=0;
for(i=0;i<4;i++) buff[i]=auxInt[i];
s.enviar(buff); //Está en Sock.java
}
//////////////////////////////////////////////
static private byte[] toByteArray(int foo) {
byte array[]=new byte[4];
 for (int iInd = 0; iInd < 4; ++iInd) {
    array[iInd] = (byte) ((foo >> (iInd*8)) & 0xFF); 
}
  return darVuelta(array);}
////////////////////////////////
static private byte[] darVuelta(byte[] aux){
byte buff[]=new byte[4];
int i,j;
for(i=0,j=3;i<4;i++,j--) buff[i]=aux[j];
return buff;
}
////////////////////////////////


}





and the server:

int Transmisor::recibirArch(char* archNuevo,char* archTexto, int socket) {
/**Se encarga de recibir un archivo desde destinatario*/
int resp;

cout<<"archNUevo: "<<archNuevo<<endl;
cout<<"socket: "<<socket<<endl;

ofstream out(archNuevo, ios::out | ios::binary | ios::trunc);
int cant_bloques,tam_bloque,i,cant_restantes;


char buf[MAX_TAM_BUFFER];

bzero(buf,MAX_TAM_BUFFER);
if(Socket::recibir(socket,buf,4)==-1) perror("Error");
tam_bloque=0;
memcpy(&tam_bloque,buf,4);
tam_bloque=ntohl(tam_bloque);
cout <<"tam bloquue: "<<tam_bloque<<endl;

bzero(buf,MAX_TAM_BUFFER);
if(Socket::recibir(socket,buf,4)==-1) perror("Error");
cant_bloques=0;
memcpy(&cant_bloques,buf,4);
cant_bloques=ntohl(cant_bloques);
cout <<"cant_bloques: "<<cant_bloques<<endl;


bzero(buf,MAX_TAM_BUFFER);
if(Socket::recibir(socket,buf,4)==-1) perror("Error");
cant_restantes=0;
memcpy(&cant_restantes,buf,4);
cant_restantes=ntohl(cant_restantes);
cout <<"cant_restantes: "<<cant_restantes<<endl;


char buffer[tam_bloque];

for(i=0; i<cant_bloques; i++) {
bzero(buffer,tam_bloque);
Socket::recibir(socket,buffer,tam_bloque);
out.write(buffer,tam_bloque);
}
cout<<"okok,I: "<<i<<endl;




char buffer2[cant_restantes];
bzero(buffer2,cant_restantes);
Socket::recibir(socket,buffer2,cant_restantes);

out.write(buffer2,cant_restantes);
out.flush();
out.close();

/*una vez que recibi bien el arch llamo a un metodo para que me lop convuierta a
arch de texto plano*/
resp = this->convertirATexto(archNuevo,archTexto);
return resp;
}




lots of thanks

graemef 02-12-2006 04:23 AM

Why are you breaking the file down?
Are you using raw sockets, if so do you have any error checking, why not use tcp which has some error checking ad flow control?

I'd suggest that you send the file as a whole. See if that works then investigate the other requirements.

As for your problem why not do a diff between the two files?

crapodino 02-12-2006 07:29 AM

i use tcp sockets. the second file is because y need to convert the original file to a txt file. But i need to store both of them.

I heard that tcp/ip doesn't let you send more than 64KB in one send(). I mean, you can send more but in more sends().

is this ok ??

thanks


All times are GMT -5. The time now is 03:48 PM.