11 Replies Latest reply: Jan 4, 2008 8:05 PM by EJP RSS

    read/write a string in a socket

    807603
      hi everyone!
      I d like to write and read a String such as ("YOU WON: MAGIC NUMBER IS"+ num); through a socket.
      Can i use a method of DataInputStream-DataOutputStream or i have to use a method of other class?

      thanks
        • 1. Re: read/write a string in a socket
          807603
          dimitri_ath wrote:
          Can i use a method of DataInputStream-DataOutputStream
          Yes
          • 2. Re: read/write a string in a socket
            807603
            i am using writeChars() but what is the corresponding method of DataInputStream? API refers to a method
            readChar().
            • 3. Re: read/write a string in a socket
              807603
              dimitri_ath wrote:
              i am using writeChars() but what is the corresponding method of DataInputStream? API refers to a method
              readChar().
              The trouble with writeChars is that there is no indication in the stream of the number of chars sent. Consider writeUTF/readUTF, or using a Writer subclass and a Reader subclass instead, since your transmission is text.
              • 4. Re: read/write a string in a socket
                796440
                If you want to send a String across a socket, you could either use DataOutputStream.writeObject(yourString), or you could use wrap a BufferedWriter or PrintWriter around the socket's OutputStream and use the appropriate write or print method. The corresponding read mecahism for each of these should be obvious if you think about it a little and do a little research in the java API docs.
                • 5. Re: read/write a string in a socket
                  807603
                  You can do one of two things.

                  You can write the length as an int or short and then write the chars and on the other side read the length and then read that number of chars

                  or...

                  The easy way. Use the readUTF and writeUTF methods
                  • 6. Re: read/write a string in a socket
                    807603
                    jverd wrote:
                    If you want to send a String across a socket, you could either use DataOutputStream.writeObject(yourString), or you could use wrap a BufferedWriter or PrintWriter around the socket's OutputStream
                    Something to note.

                    ejp is anti-printwriters-attached-to-sockets with the reason being that they swallow exceptions which is not a good idea in networking programming in general.

                    And I think this is a good point.
                    • 7. Re: read/write a string in a socket
                      807603
                      thank you!
                      • 8. Re: read/write a string in a socket
                        796440
                        cotton.m wrote:
                        jverd wrote:
                        If you want to send a String across a socket, you could either use DataOutputStream.writeObject(yourString), or you could use wrap a BufferedWriter or PrintWriter around the socket's OutputStream
                        Something to note.

                        ejp is anti-printwriters-attached-to-sockets with the reason being that they swallow exceptions which is not a good idea in networking programming in general.

                        And I think this is a good point.
                        I hand't thought of that. I agree in general, but would say it should be evaluated for appropriateness on a case-by-case basis.
                        • 9. Re: read/write a string in a socket
                          807603
                          cotton.m wrote:
                          ejp is anti-printwriters-attached-to-sockets with the reason being that they swallow exceptions which is not a good idea in networking programming in general.
                          I knew PrintWriter's methods didn't throw IOException, but I didn't realise there was no "getLastException" method. Not good.
                          • 10. Re: read/write a string in a socket
                            807603
                            For reading from a socket, you may want to consider using my InfoFetcher class. I actually built InfoFetcher for exactly this purpose - reading from a Socket.

                            http://forum.java.sun.com/thread.jspa?threadID=759854&messageID=4338547

                            Cheers

                            -T
                            • 11. Re: read/write a string in a socket
                              EJP
                              I agree in general, but would say it should be evaluated for appropriateness on a case-by-case basis.
                              Of course, but now you need to come up with a case where it is OK to fail to write something to the network and not know about it and keep doing it, rather than close the socket and try again with a new one, or log it, or tell the user, or ... I've never found one.