Synchronizing peers in transfer over Socket

Hi again!
I'm relatively new to Java, so there are many issues I still have to figure out ;-)
Here are two of them:
1) How can peers be synchronized in transfer over Socket? For example:
This is the receiving peer:
DataInputStream dsin = new DataInputStream(socket.getInputStream());
String s = dsin.readUTF();This is the sending peer:
DataOutputStream dsout = new DataOutputStream(socket.getOutputStream());
dsout.writeUTF("something...");
dsout.close();At the receiving peer in some cases the EOFException or SocketException (Socket closed) are raised. I suspect this happens either because the receiver tries to read input before it is fully transfered or when the sender already managed to close the socket's OutputStream.
So how can such a transfer be synchronized?
2) What if both peers close their socket's OutputStream? Does this result in closing the connection and releasing all its resources, such as port?
Thank you everyone!

this is what I learnt:Sorry, try again!
DO NOT close the Socket on any endpoint until you make sure that the other endpoint managed to call socket.getInputStream().This 'rule' is quite unnecessary.
According to the documentation, getInputStream() may return a stream which buffer was discarded by network software if a broken connection was detected.That's a complete misreading and misquoting of the documentation. A reset can happen to the connection at any time; can cause loss of data from te socket receive buffer; and has nothing to do with the operation of getting the input stream. Getting the input stream of a socket has nothing to do with the network or the state of the connection. It just constructs a Java object. So this rule is quite pointless.
Even more, it seems to be a good practice not to close the Socket until you are sure that the other endpoint will not try to read from the channel.You've got that back to front as well. THink about that some more and you will see it involves an infinite regression.
The only rules you need are these:
(a) don't close the socket until you're sure the other end has finished writing. You have to close the socket while the other end is still reading, so that it will get a EOS indication, so it will know you've gone away, so it will stop writing and close its end of the socket.
(b) closing the socket or either of its streams closes the other two items. You should always close the topmost output stream of a socket and nothing else.

