2 Replies Latest reply on Apr 10, 2008 8:00 AM by 807601

    Server to client using Socket

      Hello, A program is running on server which is on the different computer in n/w and sending a string.I'm trying to write a client program which will read that string object...but the result does not come properly.
      when I use in.readByte(); the result comes but when use in.readLine() or in.readUTF() it fails and wait forever.
      If I use in.toString() the result is like java.io.DataInputStream@6b97fd.
      Please help me in solve this problem..
      thanks in advance
      import java.io.*;
      import java.net.Socket;
      public class MainClass extends Thread {
        public MainClass() throws Exception {
        public void run() {
          try {
                Socket socket = new Socket("", 10000);
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                DataInputStream in = new DataInputStream(socket.getInputStream());
                PrintWriter printstream=new PrintWriter(out);
                while (true) {
                   byte message = in.readByte();
                   //String msg1 = in.readUTF(); -->it doest not work,goes in waiting state
                   String msg = in.toString();
                   System.out.println("message >> "+message);// it works
                   System.out.println("msg >> "+msg); // it gives the o/p as java.io.DataInputStream@6b97fd
                     //System.out.println("connection ending here ...");
          } catch (IOException e) {
        public static void main(String[] args) {
          try {
            Thread t = new MainClass();
          } catch (Exception e) {
      Edited by: more_java on Apr 10, 2008 12:32 AM
        • 1. Re: Server to client using Socket
          How is the server sending the String?

          - if it's using DataOutputStream.writeUTF(), you have to use DataInputStream.readUTF()

          - if it's using PrintWriter.println(), or BufferedWriter.write()/newLine(), you have to use BufferedReader.readLine()

          - if it's using Writer.writeChars() you have to use Reader.readChars()

          - if it's using DataOutputStream.writeBytes() or write() you have to use DataInputStream.read().

          - ... etc, you get the picture

          And turning a DataInputStream into a String with toString() doesn't give you any of the input it has read. You have to store the data you've read somewhere.
          • 2. Re: Server to client using Socket
            Many thanks for your help...