Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-19-2008, 12:53 PM
Member
 
Join Date: Dec 2007
Posts: 38
rameshraj is on a distinguished road
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();
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-27-2008, 10:51 AM
M77's Avatar
M77 M77 is offline
Member
 
Join Date: May 2008
Location: LV
Posts: 35
M77 is on a distinguished road
Send a message via Skype™ to M77
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();
}
}
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-30-2008, 11:18 PM
Member
 
Join Date: Jul 2007
Posts: 14
vglass is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending Mail Using Sockets Java Tip java.net 0 04-07-2008 09:05 PM
Sending files from Servlet JavaForums Java Blogs 0 01-15-2008 06:11 PM
how to send files through sockets gabriel Advanced Java 3 01-12-2008 09:10 AM
sending jar files from client to server? gobinathm New To Java 2 11-13-2007 06:12 AM
Problems sending file throught TCP sockets Nite Advanced Java 2 08-04-2007 10:01 PM


All times are GMT +3. The time now is 06:36 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org