LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2006, 08:03 PM   #1
crapodino
Member
 
Registered: Aug 2005
Distribution: SuSE 10
Posts: 72

Rep: Reputation: 15
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:ut | 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
 
Old 02-12-2006, 04:23 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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?
 
Old 02-12-2006, 07:29 AM   #3
crapodino
Member
 
Registered: Aug 2005
Distribution: SuSE 10
Posts: 72

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cant send a whole struct with sockets (c code) alaios Programming 9 04-12-2020 09:52 AM
send/recv binary files with C++ sockets dafatdude Programming 3 02-12-2006 08:02 AM
how can i send a file using linux sockets?? crapodino Programming 2 10-09-2005 11:17 PM
Sockets: multiple send() calls throttle server framerate. JCipriani Programming 3 09-22-2005 07:06 PM
sockets dont ever send data alaios Programming 1 09-21-2005 06:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration