Hi everyone..
i m developing an application to backup files\directories to a server..
i initially developed it in RMI but then to make it possible to run over Internet i made some modifications and used sockets..
I have designed few packets for sending login details, file information to be backed-up, file data, etc. to server. The server parses the packets, identifies the type of packet and takes action accordingly.
Problem is that the application works fine, throws no exception but the data transfered gets corrupted. This is the case only with data packet.
Other packets; like login packet, file information packet; aint get corrupted. The action taken on receipt of these packets are as desired.
Sometimes the data is not corrupted while most of the time it is..
I test the app by backing up an image file so that its easy to check the backed-up file. I test the application on same PC.
I have created a thread to listen incoming data on client and server side, each.
Right now m testing the app on same PC.
Can anyone guide me what might be wrong with the code?? Any do's or dont's in socket programming in JAVA...???
I m using byte array as packets and DataInputStream and DataOutputStream for reading and writing the packets.
Thank you,
Janvi

Recommended Answers

All 8 Replies

I dont know what the problem is, but instead dataInputStream and dataOutputStream, you can try BufferedInputStream and PrintWriter which I always use in socket programming in java. And a byte[4096] buffer maybe better.

I dont know what the problem is, but instead dataInputStream and dataOutputStream, you can try BufferedInputStream and PrintWriter which I always use in socket programming in java. And a byte[4096] buffer maybe better.

Thank you so much for ur reply..
can u please give an example as to how should i use PrintWriter to write byte array??
I got no methods to write byte array using PrintWriter and i guess converting byte array to String will give undesirable outputs. I read it somewhere and noticed it while backup of image files.
Do you think object serialization has anything to do with it??
Thank you,
Janvi

Thank you so much for ur reply..
can u please give an example as to how should i use PrintWriter to write byte array??
I got no methods to write byte array using PrintWriter and i guess converting byte array to String will give undesirable outputs. I read it somewhere and noticed it while backup of image files.
Do you think object serialization has anything to do with it??
Thank you,
Janvi

................

		private Socket client;
		private BufferedInputStream input;
		private PrintWriter output;
		byte[] buffer = new byte[4096];

...............

                client = new Socket(host,port);
                RandomAccessFile file = new RandomAccessFile(filePath,"rw");

.................
                  try {
				input = new BufferedInputStream(client.getInputStream());
				output = new PrintWriter(client.getOutputStream(),true);
			}
			catch(IOException ioEx) {
				ioEx.printStackTrace();
			}
...................

// read from networkInput and write to file
			try {
				while( (i = input.read(buffer)) != -1) {
					file.write(buffer,0,i);
				}
				logger.println(fileName + " " + client.getInetAddress().getHostAddress() + ":" + client.getPort() );
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					file.close();
					client.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

This code piece is not completed. Just example how to use byte array using BufferedInputStream.

I dont know how you designed this program, how many threads you used. So I cant tell you to use serialization.

Post some code...

You can use byte transfer over any image, text, mp3... files . Moreover you have to use byte transfer.

Thanks for ur reply and for the example on how to use PrintWriter..
The problem is sorted out.. It was due to the packet size.. As i said before i have created a thread for reading the data on client and server each.. the thread on the server side, for some reason, wasn't reading the whole packet. For that reason complete data wasn't written at server side.
The packet size i used initially was 500 KB. Now, the issue is, because of reduction in size of packet, the transfer speed has also reduced.

plz help me "i want to transfer files from single server to many clients using java codings"

Hi everyone..
i m developing an application to backup files\directories to a server..
i initially developed it in RMI but then to make it possible to run over Internet i made some modifications and used sockets..
I have designed few packets for sending login details, file information to be backed-up, file data, etc. to server. The server parses the packets, identifies the type of packet and takes action accordingly.
Problem is that the application works fine, throws no exception but the data transfered gets corrupted. This is the case only with data packet.
Other packets; like login packet, file information packet; aint get corrupted. The action taken on receipt of these packets are as desired.
Sometimes the data is not corrupted while most of the time it is..
I test the app by backing up an image file so that its easy to check the backed-up file. I test the application on same PC.
I have created a thread to listen incoming data on client and server side, each.
Right now m testing the app on same PC.
Can anyone guide me what might be wrong with the code?? Any do's or dont's in socket programming in JAVA...???
I m using byte array as packets and DataInputStream and DataOutputStream for reading and writing the packets.
Thank you,
Janvi

Dear Janvi,

I am looking for something similar, but with some encryption built in and error checking
can you please share this code, which you have developed.

Thanks in advance,

Kindly send the code. I need the code to transfer bulk of files from clinet to server. if you have the code means ,please share it with me. Thanks in advance

Kindly send the code. I need the code to transfer bulk of files from clinet to server. if you have the code means ,please share it with me. Thanks in advance

1.your hijacking a thread (start your own)
2.the thread your hijacking/reviving is from 2 years ago (necroposting)
3.we don't "give" codes as we're not a coding service we only help those who show effort on their side

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.