Results 1 to 4 of 4
  1. #1
    rameshraj is offline Member
    Join Date
    Dec 2007
    Posts
    42
    Rep Power
    0

    Thumbs up 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 07:52 AM.

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    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 @ [www.littletutorials.com]
    Language is froth on the surface of thought

  3. #3
    rameshraj is offline Member
    Join Date
    Dec 2007
    Posts
    42
    Rep Power
    0

    Thumbs up

    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).

  4. #4
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    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 @ [www.littletutorials.com]
    Language is froth on the surface of thought

Similar Threads

  1. To transfer a file from client to server
    By phani in forum Networking
    Replies: 4
    Last Post: 10-12-2010, 06:15 PM
  2. Sending Mail Using Sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:05 PM
  3. Help with Sockets
    By Eric in forum Networking
    Replies: 3
    Last Post: 12-01-2007, 08:09 PM
  4. Replies: 7
    Last Post: 08-12-2007, 03:47 PM
  5. Problems sending file throught TCP sockets
    By Nite in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2007, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •