Results 1 to 3 of 3
Thread: Sending files over sockets!
- 05-19-2008, 11:53 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 42
- Rep Power
- 0
Sending files over sockets!
While sending files over sockets,it is ok for small files like text and java.When some larger files like .pdf,.wmv..mp3 etc are sent then the file received is corrupted on the receiving end and the file size is also greater than the sent one.Also the receiving end is waiting infinitely to write.
Actually what might have been happening?Can anyone give me some idea?
The receiving end has the receiving code as shown below:
InputStream in = clientSocket.getInputStream();
byte[] buf = new byte[Integer.parseInt(headers[2])];//allocate the size as //the number of bytes available on the stream
//to get the filename and file extensions
String []fileExtension=headers[1].split("\\.");//split on basis of dot to get the
//file extension
//the last content of the filedetails array will be the extension
String f2=headers[1];//+"."+fileExtension[fileExtension.length-1];
//or
f2="E:/E-resources/java/Transfer."+fileExtension[fileExtension.length-1];
//delete already existing file with same name
if(new File(f2).exists())
{
//new File(f2).delete();
File file=new File(f2);
System.out.println("File Deleted\t"+file.delete());
}
System.out.println("Writing as a file\t"+f2);
FileOutputStream fos = new FileOutputStream(new File(f2));
//int size=in.available();
while(in.read(buf)>=0)
{
fos.write(buf);
System.out.println("Input streams available are\t"+in.read(buf));
}
fos.close();//close the fileoutputStream finally
in.close();
- 05-27-2008, 09:51 AM #2
U will need at least 3 classes: Server that listens port for connection and creates ServerConnections when clients sends files. And Client. Here is the core of these clasees:
import java.net.*;
import java.io.*;
public class Server implements Runnable {
public static void main(String[] args) {
new Thread(new ServerLogger(out)).start();
}
private Server() {
conCount=0;
}
public void run() {
try {
ServerSocket acceptSocket=new ServerSocket(Config.port);
while (true) {
try {
Socket sock=acceptSocket.accept();
new ServerConnection(this, sock);
} catch(Exception ex) {
ex.printStackTrace();
}
}
} catch(Exception e) {
if (!e.toString().startsWith("java.net.SocketExceptio n")) {
e.printStackTrace();
}
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;
public class ServerConnection implements Runnable {
private BufferedReader in;
public ServerConnection(Socket sock) {
try {
this.serverLogger=serverLogger;
sock.setSoTimeout(30000);
in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
new Thread(this).start();
} catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
String s=in.readLine();
if (s != null) {
System.out.println(s);
} else {
return;
}
} catch(Exception e) {
if ((!e.toString().startsWith("java.net.SocketExcepti on")) &&
(!(e instanceof SocketTimeoutException))) {
e.printStackTrace();
} else {
return;
}
}
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class Client implements {
private PrintWriter out;
public static void main(String[] args) {
new Logger();
}
public Client() {
try {
Socket s=new Socket(Config.serversite, Config.serverport);
out=new PrintWriter(s.getOutputStream(), true);
//write file to out here
} catch(Exception e) {
e.printStackTrace();
}
}
}
- 05-30-2008, 10:18 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 14
- Rep Power
- 0
In your code you have a comment that says:
"the number of bytes available on the stream"
How exactly are you determining the number of bytes available? The available() method is known to be unreliable, so this may be part of the reason your data is getting corrupted.
Also, you may want to avoid using Reader/Writer classes. If I remember correctly these are meant for character data and can corrupt file transfers.
Similar Threads
-
Sending Mail Using Sockets
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:05 PM -
how to send files through sockets
By gabriel in forum Advanced JavaReplies: 3Last Post: 01-12-2008, 08:10 AM -
sending jar files from client to server?
By gobinathm in forum New To JavaReplies: 2Last Post: 11-13-2007, 05:12 AM -
Problems sending file throught TCP sockets
By Nite in forum Advanced JavaReplies: 2Last Post: 08-04-2007, 09:01 PM


LinkBack URL
About LinkBacks

Bookmarks