Results 1 to 4 of 4
  1. #1
    gabriel is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default how to send files through sockets

    I'm developing a program and I need to send files through sockets
    I'm using BufferInputStream,DataInputStream.
    I choose some file from server and then send it to the client.
    The client receives it but it doesn't open!
    I send you my code


    In server I do something like that:
    Java Code:
    public class Server
    {
    static final int Port=5000;
    public int nClients=0;
    private String file="./files/3.jpg";
    /** Creates a new instance of server*/
    public Server() 
    {
    try
    {
    ServerSocket serverSock=new ServerSocket(Port);
    System.out.println( Port);
    while(true)
    { 
    serverSock.accept(); 
    Socket cliente=serverSock.accept();
    nClients++;
    System.out.println(nClients);
    SClient(client,nClients);
    
    
    SocketThread thread=new SocketThread
    (client,file,nClients-1);
    hilo.start();
    }
    }
    catch(Exception ex)
    {
    System.out.println(ex);
    }
    }
    In the client I do that:
    Java Code:
    DataInputStream en = new DataInputStream
    (skClient.getInputStream());
    byte buffer[] = new byte[breaden];
    //breaden=en.read(buffer);
    File rec=new File("./rec/1.jpg");
    rec.setReadable(true);
    rec.setWritable(true);
    //FileOutputStream f=new FileOutputStream(rec);
    FileOutputStream f=new FileOutputStream
    ("./rec/1.jpg");
    f.write(buffer);
    
    skClient.close();

  2. #2
    katie is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

  3. #3
    michaelmarosi is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default Sending files through sockets

    Anyone know where I can access a complete tutorial on how to send files through java sockets? If so, please tell me where, I'd be glad to receive any references. Thanks in advance!!!!:)

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    10

    Default

    Gabriel, try to review this and see where you're going astray. This is something I plan to implement in my custom chat client, when I someday get to it, due to time constraints.

    Best of luck!

    EDIT: See these tutorials found here in our very own community, at the end - which makes note of using ObjectOutputStream and ObjectInputStream.
    Last edited by CaptainMorgan; 01-12-2008 at 09:24 AM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. An echo server using UDP sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:09 PM
  2. Sending Mail Using Sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:05 PM
  3. how to send .jar files client to server
    By gobinathm in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 05:05 AM
  4. Help with Sockets
    By Eric in forum Networking
    Replies: 3
    Last Post: 12-01-2007, 09:09 PM
  5. send files through socket
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-05-2007, 02:08 AM

Posting Permissions

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