Send files through Sockets

how do i send an entire file through a socket...?
thank you....

[http://forums.sun.com/thread.jspa?forumID=11&threadID=390636] this may help you

Similar Messages

  • Sending file through TCP/IP

    Hi all,
    I would like to know whether LabVIEW is able to send file through TCP/IP. Can anyone please enlighten me on this. Thank You
    Rgds 

    Hi taytay,
    Of course it is possible !
    Go to function palette >> Communication >> TCP ; I have never used them but here they are.
    Go to Help >> Find examples.. and search for TCP, you'll find example code
    You can also purchase the "internet toolkit" that provide ready to use FTP VIs to send or copy simple/multiple file(s).
    Hope this helps.
    When my feet touch the ground each morning the devil thinks "bloody hell... He's up again!"

  • Transfering a file through Socket programming

    Hi all,
    I want to return a file from server to client through a socket. I tried using ObjectOutput Stream where in I returned a java.io.File from server. But at client side when I say file.getLeangth() it comes as 0 and if I try to assign FileInputStream on the object it throws an exception as the file not found as the path associated with the file will be of Server.
    So can anyone help as to how to transfer a file through socket programming???
    thx in advance
    MK

    java.io.File is NOT the contents of the file. It really just represents the path.
    If you want to transfer the file's contents, you'll have to use a FileInputStream or FileReader, read from it, and then write the bytes or chars on the wire.

  • Problem with Sending files over Sockets

    Hi. I have a problem when I try to send files over sockets. When I send text files it works well but when I try to send .jpg or other kind of files it doesn't work well because some characters are wrong, I hope you can help me.
    Here is my code:
    //Sender code
    import java.io.*;
    import java.net.*;
    class Servidor{
         Servidor(){
              try{
                   String arch="art.jpg";
                   ServerSocket serv=new ServerSocket(125);
                   Socket socket=serv.accept();
                   System.out.println("Conectado");
                   int c;
                   BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                   FileInputStream fis=new FileInputStream(arch);
                   File f=new File(arch);
                   bw.write(""+f.length());
                   bw.newLine();
                   bw.flush();
                   System.out.println("Escribiendo");
                   while((c=fis.read())!=-1){
                        bw.write(c);
                   bw.flush();
                   fis.close();
                   System.out.println("Terminado");
                   while(true){
              }catch(Exception e){
                   System.out.println(e);
         public static void main(String arg[]){
              new Servidor();
    //Client code
    import java.io.*;
    import java.net.*;
    class Cliente{
         Cliente(){
              try{
                   Socket socket=new Socket("localhost",125);
                   System.out.println("Conectado");
                   BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                   long tam=Long.parseLong(br.readLine());
                   long act=0;
                   FileOutputStream fos=new FileOutputStream("resp.jpg");
                   System.out.println("Recibiendo: "+tam);
                   while(act<tam){
                        fos.write(br.read());
                        act++;
                   System.out.println("Terminado: "+act+" / "+tam);
                   fos.close();
              }catch(Exception e){
                   System.out.println(e);
         public static void main(String arg[]){
              new Cliente();
    }

    I don't think you want BufferedReader and OutputStreamWriter, you want ByteArrayOutputStream and ByteArrayInputStream . The problem is that you are sending jpegs (binary data) as text.

  • I can't send files through bluetooth, but can receive them.

    I can't send files through the bluetooth on my imac 20" any longer...
    I can toggle through all the bluetooth-options though.

    i have exactly the same problem with my macbook, and no solution either...

  • Problem in sending file thru sockets

    Hello
    i am sending a file thru sockets....the file reaches the server but there is loss of file contents. i do not recieve the entire file. whats wrong?
    my Server code
    //package socket_try ;
    // SimpleServer.java: a simple server program
    import java.net.*;
    import java.io.*;
    import javax.comm.*;
    import java.util.*;
    public class Server
    public static final int BUFFER_SIZE = 1024 * 50;
    static CommPortIdentifier portId;
    static Enumeration portList;
    SerialPort serialPort;
    Thread readThread;
    public static void main(String[] ar)
    byte[] buffer;
    buffer = new byte[BUFFER_SIZE];
    int port = 6666; // just a random port. make sure you enter something between 1025 and 65535.
    boolean portFound;
    try
    ServerSocket ss = new ServerSocket(port); // create a server socket and bind it to the above port number.
    System.out.println("Waiting for a client...");
    Socket socket = ss.accept(); // make the server listen for a connection, and let you know when it gets one.
    System.out.println("Got a client :) ... Finally, someone saw me through all the cover!");
    System.out.println();
    BufferedInputStream bin = new BufferedInputStream(socket.getInputStream());
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:\\SMS.txt"));
    int in;
    try
    int len = 0;
              while ((len = bin.read(buffer)) > 0) {
                   bos.write(buffer, 0, len);
                   System.out.print("#");
              bin.close();
              bos.flush();
              bos.close();
              socket.close();
    ss.close();
    catch(Exception x)
    x.printStackTrace();
    catch(Exception e)
    System.out.println("Exception : "+e);
    my client code
    import java.net.*;
    import java.io.*;
    public class Client
    public static final int BUFFER_SIZE = 1024 * 50;
    public static void main(String[] ar)
    int serverPort = 6666; // make sure you give the port number on which the server is listening.
    String address = "192.168.1.3"; // this is the IP address of the server program's computer. // the address given here means "the same computer
    as the client".
    int in=0;
    byte[] byteArray;
    byte[] buffer;
    buffer = new byte[BUFFER_SIZE];
    try
    InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
    System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
    Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port.
    System.out.println("Yes! I just got hold of the program.");
    // Get the input and output streams of the socket, so that you can receive and send data to the client.
    // Just converting them to different streams, so that string handling becomes easier.
    BufferedInputStream bis;
    BufferedOutputStream bos;
    bis = new BufferedInputStream(new FileInputStream("c:\\send.txt"));
    bos = new BufferedOutputStream(socket.getOutputStream());
    byte[] receivedData;
    int len = 0;
    while ((len = bis.read(buffer)) > 0)
    bos.write(buffer, 0, len);
    System.out.print("#");
         for(int y =0 ; y < buffer.length ; y++)
    char s =(char) buffer[y];
    System.out.println("character:"+s);
    bis.close();
    bos.flush();
    bos.close();
    socket.close();
    System.out.println("\nDone!");
    catch(Exception x)
    x.printStackTrace();
    } /* end of main */
    }/* end of class */
    what is wrong can please anyone tell me??????

    As I don't know how long a record is, that's not a very useful answer. How many bytes are you expecting and how many do you actually get?

  • Tansfer files through socket

    Hi all
    Can anyone tell me that how can i send files from server to the client through socket programming.
    if u recommend to send file using
    int read(byte[] b, int offset, int blength)
    void write( byte[] b, int offset, int blength) then how plz tell me how can use these methods to send a receive files??? if u give a small example code then i will very greatful to u.
    Usman

    Thanks for ur help.
    but i have got it. i have designed a client and server. Client requests the server to send a specif file (Client takes the path of file from user). And server sends the file to the client.
    I have checked this program on many files. like *.exe, *.txt, *.dat, *.zip, *.mp3, *.jpg.
    it worked smoothly. I have test to trasfer a file of 700MB and it worked correctly.
    Ooops! i did not check it on Network. i run these program on my own computer. both client and server were running on same computer.
    but i am facing a little problem. that is: the speed of trasfering the files is very slow.
    any1 know how to increase the speed of data transfer over the sockets ???
    And plz tell me how can i trasfer a complete directory just as i trasfered file over the sockets. (Remember: a dircetory may or may not contains may other files and also other directories
    thanks in advance...!!!
    here is the code:
    *          CLIENT            *
    import java.net.*;
    import java.io.*;
    public class TransferClient
        public static void main(String[] args)
            try
                 int fileSize = 0;
                 int readBytes = 0;     
                 final int BUFFER_SIZE = 512;
                 byte[] buffer = new byte[BUFFER_SIZE];       
                   Socket sockClient = new Socket(InetAddress.getLocalHost(),2000);     
                   DataInputStream dataInputStream = new DataInputStream(sockClient.getInputStream());
                   BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("filename.ext"));
                   BufferedReader inputUser   = new BufferedReader(new InputStreamReader(System.in)); //For User input
                   PrintWriter output = new PrintWriter (sockClient.getOutputStream(), true);     //To send the user input to the server
                   //get path from user
                   System.out.print("Enter the complete path of file: ");
                   String path = inputUser.readLine();
                   //send path to server
                   output.println(path);
                   readBytes = dataInputStream.readInt();
                   while(readBytes > 0)
                        readBytes = dataInputStream.read(buffer, 0, readBytes);
                        fileSize = fileSize + readBytes;
                        bufferedOutputStream.write(buffer, 0, readBytes);
                        readBytes = dataInputStream.readInt();
                   }//end while
                   bufferedOutputStream.flush();
                   System.out.println("Received Bytes:  " + fileSize);
                   dataInputStream.close();
                   bufferedOutputStream.close();                              
                   inputUser.close();
                   output.close();
                   sockClient.close();
            }//end try
            catch(IOException ioException)
                 ioException.printStackTrace();
            }//end catch
        }//end main()
    }//end class TransferClient
    *          SERVER            *
    import java.io.*;
    import java.net.*;
    public class TransferServer
        public static void main(String[] args)
             int fileSize = 0;         
             int readBytes = 0;
             final int BUFFER_SIZE = 512;
             byte[] buffer = new byte[BUFFER_SIZE];
             try
                  ServerSocket ss = new ServerSocket(2000);
                  System.out.println("Server Strarts...");
                  Socket sock = ss.accept();
                  BufferedReader clientInput = new BufferedReader(new InputStreamReader (sock.getInputStream()));
                   DataOutputStream dataOutputStream = new DataOutputStream(sock.getOutputStream());
                   PrintWriter output = new PrintWriter(sock.getOutputStream(), true);
                   System.out.println ("Connection Received");          
                   String path = clientInput.readLine();               
                   File f = new File(path);                    
                   if( f.exists() )
                        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(path));               
                        while((readBytes = bufferedInputStream.read(buffer))!= -1)
                             fileSize = fileSize + readBytes;
                             dataOutputStream.writeInt(readBytes);
                             dataOutputStream.write(buffer, 0, readBytes); 
                        }//end while
                        dataOutputStream.writeInt(0);          
                        System.out.println("Sent Bytes:  " + fileSize);
                        bufferedInputStream.close();
                   }//end if
                   clientInput.close();
                   dataOutputStream.close();
             }//end try
             //catch(FileNotFoundException fileNotFoundException)
             //     output.println("Sorry! file does not exist.");
             catch(IOException ioException)
                  ioException.printStackTrace();
             }//end catch       
        }//end main()
    }//end class TransferServer

  • Sending file using sockets (difficult)

    Hi, i have the following section of code
    (of which i am not claiming ownership) which
    implements a DCC Send command from irc.
    It accomplishes this using sockets, and while
    i understand how the code accomplishes what it
    does, i need help with a possible modification.
    Basically what i am looking to do is keep track
    of the progress of the send - so that the
    user can at any time see how much of the file
    has been sent and how much is remaining.
    If anyone has done such a thing in the past or
    has a good idea as to how this should be implemented,
    i would be greatful for their help.
    Here is the code:
    new Thread() {
                public void run() {
                    try {
                        ServerSocket ss = new ServerSocket(0);
                        ss.setSoTimeout(timeout);
                        int port = ss.getLocalPort();
                        //byte[] ip = ss.getInetAddress().getAddress();
                        byte[] ip = InetAddress.getLocalHost().getAddress();
                        long ipNum = 0;
                        long multiplier = 1;
                        for (int i = 3; i >= 0; i--) {
                            int byteVal = (ip[i] + 256) % 256;
                            ipNum += byteVal*multiplier;
                            multiplier *= 256;
                        // Rename the filename so it has no whitespace in it when we send it.
                        // .... I really should do this a bit more nicely at some point ....
                        String safeFilename = file.getName().replace(' ', '_');
                        safeFilename = safeFilename.replace('\t', '_');
                        // Send the message to the user, telling them where to connect to in order to get the file.
                        sendCTCPCommand(nick, "DCC SEND " + safeFilename + " " + ipNum + " " + port + " " + file.length());
                        // The client may now connect to us and download the file.
                        Socket socket = ss.accept();
                        socket.setSoTimeout(30000);
                        // Might as well close the server socket now; it's finished with.
                        ss.close();
                        BufferedOutputStream output = new BufferedOutputStream(socket.getOutputStream());
                        BufferedInputStream input = new BufferedInputStream(socket.getInputStream());
                        BufferedInputStream finput = new BufferedInputStream(new FileInputStream(file));
                        byte[] outBuffer = new byte[bufferSize];
                        byte[] inBuffer = new byte[4];
                        int bytesRead = 0;
                        while ((bytesRead = finput.read(outBuffer, 0, outBuffer.length)) != -1) {
                            output.write(outBuffer, 0, bytesRead);
                            output.flush();
                            input.read(inBuffer, 0, inBuffer.length);
                        output.close();
                        input.close();
                        log("+++ DCC SEND Completed to " + nick + " (" + file.getPath() + ")");
                    }

    You already have the necessary code to find the number of bytes sent at any point during the transmission. You can find out the size of the file by instantiating a RandomAccessFile before you send it, and querying for the file length.
    I suggest you make the file size and bytes sent volatile and conveniently accessible to another thread (best implemented here as an inner class?). Your new thread will need to monitor the file transfer at intervals to update the progress indicator. You can generate estimates of time remaining by measuring the average transmission rate, and extrapolating using the total file size. Classically this is done using an average, but you might be better just maintaining a list of fairly recent samples, allowing for the speed swings inherent in internet connections.
    How you update the progress indicator from your monitor thread is up to you. I suggest exposing methods in the UI for setting the progress and time remaining, and simply update them from the monitor.
    Does this help any?

  • Sending files through lan

    hi
    i want to send text,audio and video files through LAN by using jmf and tcp/ip protocol
    if any body know the answer pls send me the code as early as possible ..pls......it's urgent for me...
    thanks in advance

    Post your question in the iChat disucssions, or just email the files or put them on a server, etc...

  • Sending files through FTP

    task is to take files or folders and ZIP it and send it through FTP.i was able to take the files and folders and zip it ,I was able to connect to FTP server but to send the file what code shoud i use.

    Multipost:
    http://forum.java.sun.com/thread.jspa?threadID=729985

  • I tried to send "File:  / / / " through messages...

    I tried to send "File: / / / " without quotes to a friend through the messages application which obviously crashed it. Now, however, File: / / /  is saved as a draft message and will crash the messages app upon opening it. Is there a way to delete draft messages outside of the app?
    <Edited by Host>

    In the Finder choose the "Go" menu and press Option followed by selecting the Library. Then go to the Messages folder and remove the files that begin with "chat.db" in their names. Then re-open the Messages program.

  • Trouble sending files through ichat after 10.4.7 update

    after the 10.4.7 update (at least it was right around the same time as the update), my ichat stopped being able to send files to my friend. the picture or video would appear to be sending, but the next time i said something, i would be informed that it could not be sent. my friend was still able to send me files, successfuly during this time.
    simultaneously, this would happen with EVERYONE on my buddy list. except one difference: they would get the file, and i would be merely informed that it had failed.
    a new discovery i made today is that with the first friend i mentioned, after a failed attempt, he can no longer see anything i write.
    does anyone have a suggestion as to how to make ichat be able to send files normally again? thanks for your help!

    Hi Jorden,
    Try running the Cron scripts as the first option.
    http://discussions.apple.com/thread.jspa?threadID=122021
    Then try running the combo 10.4.7 update
    http://www.apple.com/support/downloads/macosxupdate1047comboppc.html
    11:03 PM Friday; July 28, 2006

  • I' d like to send files through webservice not using XI

    i am trying p2p connection with Webservice.
    sending message success but files not.
    is any way to send files? someone tell that SOAP attach maybe help me.
    but i don't know how to do.
    please let me know how to use SOAP attach in SAP.
    all your comments make me solve my problem.
    thanks.

    InDesign CS4 can open .indd, .indt  and .inx files from earlier versions, but not from later. It should, however, be able to open .idml files saved or exported from newer versions, with the understanding that unsupported features will be lost, text will probably reflow due to differences in texts engines between versions, and that the greater the gap between versions the more likely there are to be noticeable differences from the original. You will need to be sure CS4 is at version 7.0.6 to read the .idml.

  • Skype send file through internet or internal?

    Today, We have a 2 internal user send a large size file from skype. We find our internal network was very busy. How may I control the skype if send files is using internal network or internet network? Thanks a lot 

    I think that skype as a Point-to-Point will try to find the fastest route.
    Even if used the Internet, then it will load your local network twice:
    1. For the upload to the Internet.
    2. For the download from the Internet.

  • Doesnt iphone have the capability of sending files through bluetooth?

    my bluetooth seems to do NOTHING AT ALL, it keeps trying to find devices and never gets anything, then when i try to send a file from my mac to my iphone it says the device needs more elements and gives me an error, what the **** is going on, can any1 help me out?
    does iphone have the capability of sending pictures youve taken through bluetooth?

    You can try sending feedback here, not that it will do much good > http://www.apple.com/feedback/iphone.html

Maybe you are looking for

  • After kernel 2.6.27, X does not start

    Hi guys! Since I made the much awaited (and sometimes dreaded) pacman -Syu, the latest kernel was installed. Then, I updated ndiswrapper to the latest version (1.53) and catalyst (I actually downgraded that one from 8.9 to 8.8). I then rebooted and d

  • Listing WBS elements and object relations

    Hello Friends, I want to output all wbs-elements and attached objects in a list, but I'm not sure how to do this. Is there a standard report for this? In trans cj03 you can only se the relation between one wbs-element and its related objects... I wan

  • Computer will not install 10.5

    my computer will not install iTunes 10.5.  I have 10.4 currently and it syas that a required file is missing during the 10.5 install and shuts down.  I purchased an iTouch for my daughter and it won't sync with 10.4!  Ugh!!

  • Time loop and events (again)

    Good morning, I still have problems with time loops and events. I have an event structure based on run time menu (two buttons: start stop). When I push start, an automatic sequence is started made of a state machine (so a while loop). I want to stop

  • East US - Websites not available

    I have a number of websites hosted in Azure and I'm currently getting 503 errors from a few of them. The status dashboard shows no issues but these are sites that were working perfectly fine prior to this. East US region. Anybody else having any issu