0

I am using TCP socket connection. Server sends the data in an object and I need to receive and read it. Server sends the data like this

public class SymbolData
{
public string Symbol { get; set; }
public double AskPrice { get; set; }
public double BidPrice { get; set; }
public double Open { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Close { get; set; }
public double PerChange { get; set; }
public double NetChange { get; set; }
public int Volume { get; set; }
}

My code is as below. I donno my flow is correct or not.

public class Client implements Runnable, java.io.Serializable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            Socket s = new Socket("172.16.x.xx",xxx);

            //outgoing stream redirect to socket
            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));              
            InputStream is = new DataInputStream(s.getInputStream());
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            int nRead;
            byte[] data = new byte[1024];
            while ((nRead = is.read(data, 0, data.length)) != -1) {
              buffer.write(data, 0, nRead);
            }

            try {
                deserialize(data);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            buffer.flush();

           InputStreamReader(s.getInputStream()));
           s.close();


    } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }


    }

}

// Deseralize the bytes

public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream b = new ByteArrayInputStream(bytes);
    ObjectInputStream o = new ObjectInputStream(b);
    return o.readObject();
}

Please help

user2085965
  • 393
  • 2
  • 13
  • 33

0 Answers0