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-14-2008, 08:49 AM
Member
 
Join Date: Dec 2007
Posts: 38
rameshraj is on a distinguished road
File and Message transfer over sockets!
I am developing a chat application in JAVA and need to transfer the files from one user to another.I have successfully sent the messages from one user to another via the sockets.Now I want to transfer the file also using the same sockets.The problem is how the user side distinguishes whether s/he has received the message or the file.
Is there any other effective technique to do the same thing?
Also how can I use the drag and drop over the Swing GUI so that the user can simply drag the file to be sent over the socket on the GUI and then send the file

Last edited by rameshraj : 05-14-2008 at 08:52 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-14-2008, 09:11 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
You have to implement your own "protocol" for doing this. you can also maintain connections open: one for commands and one for data. This is how FTP does it
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-14-2008, 09:33 AM
Member
 
Join Date: Dec 2007
Posts: 38
rameshraj is on a distinguished road
public static void main(String[] args) throws Exception {
Thread serverthread = new Thread(new Runnable(){
public void run(){
ServerSocket ss = null;
try{

ss = new ServerSocket(44444);

Socket s = ss.accept();

OutputStream out = s.getOutputStream();
FileInputStream fin = new FileInputStream("Test.txt");
byte[] buf = new byte[1024];
int read;
while( (read=fin.read(buf)) != -1) {
out.write(buf, 0, read);

}..................................




I am using the above concept to read the file in a Stream and sending it to the OutputStream.
On the other side I am receiving similarly in a loop and saving it to a file:

String f2="E:/E-resources/java/Test.txt";
FileOutputStream fos = new FileOutputStream(new File(f2));

while(in.read(buf)!=-1)
{
fos.write(buf);
System.out.println("Writing to the file is\t"+buf[29]+".."+fos.getChannel().size());
}
.........................


Using this technique how can I distinguish the two(ordinary message and File Transfers).
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-14-2008, 09:13 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
As I said, you can use to channels, one for commands and one for data. Each command can have a unique ID generated on the server. The corresponding data will have the same ID. In order to do this you have to implements some protocol kind of protocol. Read a document like this RFC 959 (rfc959) - File Transfer Protocol to understand hot things are done usually.
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
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
To transfer a file from client to server phani Networking 1 04-16-2008 02:39 AM
Sending Mail Using Sockets Java Tip java.net 0 04-07-2008 09:05 PM
Help with Sockets Eric Networking 3 12-01-2007 09:09 PM
Help with error message when running JAR via HTML file Simmy AWT / Swing 7 08-12-2007 04:47 PM
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 05:59 AM.


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