Similar Messages

  • Folder transfer over sockets!

    How can I transfer folders over the socket?
    Also in File transmission is it required to use BufferedInputStream or Output/InputStream alone can do the work?

    rameshraj wrote:
    How can I transfer folders over the socket?
    Also in File transmission is it required to use BufferedInputStream or Output/InputStream alone can do the work?Conceptually yes.
    Technically no.
    Sockets send data.
    A 'folder' represents a resource on the computer. Thus you can no more send that than you can send your physical hard drive.
    So conceptually if you wanted to simulate a copy.
    1. Design a data structure that allows you to encapsulate the folder structure (folder names, file names, file dates, file contents perhaps file permisions.)
    2. Create an API that allows you to create that data structure (fill it in) when pointed to a folder.
    3. Create an API that sends that data structure via a socket.
    4. Create an API that receives that data structure via a socket.
    5. Create an API that takes a data structure and create folders, files, etc from it.
    Note that 2-5 are code but 1 is not. And you do not start untile 1 is complete.
    Or as an alternative to the above learn how FTP works and use that instead.

  • Urgently needed(data transfer on sockets)

    hello friends,
    I m in need of java code to read text and graphics(lines,circles etc.)from JTextPane and then to transfer this data on sockets to a client.
    IF anybody can help,I'll be highly thankful.I need it urgently.

    i did the follwing when i had a similar requirement...i used java.util.vectors and passed them over sockets to the other end. the advantage of doing this was that I inserted a string ('string_msg') if any text messages were sent or ('graph_pts') if Point objects of the circle or line was sent, as the first element of the vector and the next element was the text-object(StringBuffer) / object[](incase of Point objects) itself.
    here is the piece of code to send the vector...
    try {
                ucon = (new URL(serverURL, serverPathStr)).openConnection();
                ucon = setUcon(ucon);
                objectoutputstream = new ObjectOutputStream(new BufferedOutputStream(ucon.getOutputStream()));
                objectoutputstream.writeObject(obj);
                objectoutputstream.flush();
                objectinputstream = new ObjectInputStream(new BufferedInputStream(ucon.getInputStream()));
                obj1 = objectinputstream.readObject();
                objectinputstream.close();
                objectoutputstream.close();
            catch....in the case above, the server simply returns a string "OK_recd" which is recd as a sent confirmation via obj1.
    Note: the server &/ client should also use ObjectInput/Output Streams to recieve the object and type cast the object recd to Vector.
    the first element in the vector (used as a handshake) to identify the next element in the vector...
    hope this helps!

  • I am going to buy a Mac desktop and want to know how to transfer all my purchases and uploaded music from my PC. How can I do this to ensure all videos and music transfer over and not just purchases?

    I am going to buy a Mac desktop and want to know how to transfer all my purchases and uploaded music from my PC. I originally started using iTunes on my PC so it has everything I have ever purchased or uploaded from my own collection of music.
    How can I do this to ensure all videos and music transfer over and not just purchases? Do they do it at the Apple Store or at Best Buy? help!

    Move iTunes Library from PC to MAC
    http://www.macworld.com/article/146958/2010/03/move_itunes_windows_mac.html
    Also, See here if you need to...  iTunes to an External Drive
    http://support.apple.com/kb/HT1751

  • HT204053 I recently changed ny Apple ID but it did not transfer over to my iCloud account. How do I change my iCloud ID? The login popup for iCloud will not go away so I cannot get into my iPad since it pops up as soon as I turn the device on.

    I recently changed my Apple ID but it did not transfer over to my iCloud account. How do I change my iCloud ID? The password for the new Apple ID does not work since the iCloud default user name is imbedded in the login box. The login popup for iCloud will not go away so I cannot get into my iPad since it pops up as soon as I turn the device on and re-occurs every few seconds.

    You have to go to Settings>iCloud, tap Delete Account, provide the password for the old ID when prompted to turn off Find My iPhone, then sign back in with the ID you wish to use.  If you don't know the password for your old ID, or if it isn't accepted, go to https://appleid.apple.com, click Manage my Apple ID and sign in with your current iCloud ID.  Click edit next to the primary email account, change it back to your old email address and save the change.  Then edit the name of the account to change it back to your old email address.  You can now use your current password to turn off Find My iPad on your device, even though it prompts you for the password for your old account ID. Then go to Settings>iCloud, tap Delete Account and choose Delete from My iPad when prompted (your iCloud data will still be in iCloud).  Next, go back to https://appleid.apple.com and change your primary email address and iCloud ID name back to the way it was.  Now you can go to Settings>iCloud and sign in with your current iCloud ID and password.

  • When upgrading to OSX Yosemite do all my programs and files transfer over such as Office for mac, saved documents from word,excel,pics,pdf files,etc.etc...

    Will all my files transfer over (or stay) automatically right after upgrading to Yosemite or do I have to backup every single desired program or document? Question is do I HAVE to please if you can answer if its mandatory or not. Not looking for recommendations that I SHOULD back up or anything like that just want to know if it is mandatory to perform a backup to be able to keep or transfer all my current programs and apps and documents currently on my MB Pro Retina.

    Yes, upgrading to Yosemite will not touch your 3rd party apps or files in the Home folder.
    Although not mandatory you should have a a backup of your hard drive regardless if you upgrade to Yosemite or not.  To not have a backup is just playing  Russian roulette as there are only two types of hard drives;  those that have failed and those that will.

  • My files, photos, email files, software programs, and all information did NOT transfer over to Mavericks (from Mt. Lion).  How do I get all my stuff to appear when using Mavericks?

    Will someone please help me?
    My files, photos, email files, software programs, calendar info, and all information did NOT transfer over to Mavericks (from Mt Lion).  How do I get all my stuff to appear when using Mavericks?
    My email did transfer over.
    Thank you.

    First click on the Apple > Logout and Log back in or restart the unit see if that solves it.  If it doesn't do you have a backup of your system prior to upgrade?  Check Disk Utility and see how much space is being used to make sure the drive wasn't somehow erased.

  • Help Please.  I am setting up my macbook pro for a second user but office didn't transfer over.  Is there a way to have additional users on the same comp have office available without having to re-install it for each user?

    Help Please.  I am setting up my macbook pro for a second user but office didn't transfer over.  Is there a way to have additional users on the same comp have office available without having to re-install it for each user?

    mpr130 wrote:
    Help Please.  I am setting up my macbook pro for a second user but office didn't transfer over. Is there a way to have additional users on the same comp have office available without having to re-install it for each user?
    How did you attemp to transfer Office?
    OS X Lion: Set up a guest account

  • I bought a new mac and my music in itunes didn't transfer over from my old cpu.  Most of it was put on with cd's I have. Can I transfer the music from my I-phone to itunes on my mac?

    I bought a new mac and my music in itunes didn't transfer over from my old cpu.  Most of it was put on with cd's I have. Can I transfer the music from my I-phone to itunes on my mac?

    It sounds like there's something amiss with either your ID or the new system you're using.  I've been using iTM on a Win7 and OSX SL system and have been able to upload and download to the Cloud on both.  I've been using them interchangeably for the last week or so.
    I would also recommennd giving Apple support a call, to see what they say.

  • HT1386 The first time I synced my iphone with my mac, I didn't realize that all of my photos from iphoto would transfer over to the phone.   Now, I need to remove some, as they are taking up too much space.  I cannot figure out how to remove them from the

    The first time I synced my iphone 4 with my mac, I didn't realize that all of my photos from the iphoto library would transfer over to the phone (more than 3,000).   Now, I need to remove some, as they are taking up too much space.  I cannot figure out how to remove them from the phone.  I tried to uncheck boxes and sync again, but I get a message that there is no room on the iphone.  I've read as many articles as I can find, but still cannot manage this.  Thanks for any help.

    Open itunes, connect iphone, select what you want, sync

  • File transfer via socket problem (with source code)

    I have a problem with this simple client/server filetransfer program. When I run it, only half the file is transfered via the net? Can someone point out to me why this is happening?
    Client:
    import java.net.*;
    import java.io.*;
         public class Client {
         public static void main(String[] args) {
              // IPadressen til server
              String host = "127.0.0.1";
              int port = 4545;
              //Lager streams
              BufferedInputStream fileIn = null;
              BufferedOutputStream out = null;
              // Henter filen vi vil sende over sockets
              File file = new File("c:\\temp\\fileiwanttosend.jpg");
    // Sjekker om filen fins
              if (!file.exists()) {
              System.out.println("File doesn't exist");
              System.exit(0);
              try{
              // Lager ny InputStream
              fileIn = new BufferedInputStream(new FileInputStream(file));
              }catch(IOException eee) {System.out.println("Problem, kunne ikke lage fil"); }
              try{
              // Lager InetAddress
              InetAddress adressen = InetAddress.getByName(host);
              try{
              System.out.println("- Lager Socket..........");
              // �pner socket
              Socket s = new Socket(adressen, port);
              System.out.println("- Socket klar.........");
              // Setter outputstream til socket
              out = new BufferedOutputStream(s.getOutputStream());
              // Leser buffer inn i tabell
              byte[] buffer = new byte[1024];
              int numRead;
              while( (numRead = fileIn.read(buffer)) >= 0) {
              // Skriver bytes til OutputStream fra 0 til totalt antall bytes
              System.out.println("Copies "+fileIn.read(buffer)+" bytes");
              out.write(buffer, 0, numRead);
              // Flush - sender fil
              out.flush();
              // Lukker OutputStream
              out.close();
              // Lukker InputStrean
              fileIn.close();
              // Lukker Socket
              s.close();
              System.out.println("File is sent to: "+host);
              catch (IOException e) {
              }catch(UnknownHostException e) {
              System.err.println(e);
    Server:
    import java.net.*;
    import java.io.*;
    public class Server {
         public static void main(String[] args) {
    // Portnummer m� v�re det samme p� b�de klient og server
    int port = 4545;
         try{
              // Lager en ServerSocket
              ServerSocket server = new ServerSocket(port);
              // Lager to socketforbindelser
              Socket forbindelse1 = null;
              Socket forbindelse2 = null;
         while (true) {
         try{
              forbindelse1 = server.accept();
              System.out.println("Server accepts");
              BufferedInputStream inn = new BufferedInputStream(forbindelse1.getInputStream());
              BufferedOutputStream ut = new BufferedOutputStream(new FileOutputStream(new File("c:\\temp\\file.jpg")));
              byte[] buff = new byte[1024];
              int readMe;
              while( (readMe = inn.read(buff)) >= 0) {
              // Skriver filen til disken ut fra input stream
              ut.write(buff, 0, readMe);
              // Lukker ServerSocket
              forbindelse1.close();
              // Lukker InputStream
              inn.close();
              // flush - sender data ut p� nettet
              ut.flush();
              // Lukker OutputStream
              ut.close();
              System.out.println("File received");
              }catch(IOException e) {System.out.println("En feil har skjedd: " + e.getMessage());}
              finally {
                   try {
                   if (forbindelse1 != null) forbindelse1.close();
                   }catch(IOException e) {}
    }catch(IOException e) {
    System.err.println(e);
    }

    Hi!!
    The problem is at this line:
    System.out.println("Copies "+fileIn.read(buffer)+" bytes");
    Just comment out this line of code and see the difference. This line of code causes another "read" statement to be executed, but you are writing only the data read from the first read statement which causes approximately half the file to be send to the server.

  • I am running aperture 2.1.4 and am considering purchasing the latest version. will my photos, projects, and folders etc. transfer over automatically ?

    I am running aperture 2.1.4 and am considering purchasing the latest version. will my photos, projects, and folders etc. transfer over automatically ?

    will my photos, projects, and folders etc. transfer over automatically ?
    Nearly automatically.
    This will be a major upgrade, and Aperture will have to upgrade your library.
    As a precaution, make sure you have a working backup of your library, and I'd keep a copy of the Aperture 2.1.4 Application installer as well.
    Also it is recommended to repair the permissions and to rebuild the library, to make sure that  the library has no corruptions before you upgrade.
    Rename the Aperture Library to a name different from the default name before you upgrade.
    Launch the new Aperture version by double clicking your Aperture library. Aperture will prompt you, that it is necessary to upgrade the library; let it run, this may take a while. And yes, you should find all your photos, projects, folders, etc. after the upgrade just as you imported and organized them.
    Regards
    Léonie

  • Macintosh transfer over the presets and preferences WITHOUT transferring the ENTIRE HD with "target disc mode

    I'm picking up the new 2014 MBP tomorrow so should be installing over the weekend / next week.
    I've been currently using a 2011 MBP with OS 10.7.5, using the "home" folder, which I will NOT be doing on the new one MBP with Mavericks.
    What is the best way to transfer over the presets and preferences I want, WITHOUT transferring the ENTIRE HD with "target disc mode" or something similar? I am FRESH installing all  APPLICATIONS.
    Mainly it's the presets and preferences I want to transfer.
    Thanks in advance.

    I'm picking up the new 2014 MBP tomorrow so should be installing over the weekend / next week.
    I've been currently using a 2011 MBP with OS 10.7.5, using the "home" folder, which I will NOT be doing on the new one MBP with Mavericks.
    What is the best way to transfer over the presets and preferences I want, WITHOUT transferring the ENTIRE HD with "target disc mode" or something similar? I am FRESH installing all  APPLICATIONS.
    Mainly it's the presets and preferences I want to transfer.
    Thanks in advance.

  • You sell an older iphone; will apps transfer over to a new user?

    You sell your iphone 4; to someone; who presumably registers it as their own; will the 300 apps you have on it transfer over to the new user?

    Yes, if you set up the new phone to the same Apple ID and iTunes account you used to purchase the apps.  Your content is tied to your account and Apple ID, not the device.
    The easiest thing to do is to back up the old phone before you sell it.  When you first connect the new phone to iTunes, choose to restore from backup.  You new phone will be nearly the same as your old phone (it may not put everything on there perfectly - I've had some folders messed up and had to re-organize some apps, but nothing really major).

  • IGoogle settings did not transfer over from IE 8 to FF4.

    Hmm...
    I had an iGoogle page in IE 8, but the settings did not transfer over to a new install of FF 4?
    What might be the problem and can stillI migrate those setting to FF 4?
    Thanks.
    P.S. It worked fine on another system with the same configs. Strange.

    Playlists are part of the iTunes Library.itl file, so if you just moved the music folder, your playlists would not have transferred. This site might be of help:
    http://lifehacker.com/software/itunes/geek-to-live--how-to-move-an-itunes-librar y-from-a-pc-to-mac-and-back-242468.php

Maybe you are looking for

  • Different issues on Satellite L450D.

    Hi, I bought this laptop a couple of months ago and im having major problems! First of all my first problem was that when i first tried to activate my mcafee trial it said it had expired even though i only started up the laptop! But this problem didn

  • Separating out a child's music library in iTunes

    Forgive my ignorance, but I've never mastered iTunes music. Now I've complicated things by giving my child an old iPhone.  She's purchasing music and I really don't want it mixed up with mine.  Two questions: (1) Is there a way to remove her music fr

  • IPad and iTunes taking forever to sync out of the blue

    My iPad synced in 'normal' time, just under 10 minutes, yesterday. Today, the syncing is taking forever! Appears that a completely new backup was done, and now it barely moves. I did not download anything to iPad between these syncs. I used it, charg

  • I don't know where cs6 disappeared to. It is no longer on my computer.

    Please advise as to where my cs6 went to on my macbook pro. LIghtroom 4 did the same thing, but I found it in applications. I can't find cs6 there. Any help is appreciated.

  • How do I create a feathered square similar to Illustrator?

    I am coming to this with good familiarity in Illustrator. I am learning Photoshop. After I use the rectangle tool initially I am then not able to edit its size. Also, it seems to become a mask, which is an element I don't really understand. When I ap